VC 中怎样改变窗体背景色

首先在头文件里定义一个背景色的宏:

#define DLG_BACKGROUND RGB(220, 210, 201)


然后在CPP文件里添加映射二个函数:

BEGIN_MESSAGE_MAP(CDb9EWaveProDlg, CDialogEx)

  ON_WM_CTLCOLOR()//字体颜色
  ON_WM_ERASEBKGND()  //背景色

END_MESSAGE_MAP()



重载函数定义例如以下:

HBRUSH CxxDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr =  CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

	if (nCtlColor == CTLCOLOR_EDIT || nCtlColor == CTLCOLOR_LISTBOX)
	{
		pDC->SetBkColor(RGB(255, 255, 255));   

		return hbr;
	}
	else
	{
		HBRUSH hBrush =  CreateSolidBrush(DLG_BACKGROUND); //创建背景刷;
		pDC->SetBkMode(TRANSPARENT);
		return hBrush;
	}
}
 

BOOL CxxDlg::OnEraseBkgnd(CDC* pDC) 
{
	 CBrush back(DLG_BACKGROUND);
	 CBrush* pold=pDC->SelectObject(&back);
	 CRect rect;
	 pDC->GetClipBox (&rect);
	 pDC->PatBlt (rect.left,rect.top,rect.Width(),rect.Height(),PATCOPY);
         pDC->SelectObject(pold);
	return TRUE; 
}



OK, Down.

posted on   wgwyanfs  阅读(239)  评论(0编辑  收藏  举报

努力加载评论中...

导航

< 2025年1月 >
29 30 31 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 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示