• WTL对话框添加背景图片


    WTL91_5321_Final + VS2013 + WIN7

    // MainDlg.h : interface of the CMainDlg class
    //
    /////////////////////////////////////////////////////////////////////////////
    
    #pragma once
    #include <GdiPlus.h>
    #include <atlimage.h>
    extern Gdiplus::GdiplusStartupInput gGdiInput;
    
    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 OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);
    	LRESULT OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/);
    private:
    	CImage bg;
    };
    
    1、对话框头文件(MainDlg.h)添加:

    #include <GdiPlus.h>
    extern Gdiplus::GdiplusStartupInput gGdiInput;
    2、添加消息映射

    MESSAGE_HANDLER(WM_PAINT, OnPaint)

    LRESULT OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/);

    3、对话框源文件(MainDlg.cpp)添加:

    #pragma comment(lib, "GdiPlus.lib")
    Gdiplus::GdiplusStartupInput gGdiInput;
    ULONG token = 0;

    OnInitDialog函数内进行Gdiplus初始化

    GdiplusStartup(&token, &gGdiInput, NULL);

    重载OnPaint消息响应函数:

    LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
    	CPaintDC dc(m_hWnd);
    	RECT rect;
    	GetClientRect(&rect);
    	HDC hDC = dc.m_hDC;
    	Gdiplus::Graphics g(hDC);
    	Gdiplus::Image im(_T("res/green.jpg"));
    	g.DrawImage(&im, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
    	return TRUE;
    }
    关闭:

    Gdiplus::GdiplusShutdown(token);

    // MainDlg.cpp : implementation of the CMainDlg class
    //
    /////////////////////////////////////////////////////////////////////////////
    
    #include "stdafx.h"
    #include "resource.h"
    
    #include "MainDlg.h"
    #include "table.h"
    #pragma comment(lib, "GdiPlus.lib")
    Gdiplus::GdiplusStartupInput gGdiInput;
    ULONG token = 0;
    
    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);
    
    	GdiplusStartup(&token, &gGdiInput, NULL);
    
    
    	//CTable table;
    
    	return TRUE;
    }
    
    LRESULT CMainDlg::OnPaint(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
    {
    	CPaintDC dc(m_hWnd);
    	RECT rect;
    	GetClientRect(&rect);
    	HDC hDC = dc.m_hDC;
    	Gdiplus::Graphics g(hDC);
    	Gdiplus::Image im(_T("res/green.jpg"));
    	g.DrawImage(&im, 0, 0, rect.right - rect.left, rect.bottom - rect.top);
    	return TRUE;
    }
    
    LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
    {
    	bg.Destroy();
    	Gdiplus::GdiplusShutdown(token);
    	EndDialog(wID);
    	return 0;
    }
    


    参考于:

    http://blog.csdn.net/faithzzf/article/details/51897140

    Keep it simple!
    作者:N3verL4nd
    知识共享,欢迎转载。
  • 相关阅读:
    阅读笔记(四)——《代码整洁之道》1
    阅读笔记(一)——《需求工程——软件建模与分析》一
    数据提取
    HIVE数据清洗练习
    第八周hadoop-MapReduce
    小程序新增页面导航栏
    个人课程总结
    第十六周总结

    排序
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/6616367.html
Copyright © 2020-2023  润新知