禁止RestartByRestartManager

有时候很烦人,程序跑着跑着就重启了,这并不是因为程序崩了,而是在新版的visual studio中对MFC程序默认增加了一个功能:程序自动重启。要禁用它,只须在程序最开始继承自CWinApp的类的构造函数中将下面一句话注释掉:

1
2
	// support Restart Manager
	//m_dwRestartManagerSupportFlags = AFX_RESTART_MANAGER_SUPPORT_RESTART;

win32渐变颜色矩形函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#define FILL_STYLE_VERTICAL	0
#define FILL_STYLE_HORIZONTAL	1
 
void fillGradientRect(HDC hdc, RECT *rc, COLORREF fromRGB, COLORREF toRGB, int fillStyle)
{
	BITMAPINFO bmpInfo;
	memset(&bmpInfo, 0, sizeof(bmpInfo));
	bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	bmpInfo.bmiHeader.biWidth = rc->right - rc->left;
	bmpInfo.bmiHeader.biHeight = rc->bottom - rc->top;
	bmpInfo.bmiHeader.biPlanes = 1;
	bmpInfo.bmiHeader.biBitCount = 32;
	bmpInfo.bmiHeader.biCompression = BI_RGB;
 
	BYTE *rgbBuffer = (BYTE *)malloc(bmpInfo.bmiHeader.biWidth * bmpInfo.bmiHeader.biHeight * 4);
 
	int width = bmpInfo.bmiHeader.biWidth;
	int height = bmpInfo.bmiHeader.biHeight;
 
	int rStart = fromRGB & 0xFF;
	int rEnd = toRGB & 0xFF;
	int gStart = (fromRGB & 0xFF00) >> 8;
	int gEnd = (toRGB & 0xFF00) >> 8;
	int bStart = (fromRGB & 0xFF0000) >> 16;
	int bEnd = (toRGB & 0xFF0000) >> 16;
	if (FILL_STYLE_VERTICAL == fillStyle)
	{
		double rStep = (rEnd - rStart) * 1.0 / height;
		double gStep = (gEnd - gStart) * 1.0 / height;
		double bStep = (bEnd - bStart) * 1.0 / height;
		for (int y = 0; y < height; y++)
		{
			BYTE r = (BYTE)(rStart + rStep * y);
			BYTE g = (BYTE)(gStart + gStep * y);
			BYTE b = (BYTE)(bStart + bStep * y);
			for (int x = 0; x < width; x++)
			{
				BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4;
				*p++ = b;
				*p++ = g;
				*p++ = r;
			}
		}
	}
	else
	{
		double rStep = (rEnd - rStart) * 1.0 / width;
		double gStep = (gEnd - gStart) * 1.0 / width;
		double bStep = (bEnd - bStart) * 1.0 / width;
		for (int x = 0; x < width; x++)
		{
			BYTE r = (BYTE)(rStart + rStep * x);
			BYTE g = (BYTE)(gStart + gStep * x);
			BYTE b = (BYTE)(bStart + bStep * x);
			for (int y = 0; y < height; y++)
			{
				BYTE *p = rgbBuffer + ((height - y) * width - (width - x)) * 4;
				*p++ = b;
				*p++ = g;
				*p++ = r;
			}
		}
	}
 
	StretchDIBits(hdc, rc->left, rc->top, width, height, 0, 0, width, height, rgbBuffer, &bmpInfo, DIB_RGB_COLORS, SRCCOPY);
 
	free(rgbBuffer);
}
 
void CMFCApplicationDlg::OnPaint()
{
	CPaintDC dc(this);
	CRect rc(50, 50, 200, 200);
	fillGradientRect(dc.m_hDC, rc, RGB(255, 0, 255), RGB(0, 255, 0), FILL_STYLE_HORIZONTAL);
 
	CDialogEx::OnPaint();
}

MFC CScrollBar拖动完后总跑到零位置

原因是

  1. 要处理SB_ENDSCROLL事件。
  2. 一定要在适当的地方GetScrollPos或SetScrollPos。

正确示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
void SampleDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	int iMin = 0, iMax = 0;
	pScrollBar->GetScrollRange(&iMin, &iMax);
 
	// nPos = pScrollBar->GetScrollPos(); // 此处这句话一定不能有
 
	switch (nSBCode)
	{
	case SB_THUMBTRACK:
		break;
	case SB_LINELEFT:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		nPos--;
		if (nPos < (UINT)iMin)
			nPos = 0;
		break;
	case SB_LINERIGHT:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		nPos++;
		if (nPos >= (UINT)iMax)
			nPos = iMax - 1;
		break;
	case SB_ENDSCROLL:
		nPos = pScrollBar->GetScrollPos(); // 此处这句话一定要有
		break;
	default:
		break;
	}
 
	pScrollBar->SetScrollPos(nPos, 1); // 此处这句话也一定要有
 
	// Todo real work with nPos.
 
	CDialogEx::OnHScroll(nSBCode, nPos, pScrollBar);
}

error C4839: 作为可变参数函数的参数的非标准用法

报错语句为:

1
	_stprintf_s(filepath, _T("%s\\%s"), curpath, CA2T(iniFilePath));

改为如下就不报错了:

1
2
	CString iniFilePathT = CA2T(iniFilePath);
	_stprintf_s(filepath, _T("%s\\%s"), curpath, (LPCTSTR)iniFilePathT);

MFC重绘控件

举个按钮简单重绘的例子。

头文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CMyButton : public CButton
{
public:
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
 
class CMFCApplication1Dlg : public CDialogEx
{
	...
protected:
	virtual void DoDataExchange(CDataExchange* pDX);
	CMyButton m_myButton;
	...
};

Read more