先载入一张图片, .。ID为IDB_BITMAP1CBrush m_brBk;//在public中定义
#pragma once// LoginDlg 对话框class LoginDlg : public CDialog{ DECLARE_DYNAMIC(LoginDlg)public: LoginDlg(CWnd* pParent = NULL); // 标准构造函数 virtual ~LoginDlg();// 对话框数据 enum { IDD = IDD_DIALOG_LogIn };protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持 DECLARE_MESSAGE_MAP()public: CString m_name; CString m_password; CBrush m_brBk ; int log_time ; afx_msg void OnBnClickedButtonlog(); afx_msg void OnBnClickedButtonloginexit(); virtual BOOL OnInitDialog(); afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);};
BOOL LoginDlg::OnInitDialog(){ CDialog::OnInitDialog(); CBitmap bmp ; bmp.LoadBitmap(IDB_BITMAP1) ; m_brBk.CreatePatternBrush(&bmp); bmp.DeleteObject(); // TODO: 在此添加额外的初始化 return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE}
打开类向导,找到WM_CTLCOLOR消息,重载得对应函数OnCtlColor(),添加如下:
HBRUSH LoginDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor){ HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); if(pWnd == this) return m_brBk ; return hbr;}