内存DC解决窗口闪烁问题


分类: 半路出家之Windows编程


null 图形 mfc windows class 网络


在使用vc开发图形相关的应用程序时,常常需要使用MFC的CDC类直接把图形画在窗口上。这通常是通过响应windows的WM_PAINT消息实现的。如果要画的图形比较复杂,或者比较大,那么画图过程可能会造成窗口的闪烁。当窗口调整大小时,这种闪烁由为明显。

      解决窗口闪烁问题的有效办法就是使用内存DC,也称为缓冲DC。在内存中准备一个和窗口DC相同属性的DC,在这个内存DC上执行画图操作。完成画图以后,把画图输出的内容整体复制到目标窗口DC上。因为画图操作不在窗口DC上进行,所以在画图的过程中窗口可以保持原来的内容。当画好的内容被复制到窗口DC时,因为复制操作执行的非常快,所以用户感觉窗口仿佛被立刻被画好,从而消除了从旧画面到白板再到新画面的闪烁现象。

      生成内存DC主要用到以下四个函数:

      CreateCompatibleDC(CDC* pDC )。CDC类的成员函数,用于创建一个和pDC指向的DC兼容的内存DC。

      CreateDiscardableBitmap( CDC* pDC, int nWidth, int nHeight)。CBitmap类的成员函数,用于按指定尺寸创建一个和pDC指向的DC兼容的位图。   

      SelectObject(CBitmap * pBitmap)。CDC类的成员函数,执行以后,所以在该DC上的图像输出都将被画到pBitmap指向的位图上。
      BOOL BitBlt (int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop )。CDC类的成员函数,用于从源DC(pSrcDC)复制一个矩形的图象到当前DC中。

应用过程:
      1.以屏幕DC建立内存DC(CreateCompatibleDC),以屏幕DC建立位图(CreateCompatibleBitmap),大小使用GetClientRect取得客户区大小。将上面创建的位图选入内存DC中(SelectObject)。
      2.所有的绘图动作都针对内存DC进行。
      3.完成绘图动作后,将内存DC中内容复制到屏幕DC中。
      4.释放资源。
      注意点:建立位图一定要使用屏幕DC;如果屏幕DC有使用调色板,则必须将此调色板选入内存DC中,否则将发生色彩不正确的情况。

      下面是从网络上找到的一个内存DC类,可直接放在工程中应用。

class CMemDC : public CDC {
 private: 
  CBitmap  m_bitmap;  // Offscreen bitmap
  CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  CDC*  m_pDC;   // Saves CDC passed in constructor
  CRect  m_rect;   // Rectangle of drawing area.
  BOOL  m_bMemDC;  // TRUE if CDC really is a Memory DC.
 public:
  
  CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
  {
   ASSERT(pDC != NULL);  // Some initialization
   m_pDC = pDC;
   m_oldBitmap = NULL;
   m_bMemDC = !pDC->IsPrinting();  // Get the rectangle to draw
   if (pRect == NULL) {
    pDC->GetClipBox(&m_rect);
   } else {
    m_rect = *pRect;
   }
   
   if (m_bMemDC) {
    // Create a Memory DC
    CreateCompatibleDC(pDC);
    pDC->LPtoDP(&m_rect);   m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
    m_oldBitmap = SelectObject(&m_bitmap);
    
    SetMapMode(pDC->GetMapMode());
    pDC->DPtoLP(&m_rect);
    SetWindowOrg(m_rect.left, m_rect.top);
   } else {
    // Make a copy of the relevent parts of the current DC for printing
    m_bPrinting = pDC->m_bPrinting;
    m_hDC       = pDC->m_hDC;
    m_hAttribDC = pDC->m_hAttribDC;
   }  // Fill background 
   FillSolidRect(m_rect, pDC->GetBkColor());
  } 
  ~CMemDC() 
  {  
   if (m_bMemDC) {
    // Copy the offscreen bitmap onto the screen.
    m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
     this, m_rect.left, m_rect.top, SRCCOPY);   
    
    //Swap back the original bitmap.
    SelectObject(m_oldBitmap);
   } else {
    // All we need to do is replace the DC with an illegal value,
    // this keeps us from accidently deleting the handles associated with
    // the CDC that was passed to the constructor.   
    m_hDC = m_hAttribDC = NULL;
   } 
  }
  
  // Allow usage as a pointer 
  CMemDC* operator->() 
  {
   return this;
  }  // Allow usage as a pointer 
  operator CMemDC*() 
  {
   return this;
  }
 };



VC++软件扉页的制作


vc++ components timer null c


      许多商品化应用软件(包括VC)在启动时都会显示一个封面(Splash Screen),给人以耳目一新的感觉



下面介绍在VC++中怎么向程序中添加封面。



      选择菜单Project|Add to Project|Component and Controls,出现Component and Control Gallery对话框,选择Visual C++ Components。在此目录下,选择组件Splash Screen,选择添加Splash Screen组件,此时工程中多了Splash.cpp和Splash.h两个文件,IDB_SPLASH位图资源,您可以根据自己的需要修改该位图,这个位图就是要显示的软件封面。在Splash.cpp的过程OnCreate中可以改变扉页的停留时间:


int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
 
 
{
 
 
    if(CWnd::OnCreate(lpCreateStruct) == -1)
 
 
        return -1;
 
 
    //Center the Window
 
 
    CenterWindow();
 
 
    //Set a timer to destroy the splash screen
 
 
    SetTimer(1,750,NULL);  //750表示扉页停留的时间,单位毫秒
 
 
    return 0;
 
 
}




