1、继承自CDoubleBufferImpl
template
class CDoubleBufferImpl
{
public:
// Overrideables
void DoPaint(CDCHandle /*dc*/)
{
// must be implemented in a derived class
ATLASSERT(FALSE);
}
// Message map and handlers
BEGIN_MSG_MAP(CDoubleBufferImpl)
MESSAGE_HANDLER(WM_ERASEBKGND, OnEraseBackground)
MESSAGE_HANDLER(WM_PAINT, OnPaint)
#ifndef _WIN32_WCE
MESSAGE_HANDLER(WM_PRINTCLIENT, OnPaint)
#endif // !_WIN32_WCE
END_MSG_MAP()
LRESULT OnEraseBackground(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
return 1; // no background painting needed
}
LRESULT OnPaint(UINT /*uMsg*/, WPARAM wParam, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
T* pT = static_cast(this);
ATLASSERT(::IsWindow(pT->m_hWnd));
if(wParam != NULL)
{
RECT rect = { 0 };
pT->GetClientRect(&rect);
CMemoryDC dcMem((HDC)wParam, rect);
pT->DoPaint(dcMem.m_hDC);
}
else
{
CPaintDC dc(pT->m_hWnd);
CMemoryDC dcMem(dc.m_hDC, dc.m_ps.rcPaint);
pT->DoPaint(dcMem.m_hDC);
}
return 0;
}
};
2、添加消息映射
3、增加DoPaint函数void DoPaint(CDCHandle dc);
4、将OnPaint的代码应用于DoPaint即可
完整代码:
MainDlg.h
// MainDlg.h : interface of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#pragma once
class CMainDlg : public CDialogImpl, public CDoubleBufferImpl
{
public:
enum { IDD = IDD_MAINDLG };
BEGIN_MSG_MAP(CMainDlg)
CHAIN_MSG_MAP(CDoubleBufferImpl)
MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)
MESSAGE_HANDLER(WM_SIZE, OnSize)
//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 OnSize(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*/);
void DoPaint(CDCHandle dc);
};
MainDlg.cpp
// MainDlg.cpp : implementation of the CMainDlg class
//
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "resource.h"
#include "MainDlg.h"
#include
#include
#include
#pragma comment (lib,"Gdiplus.lib")
using namespace Gdiplus;
ULONG_PTR gdiplusToken;
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);
GdiplusStartupInput gdiplusStartupInput;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
return TRUE;
}
LRESULT CMainDlg::OnCancel(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
EndDialog(wID);
GdiplusShutdown(gdiplusToken);
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);
*/
//RECT rect;
//获得客户区坐标
//GetClientRect(&rect);
//Graphics作图对象
//Graphics g(m_hWnd);
//画线
/*
Pen pen(Color(255, 0, 0, 0), 15);
g.DrawLine(&pen, rect.left, rect.top, rect.right, rect.bottom);
g.DrawLine(&pen, rect.left, rect.bottom, rect.right, rect.top);
*/
//画字符串
/*
SolidBrush brush(Color(255, 0, 0, 255));
FontFamily fontFamily(TEXT("Times New Roman"));
Font font(&fontFamily, 24, FontStyleRegular, UnitPixel);
PointF pt(10.0f, 10.0f);
g.DrawString(L"Hello World", -1, &font, pt, &brush);
*/
//画矩形
/*
Pen pen(Color(255, 0, 255, 0));
while (rect.left < rect.right && rect.top < rect.bottom)
{
g.DrawRectangle(&pen, rect.left, rect.top, rect.right, rect.bottom);
rect.left += 5;
rect.top += 5;
rect.right -= 10;
rect.bottom -= 10;
}
*/
//填充矩形
/*
SolidBrush brush(Color(255, 0, 255, 0));
brush.SetColor(Color(255, 0, 0, 255));
g.FillRectangle(&brush, rect.left, rect.top, rect.right, rect.bottom);
*/
//Image img(L"ing.png");
//g.DrawImage(&img, rect.left, rect.top, rect.right, rect.bottom);
//return TRUE;
//}
void CMainDlg::DoPaint(CDCHandle dc)
{
RECT rect;
GetClientRect(&rect);
//Graphics g(m_hWnd);
//Graphics g(dc.m_hDC);
Graphics g(dc);
Image img(L"forever.bmp");
g.DrawImage(&img, rect.left, rect.top, rect.right, rect.bottom);
}
LRESULT CMainDlg::OnSize(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
RedrawWindow();
return TRUE;
}
copy /b src.png + src.rar dest.png
完整代码将上面图片保存本地,改为rar格式即可获得