环境: Windows 10, VS 2022 (x64).
只要把颜色初始化函数放在类的构造函数里就会引发异常:
0x00007FF6D7085729 处(位于 ProgressBarControl.exe 中)引发的异常: 0xC0000005: 读取位置 0x0000000000000000 时发生访问冲突。
完整代码:
#pragma once
#include <graphics.h>
struct ProgressBarCtrlPosition
{
int mLeft;
int mTop;
int mRight;
int mBottom;
};
struct ProgressBarCtrlStyles
{
COLORREF mLineColor;
COLORREF mBkColor;
COLORREF mFillColor;
};
class ProgressBarControl
{
public:
ProgressBarControl(int left, int top, int right, int bottom)
{
mPosition.mLeft = left;
mPosition.mTop = top;
mPosition.mRight = right;
mPosition.mBottom = bottom;
mStyles.mLineColor = BLACK;
mStyles.mBkColor = WHITE;
mStyles.mFillColor = LIGHTBLUE;
setlinecolor(mStyles.mLineColor);
setbkcolor(mStyles.mBkColor);
setfillcolor(mStyles.mFillColor);
}
void Draw()
{
rectangle(mPosition.mLeft, mPosition.mTop, mPosition.mRight, mPosition.mBottom);
fillrectangle(mPosition.mLeft, mPosition.mTop, mPosition.mRight - 100, mPosition.mBottom);
}
private:
ProgressBarCtrlPosition mPosition;
ProgressBarCtrlStyles mStyles;
};