在使用vc开发图形相关的应用程序时,常常需要使用MFC的CDC类直接把图形画在窗口上。这通常是通过响应windows的WM_PAINT消息实现的。如果要画的图形比较复杂,或者比较大,那么画图过程可能会造成窗口的闪烁。当窗口调整大小时,这种闪烁由为明显。

      解决窗口闪烁问题的有效办法就是使用内存DC,也称为缓冲DC。在内存中准备一个和窗口DC相同属性的DC,在这个内存DC上执行画图操作。完成画图以后,把画图输出的内容整体复制到目标窗口DC上。因为画图操作不在窗口DC上进行,所以在画图的过程中窗口可以保持原来的内容。当画好的内容被复制到窗口DC时,因为复制操作执行的非常快,所以用户感觉窗口仿佛被立刻被画好,从而消除了从旧画面到白板再到新画面的闪烁现象。

      生成内存DC主要用到以下四个函数:

      CreateCompatibleDC(CDC* pDC )。CDC类的成员函数,用于创建一个和pDC指向的DC兼容的内存DC。

      CreateDiscardableBitmap( CDC* pDC, int nWidth, int nHeight)。CBitmap类的成员函数,用于按指定尺寸创建一个和pDC指向的DC兼容的位图。   

      SelectObject(CBitmap * pBitmap)。CDC类的成员函数,执行以后,所以在该DC上的图像输出都将被画到pBitmap指向的位图上。
      BOOL BitBlt (int x, int y, int nWidth, int nHeight, CDC* pSrcDC, int xSrc, int ySrc, DWORD dwRop )。CDC类的成员函数,用于从源DC(pSrcDC)复制一个矩形的图象到当前DC中。

应用过程:
      1.以屏幕DC建立内存DC(CreateCompatibleDC),以屏幕DC建立位图(CreateCompatibleBitmap),大小使用GetClientRect取得客户区大小。将上面创建的位图选入内存DC中(SelectObject)。
      2.所有的绘图动作都针对内存DC进行。
      3.完成绘图动作后,将内存DC中内容复制到屏幕DC中。
      4.释放资源。
      注意点:建立位图一定要使用屏幕DC;如果屏幕DC有使用调色板,则必须将此调色板选入内存DC中,否则将发生色彩不正确的情况。

      下面是从网络上找到的一个内存DC类,可直接放在工程中应用。

class CMemDC : public CDC {
 private: 
  CBitmap  m_bitmap;  // Offscreen bitmap
  CBitmap* m_oldBitmap; // bitmap originally found in CMemDC
  CDC*  m_pDC;   // Saves CDC passed in constructor
  CRect  m_rect;   // Rectangle of drawing area.
  BOOL  m_bMemDC;  // TRUE if CDC really is a Memory DC.
 public:
  
  CMemDC(CDC* pDC, const CRect* pRect = NULL) : CDC()
  {
   ASSERT(pDC != NULL);  // Some initialization
   m_pDC = pDC;
   m_oldBitmap = NULL;
   m_bMemDC = !pDC->IsPrinting();  // Get the rectangle to draw
   if (pRect == NULL) {
    pDC->GetClipBox(&m_rect);
   } else {
    m_rect = *pRect;
   }
   
   if (m_bMemDC) {
    // Create a Memory DC
    CreateCompatibleDC(pDC);
    pDC->LPtoDP(&m_rect);   m_bitmap.CreateCompatibleBitmap(pDC, m_rect.Width(), m_rect.Height());
    m_oldBitmap = SelectObject(&m_bitmap);
    
    SetMapMode(pDC->GetMapMode());
    pDC->DPtoLP(&m_rect);
    SetWindowOrg(m_rect.left, m_rect.top);
   } else {
    // Make a copy of the relevent parts of the current DC for printing
    m_bPrinting = pDC->m_bPrinting;
    m_hDC       = pDC->m_hDC;
    m_hAttribDC = pDC->m_hAttribDC;
   }  // Fill background 
   FillSolidRect(m_rect, pDC->GetBkColor());
  } 
  ~CMemDC() 
  {  
   if (m_bMemDC) {
    // Copy the offscreen bitmap onto the screen.
    m_pDC->BitBlt(m_rect.left, m_rect.top, m_rect.Width(), m_rect.Height(),
     this, m_rect.left, m_rect.top, SRCCOPY);   
    
    //Swap back the original bitmap.
    SelectObject(m_oldBitmap);
   } else {
    // All we need to do is replace the DC with an illegal value,
    // this keeps us from accidently deleting the handles associated with
    // the CDC that was passed to the constructor.   
    m_hDC = m_hAttribDC = NULL;
   } 
  }
  
  // Allow usage as a pointer 
  CMemDC* operator->() 
  {
   return this;
  }  // Allow usage as a pointer 
  operator CMemDC*() 
  {
   return this;
  }
 };

      许多商品化应用软件(包括VC)在启动时都会显示一个封面(Splash Screen),给人以耳目一新的感觉



下面介绍在VC++中怎么向程序中添加封面。



      选择菜单Project|Add to Project|Component and Controls,出现Component and Control Gallery对话框,选择Visual C++ Components。在此目录下,选择组件Splash Screen,选择添加Splash Screen组件,此时工程中多了Splash.cpp和Splash.h两个文件,IDB_SPLASH位图资源,您可以根据自己的需要修改该位图,这个位图就是要显示的软件封面。在Splash.cpp的过程OnCreate中可以改变扉页的停留时间:


int CSplashWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
 
 
{
 
 
    if(CWnd::OnCreate(lpCreateStruct) == -1)
 
 
        return -1;
 
 
    //Center the Window
 
 
    CenterWindow();
 
 
    //Set a timer to destroy the splash screen
 
 
    SetTimer(1,750,NULL);  //750表示扉页停留的时间,单位毫秒
 
 
    return 0;
 
 
}