WTL设置对话框背影色
阅读原文时间:2023年07月11日阅读:6

MainDlg.h

// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#pragma once

class CMainDlg : public CDialogImpl
{
public:
enum { IDD = IDD_MAINDLG };

BEGIN\_MSG\_MAP(CMainDlg)  
    MESSAGE\_HANDLER(WM\_INITDIALOG, OnInitDialog)  
    MESSAGE\_HANDLER(WM\_PAINT, OnPaint)  
    COMMAND\_ID\_HANDLER(IDCANCEL, OnCancel)  
END\_MSG\_MAP()

// Handler prototypes (uncomment arguments if needed):
// LRESULT MessageHandler(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
// LRESULT CommandHandler(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
// LRESULT NotifyHandler(int /*idCtrl*/, LPNMHDR /*pnmh*/, BOOL& /*bHandled*/)

LRESULT OnInitDialog(UINT /\*uMsg\*/, WPARAM /\*wParam\*/, LPARAM /\*lParam\*/, BOOL& /\*bHandled\*/);  
LRESULT OnCancel(WORD /\*wNotifyCode\*/, WORD wID, HWND /\*hWndCtl\*/, BOOL& /\*bHandled\*/);  
LRESULT OnPaint(UINT /\*uMsg\*/, WPARAM /\*wParam\*/, LPARAM /\*lParam\*/, BOOL& /\*bHandled\*/);  

};

MainDlg.cpp

// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "resource.h"

#include "MainDlg.h"
#include
#include

LRESULT CMainDlg::OnInitDialog(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
// center the dialog on the screen
CenterWindow();

// set icons  
HICON hIcon = AtlLoadIconImage(IDR\_MAINFRAME, LR\_DEFAULTCOLOR, ::GetSystemMetrics(SM\_CXICON), ::GetSystemMetrics(SM\_CYICON));  
SetIcon(hIcon, TRUE);  
HICON hIconSmall = AtlLoadIconImage(IDR\_MAINFRAME, LR\_DEFAULTCOLOR, ::GetSystemMetrics(SM\_CXSMICON), ::GetSystemMetrics(SM\_CYSMICON));  
SetIcon(hIconSmall, FALSE);

return TRUE;  

}

LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
EndDialog(wID);
return 0;
}

LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
srand((unsigned)time(NULL));
CPaintDC dc(this->m_hWnd);
CBrush brush;
CRect rect;
GetClientRect(&rect);
InvalidateRect(&rect, FALSE);
brush.CreateSolidBrush(RGB(rand() % 255, rand() % 255, rand() % 255));
dc.FillRect(&rect, brush);
return TRUE;
}

无限次刷新客户区,实现重绘。

class CPaintDC : public CDC
{
public:
// Data members
HWND m_hWnd;
PAINTSTRUCT m_ps;

// Constructor/destructor
CPaintDC(HWND hWnd)
{
ATLASSERT(::IsWindow(hWnd));
m_hWnd = hWnd;
m_hDC = ::BeginPaint(hWnd, &m_ps);
}

~CPaintDC()  
{  
    ATLASSERT(m\_hDC != NULL);  
    ATLASSERT(::IsWindow(m\_hWnd));  
    ::EndPaint(m\_hWnd, &m\_ps);  
    Detach();  
}  

};

可见,CPaintDC封装了只能在WM_PAINT消息下处理的BeginPaint、EndPaint函数。

case WM_PAINT:

hdc = BeginPaint(hwnd, &ps);

EndPaint(hwnd, &ps);

return 0;

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章