1、继承自CDoubleBufferImpl

template <class T>
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<T*>(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、添加消息映射

CHAIN_MSG_MAP(CDoubleBufferImpl<CMainDlg>)

3、增加DoPaint函数void DoPaint(CDCHandle dc);

4、将OnPaint的代码应用于DoPaint即可

完整代码:

MainDlg.h

// MainDlg.h : interface of the CMainDlg class
//
///////////////////////////////////////////////////////////////////////////// #pragma once class CMainDlg : public CDialogImpl<CMainDlg>, public CDoubleBufferImpl<CMainDlg>
{
public:
enum { IDD = IDD_MAINDLG }; BEGIN_MSG_MAP(CMainDlg)
CHAIN_MSG_MAP(CDoubleBufferImpl<CMainDlg>)
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 <atlimage.h>
#include <time.h>
#include <gdiplus.h>
#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格式即可获得

WTL使用双缓冲避免重绘闪烁的更多相关文章

  1. [转载] MFC绘制动态曲线,用双缓冲绘图技术防闪烁

    转载的原文地址 先上效果图 随着时间的推移,曲线向右平移,同时X轴的时间坐标跟着更新. 一.如何绘制动态曲线 所谓动画,都是一帧一帧的图像连续呈现在用户面前形成的.所以如果你掌握了如何绘制静态曲线,那 ...

  2. 【MFC】MFC绘制动态曲线,用双缓冲绘图技术防闪烁

    摘自:http://zhy1987819.blog.163.com/blog/static/841427882011614103454335/ MFC绘制动态曲线,用双缓冲绘图技术防闪烁   2011 ...

  3. MFC双缓冲解决图象闪烁[转]

    转载网上找到的一篇双缓冲的文章,很好用.http://www.cnblogs.com/piggger/archive/2009/05/02/1447917.html__________________ ...

  4. Duilib的双缓冲实现,附带GDI、WTL的双缓冲实现

    前言: 闪烁问题,之前的经验是使用双缓冲,借此机会,把双缓冲的研究心得总结下. 双缓冲的含义: 缓冲这个词,相信大家都不陌生,Cache.主要是为了解决上下游(或者模块.或者系统)等性能不匹配问题.如 ...

  5. (Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

    Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif"); pictureBox1.Image =img.Clo ...

  6. Winfrom 减少控件重绘闪烁的方法

    Winform控件的双缓冲.控件的双缓冲属性是隐藏的,可以通过反射改变其属性值. lv.GetType().GetProperty("DoubleBuffered", Bindin ...

  7. Winform解决界面重绘闪烁的问题

    在窗体或用户控件中重写CreateParams protected override CreateParams CreateParams { get { CreateParams cp = base. ...

  8. 强制设置双缓冲DoubleBuffered 解决tableLayoutPanel 闪烁

    tableLayoutPanel.GetType().GetProperty("DoubleBuffered", System.Reflection.BindingFlags.In ...

  9. VC使用双缓冲避免绘图闪烁的正确使用方法【转】

    使用内存DC绘图,然后实现双缓冲,避免绘图闪烁,这个小技术简单但很有效.但是仍然有很多人说使用了双缓冲,图片却仍然有闪烁,分析了几个这样的例子,发现 其实不是双缓冲的技术问题,而是使用者没有正确理解和 ...

随机推荐

  1. DataX-MysqlReader 插件文档

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  2. Docker——WIN7 安装 Docker实战与入门

    1.Docker简介 Docker 是一个开源项目,诞生于 2013 年初,最初是 dotCloud 公司内部的一个业余项目.它基于 Google 公司推出的 Go 语言实现. 项目后来加入了 Lin ...

  3. 基于C#的机器学习--垃圾邮件过滤

    在这一章,我们将建立一个垃圾邮件过滤分类模型.我们将使用一个包含垃圾邮件和非垃圾邮件的原始电子邮件数据集,并使用它来训练我们的ML模型.我们将开始遵循上一章讨论的开发ML模型的步骤.这将帮助我们理解工 ...

  4. 用python搭一个超简易的文件服务器

    这个文件服务器纯粹是在学习python cgi编程时,顺便玩玩而已,因为搭文件服务器的话完全可以linux,简单方便,这里就是随便玩玩,功能也就是只能下载文件 1.登录页面,做个简单验证 新建一个ht ...

  5. python切片(获取一个子列表(数组))

    切片: 切片指从现有列表中,获取一个子列表 返回一个新列表,不影响原列表. 下标以 0 开始: list = ['红','绿','蓝','白','黑','黄','青']# 下标 0 1 2 3 4 5 ...

  6. python的input()函数

    # input()函数 # 作用: 获取用户的输入,返回输入的内容 ,也可以用于暂停程序的运行 # 影响: 调用此函数,程序会立即暂停,等待用户输入 # 注意:input()的返回值是一个字符串 # ...

  7. selenium webdriver从安装到使用(python语言),显示等待和隐性等待用法,切换窗口或者frame,弹框处理,下拉菜单处理,模拟鼠标键盘操作等

    selenium的用法 selenium2.0主要包含selenium IDE 和selenium webDriver,IDE有点类似QTP和LoadRunner的录制功能,就是firefox浏览器的 ...

  8. Window下安装并使用InfluxDB可视化工具 —— InfluxDBStudio

    下载 直接访问: https://github.com/CymaticLabs/InfluxDBStudio/releases/tag/v0.2.0-beta.1 创建or编辑InfluxDB 这个软 ...

  9. JMeter——聚合报告

    AggregateReport 是 JMeter 常用的一个 Listener,中文被翻译为“聚合报告”. ​ 对于每个请求,它统计响应信息并提供请求数,平均值,最大,最小值,错误率,大约吞吐量(以请 ...

  10. Flask路由+视图补充

    一.路由设置的两种方法 1.装饰器 @app.route('/index/') def index(): return 'Hello World!' 2.源码 route->decorator- ...