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.csdn.net/clever101/

posted @ 2013-11-21 11:24  朝拜SUNS  阅读(849)  评论(0编辑  收藏  举报