在WTL或MFC的程序中使用了按钮控件,
在触发点击事件向按钮上面赋值时总是会出现闪烁的情况;
GetDlgItem(IDC_BUTTON_TEST).SetWindowText((LPCTSTR)strTest);
GetDlgItem(IDC_BUTTON_TEST).Invalidate();
Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method. When this method is called with no parameters, the entire client area is added to the update region.
查询后得知, 此 Invalidate()
函数是 “使整个窗口客户区无效, 并进行更新显示的函数”,
调用Invalidate等函数后窗口不会立即重绘,这是由于WM_PAINT消息的优先级很低,它需要等消息队列中的其它消息发送完后才能被处理。
所以,调用该函数后,对应的窗口控件无效;并且在下一次进行重绘;
我们希望它能尽快重绘,以致于不在过长时间后进行闪烁;
所以,可以在无效化窗口区域后利用RedrawWindow()
进行立即重绘,解决闪烁问题;
这样的话,代码可以:
GetDlgItem(IDC_BUTTON_TEST).SetWindowText((LPCTSTR)strTest);
GetDlgItem(IDC_BUTTON_TEST).Invalidate();
RedrawControl(GetDlgItem(IDC_BUTTON_TEST));
这样,可以有效解决按钮闪烁问题;