• WTL设置对话框背影色


    MainDlg.h

    // MainDlg.h : interface of the CMainDlg class
    //
    /////////////////////////////////////////////////////////////////////////////
    
    #pragma once
    
    class CMainDlg : public CDialogImpl<CMainDlg>
    {
    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 <atlimage.h>
    #include <time.h>
    
    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;



    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    Unity动态更换图片
    (特殊的)增删改查
    SQL Server 锁
    [转]排序规则
    [转]C#编写Windows服务程序图文教程
    [转]FreeTextBox使用详解 (版本3.1.1)
    [转]Newtonsoft.Json序列化和反序列
    C#性能优化实践(摘抄)
    一、PID控制原理
    POJ 2255已知二叉树前序中序求后序
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616370.html
Copyright © 2020-2023  润新知