💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
1、将Invalidate()替换为InvalidateRect() Invalidate()会导致整个窗口的图象重画,需要的时间比较长,而InvalidateRect()仅仅重画Rect区域内的内容,所以所需时间会少一些。虫虫以前很懒,经常为一小块区域的重画就调用Invalidate(),不愿意自己去计算需要重画的Rect,但是事实是,如果你确实需要改善闪烁的情况,计算一个Rect所用的时间比起重画那些不需要重画的内容所需要的时间要少得多。  2、禁止系统搽除你的窗口 系统在需要重画窗口的时候会帮你用指定的背景色来搽除窗口。可是,也许需要重画的区域也许非常小。或者,在你重画这些东西之间还要经过大量的计算才能开始。这个时候你可以禁止系统搽掉原来的图象。直到你已经计算好了所有的数据,自己把那些需要搽掉的部分用背景色覆盖掉(如:dc.FillRect(rect,&brush);rect是需要搽除的区域,brush是带背景色的刷子),再画上新的图形。要禁止系统搽除你的窗口,可以重载OnEraseBkgnd()函数,让其直接返回pUE就可以了。如  <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>BOOL CMyWin::OnEraseBkgnd(CDC* pDC) <br/>{ <br/> return pUE; <br/> //return CWnd::OnEraseBkgnd(pDC);//把系统原来的这条语句注释掉。 <br/>}</td></tr></tbody></table> 3、有效的进行搽除 搽除背景的时候,不要该搽不该搽的地方都搽。比如,你在一个窗口上放了一个很大的Edit框,几乎占了整个窗口,那么你频繁的搽除整个窗口背景将导致Edit不停重画形成剧烈的闪烁。事实上你可以CRgn创建一个需要搽除的区域,只搽除这一部分。如  <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>GetClientRect(rectClient); <br/>rgn1.CreateRectRgnIndirect(rectClient); <br/>rgn2.CreateRectRgnIndirect(m_rectEdit); <br/>if(rgn1.CombineRgn(&amp;rgn1,&amp;rgn2,RGN_XOR) == ERROR)//处理后的rgn1只包括了Edit框之外的客户区域,这样,Edit将不会被我的背景覆盖而导致重画。 <br/>{ <br/> ASSERT(FALSE); <br/> return ; <br/>} <br/>brush.CreateSolidBrush(m_clrBackgnd); <br/>pDC-&gt;FillRgn(&amp;rgn1,&amp;brush); <br/>brush.DeleteObject();</td></tr></tbody></table> 注意:在使用这个方法的时候要同时使用方法二。别忘了,到时候又说虫虫的办法不灵。  4、使用MemoryDC先在内存里把图画好,再复制到屏幕上 这对于一次画图过程很长的情况比较管用。毕竟内存操作比较快,而且复制到屏幕又是一次性的,至少不会出现可以明显看出一个东东从左画到右的情况。  <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>void CMyWin::OnPaint() <br/>{ <br/> CPaintDC dc1(this); // device context for painting <br/> dcMemory.CreateCompatibleDC(&amp;dc1); <br/> CBitmap bmp;//这里的Bitmap是必须的,否则当心弄出一个大黑块哦。 <br/> bmp.CreateCompatibleBitmap(&amp;dc1,rectClient.Width(),rectClient.Height()); <br/> dcMemory.SelectObject(&amp;bmp); <br/><br/> //接下来你想怎么画就怎么画吧。 <br/> //dcMemory.FillRect(rectClient,&amp;brush); <br/><br/> dc1.BitBlt(0,0,rectClient.Width(),rectClient.Height(),&amp;dcMemory,0,0,SRCCOPY); <br/> dcMemory.DeleteDC(); <br/> // Do not call CWnd::OnPaint() for painting messages <br/>}</td></tr></tbody></table> 争议 上述方法确实有效,但在有很多控件的情况下,计算一个窗口中需要擦除并重绘的“空白区域”是一件很麻烦的事情。为了方便这种方法的实际应用,我写了一组宏来完成”计算空白区域“的功能: <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>/*************************************/<br/>/************************************/<br/>/* 宏功能: 界面刷新时仅刷新指定控件以外的空白区域;可有效避免窗口闪烁<br/>/* 使用于: WM_ERASEBKGND 消息处理函数 OnEraseBkgnd(); <br/>/************************************/<br/>#define ERASE_BKGND_BEGIN \<br/>CRect bgRect;\<br/>GetWindowRect(&amp;bgRect);\<br/>CRgn bgRgn;\<br/>bgRgn.CreateRectRgnIndirect(bgRect);<br/>//#define ERASE_BKGND_BEGIN <br/>// Marco parameter 'IDC' specifies the identifier of the control <br/>#define ADD_NOERASE_CONTROL(IDC)\<br/>{\<br/> CRect controlRect;\<br/> GetDlgItem(IDC)-&gt;GetWindowRect(&amp;controlRect);\<br/> CRgn controlRgn;\<br/> controlRgn.CreateRectRgnIndirect(controlRect);\<br/> if(bgRgn.CombineRgn(&amp;bgRgn, &amp;controlRgn, RGN_XOR)==ERROR)\<br/>  return false;\<br/>}<br/><br/>// Marco parameter 'noEraseRect' specifies a screen coordinates based RECT, <br/>// which needn't erase.<br/>#define ADD_NOERASE_RECT(noEraseRect)\<br/>{\<br/> CRgn noEraseRgn;\<br/> noEraseRgn.CreateRectRgnIndirect(noEraseRect);\<br/> if(bgRgn.CombineRgn(&amp;bgRgn, &amp;noEraseRgn, RGN_XOR)==ERROR)\<br/>  return false;\<br/>}<br/><br/>// Marco parameter 'pDC' is a kind of (CDC *) type.<br/>// Marco parameter 'clBrushColor' specifies the color to brush the area.<br/>#define ERASE_BKGND_END(pDC, clBrushColor)\<br/>CBrush brush;\<br/>brush.CreateSolidBrush(clBrushColor);\<br/>CPoint saveOrg = (pDC)-&gt;GetWindowOrg();\<br/>(pDC)-&gt;SetWindowOrg(bgRect.TopLeft());\<br/>(pDC)-&gt;FillRgn(&amp;bgRgn, &amp;brush);\<br/>(pDC)-&gt;SetWindowOrg(saveOrg);\<br/>brush.DeleteObject();\<br/>//#define ERASE_BKGND_END<br/>/*************************/</td></tr></tbody></table> 说明: 1)宏 ERASE_BKGND_BEGIN 和 ERASE_BKGND_END(pDC, clBrushColor) 搭配使用。 2)宏 ADD_NOERASE_CONTROL(IDC) 和 ADD_NOERASE_RECT(noEraseRect) 根据需要放在上面两个宏的中间,用来添加不需要重绘背景的区域(正是这些区域导致了闪烁),使用次数不限。其中参数noEraseRect是一个屏幕坐标系的RECT类型或CRect类型。   使用举例1:   在当前窗体的类中重写WM_ERASEBKGND消息处理函数如下: <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>BOOL CMyWnd::OnEraseBkgnd(CDC* pDC) <br/>{<br/> ERASE_BKGND_BEGIN;<br/> ADD_NOERASE_RGN(IDC_BUTTON2);<br/> ADD_NOERASE_RGN(IDC_BUTTON1);<br/> ADD_NOERASE_RGN(IDC_LIST_STAT);<br/> ERASE_BKGND_END(pDC, GetSysColor(COLOR_3DFACE));<br/> return false;<br/>}</td></tr></tbody></table> 上面的IDC_BUTTON2,IDC_BUTTON1,IDC_LIST_STAT即窗体上的控件。 你可以指定其他已存在的控件。 这样,窗口在擦除背景时,将只对上述控件以后的”空白区域“使用系统色重绘,有效避免了闪烁。 备注: 重载WM_ERASEBKGND消息处理函数OnEraseBkgnd的方法,选择View->ClassWizard->classinfo选项卡:message filter下拉框: 选择window,然后再选择message maps选项卡,在messages下拉框应该可以找到wm_erasebkgnd.双击添加. 使用举例2:防止CListCtrl在拉动窗口时闪烁。 <table width="90%" align="center" bgcolor="#e7e9e9" border="1" style="color:rgb(68,68,68); font-size:16px; line-height:24px"><tbody><tr><td>/* * No further full-erasing is required, <br/>* to prevent screen <a target="_blank" href="http://flash.21tx.com/" style="color:rgb(61,107,167); text-decoration:none"><span style="color:#3366cc">Flash</span></a>ing caused by background erase and view repaint. <br/>* Only erase the blank area. <br/>*/<br/><br/>BOOL CExListCtrl::OnEraseBkgnd(CDC* pDC) {<br/> //compute the holding-data-items area of this list control CRect rect; <br/> CPoint dataRgnTopLeftPoint; <br/> CPoint dataRgnBottomRightPoint; <br/> GetItemPosition(0 , &amp;dataRgnTopLeftPoint); <br/> GetItemPosition(GetItemCount() , &amp;dataRgnBottomRightPoint); <br/> if(!GetHeaderCtrl()-&gt;GetItemRect(GetHeaderCtrl()-&gt;GetItemCount()-1, rect)) return <br/> CListCtrl::OnEraseBkgnd(pDC);<br/> dataRgnBottomRightPoint.x = rect.right;<br/> rect.SetRect(dataRgnTopLeftPoint, (CPoint)(dataRgnBottomRightPoint - CPoint(2,2)));<br/> ClientToScreen(dataRgnRect); <br/> //compute and erase the blank area. Using the Marco. ERASE_BKGND_BEGIN; <br/> ADD_NOERASE_RECT(dataRgnRect);<br/> ERASE_BKGND_END(pDC, GetBkColor());<br/> return false;<br/>}</td></tr></tbody></table> 说明:CListCtrl在拉动的时候,会前以背景色重刷背景,再在上面绘制有数据的Items, 而没有数据的区域则保持背景色。因此,如果在BOOL CExListCtrl::OnEraseBkgnd(CDC* pDC) 函数中简单的return false,那么没有数据的区域将显示不正常。 故举例2中先计算出有数据的items的区域,这是不需要以背景重刷的区域。 再使用本文的宏,就可以有效避免CListCtrl在拉动时候的闪烁。