MFC中利用GDI+进行双缓冲作图的有关设置

  这里只是在遇到实际问题的时候提出的一种解决方法,用以处理闪屏问题。

  首先要做的是对GDI的一个设置问题:

  在应用程序类中添加一个保护权限数据成员

1 class C...App:
2 {...
3 private:
4     ULONG_PTR m_gdiplusToken;
5 }

  在相应的cpp文件中,添加头文件。之所以把头文件放到cpp文件中是为了防止过多的引用

#include <Gidplus.h>

  然后再应用程序类的初始函数和退出函数进行修改:

1 BOOL C...App::InitInstance()
2 {
3     CWinAppEx::InitInstance();
4 
5     //GDI初始化
6     Gdiplus::GdiplusStartupInput StartupInput;
7     GdiplusStartup(&m_gdiplusToken, &StartupInput, NULL);
8         ...
9 }
1 int C...App::ExitInstance()
2 {
3     //GDI销毁
4     Gdiplus::GdiplusShutdown(m_gdiplusToken);
5         ...  
6 }    

  这样便对GDI+的初始化进行设置完毕,然后修改View类中相关代码,看是否可以达到双缓冲效果。

  设计思路是做两个graphics,一个用来显示,一个用来作图,最后要做的是将缓存区中的图贴到前台来,就可以有效地处理闪屏问题。

 

 1 void C...View::OnDraw(CDC* pDC)
 2  {
 3      C...Doc* pDoc = GetDocument();
 4      ASSERT_VALID(pDoc);
 5      if (!pDoc)
 6          return;
 7  
 8      CRect client_rect;
 9      GetClientRect(client_rect);
10      Graphics graphics(pDC->m_hDC);
11      Bitmap bitmap(client_rect.Width(), client_rect.Height(), &graphics);
12      Graphics buffer_graphics(&bitmap);
13      SolidBrush BKbrush(Color::White);                //将背景刷白
14      buffer_graphics.FillRectangle(&BKbrush, 0, 0, client_rect.Width(),     client_rect.Height());
15  
16       //测试
17      SolidBrush font_brush(Color::Black);
18      FontFamily font_family(L"宋体");
19      Gdiplus::Font font(&font_family, 24, Gdiplus::FontStyleRegular, Gdiplus::UnitPixel);
20      PointF pointF(REAL(client_rect.Width() / 2), REAL(client_rect.Height() / 2));
21      CString channel_name;
22      channel_name.Format(_T("Test"));
23      graphics.DrawString(channel_name, -1, &font, pointF, &font_brush);
24 
25  
26      graphics.DrawImage(&bitmap, client_rect.left, client_rect.top, client_rect.right, client_rect.bottom);        //将bitmap拷贝到前台
27      
28 }

 

参考:http://blog.youkuaiyun.com/clever101/

转载于:https://www.cnblogs.com/salan668/p/3435211.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值