How to stop flickering of controls in my Dialog window
-
In my MFC application, I am doing a custom drawing in my dialog class OnPaint(). I have also created a few controls in dialog template. I have also resized and repositioned some of the controls in the dialog code. When I run my application, the controls are flickering. Please suggest me how to avoid this? BOOL CFP::OnInitDialog() { CDC *pDC = GetDC(); //Get DC for Dialog's Client Area pDCTmp->CreateCompatibleDC(pDC); //Create Compatible DC(pDCTmp) for Dialog's Client Area(pDC) //pDCTmp is a member Variable pBgBitmap->CreateCompatibleBitmap (pDC, 800, 640 ); //Create Compatible Bitmap(pBgBitmap) for Dialog's Client Area(pDC) //pBgBitmap->LoadBitmap(IDB_FORESTIMAGE); pDCTmp->SelectObject(pBgBitmap); ReleaseDC(pDC); } void CFacePlate::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; rect = rScreen; //rScreen is Client rect pDCTmp->FillRect(rect,&CBrush(RGB(152, 185, 192))); //pDCTmp member variable is used to draw . . . . //Custom drawings using the pDCTmp }
-
In my MFC application, I am doing a custom drawing in my dialog class OnPaint(). I have also created a few controls in dialog template. I have also resized and repositioned some of the controls in the dialog code. When I run my application, the controls are flickering. Please suggest me how to avoid this? BOOL CFP::OnInitDialog() { CDC *pDC = GetDC(); //Get DC for Dialog's Client Area pDCTmp->CreateCompatibleDC(pDC); //Create Compatible DC(pDCTmp) for Dialog's Client Area(pDC) //pDCTmp is a member Variable pBgBitmap->CreateCompatibleBitmap (pDC, 800, 640 ); //Create Compatible Bitmap(pBgBitmap) for Dialog's Client Area(pDC) //pBgBitmap->LoadBitmap(IDB_FORESTIMAGE); pDCTmp->SelectObject(pBgBitmap); ReleaseDC(pDC); } void CFacePlate::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; rect = rScreen; //rScreen is Client rect pDCTmp->FillRect(rect,&CBrush(RGB(152, 185, 192))); //pDCTmp member variable is used to draw . . . . //Custom drawings using the pDCTmp }
I see nothing in your code snippet that causes flickering. How did you implement the "Custom drawings"? When does the flickering happen? While resizing only? Which controls flicker?
-
I see nothing in your code snippet that causes flickering. How did you implement the "Custom drawings"? When does the flickering happen? While resizing only? Which controls flicker?
A seperate thread function created already which calls a function UpdateAllViews() as shown below every half a second which in turn calls Invalidate the CFP Dialog. This is done to continuously update some real time numerical values on the dialog. When I remove this line pFPlate->Invalidate(FALSE); then there is no flickering on the dialog. But the values are not updated. How to avoid this flickering without affecting the value updation. void tUpdateAllViews( CMainFrame *pMainFrm ) { while( 1 ) { pMainFrm->UpdateAllViews(); Sleep(500); } } void CMainFrame::UpdateAllViews() { if(pFPlate->GetSafeHwnd()) //CFP Dialog member variable { if(pFPlate->IsWindowVisible()) { pFPlate->Invalidate(FALSE); } } }
-
In my MFC application, I am doing a custom drawing in my dialog class OnPaint(). I have also created a few controls in dialog template. I have also resized and repositioned some of the controls in the dialog code. When I run my application, the controls are flickering. Please suggest me how to avoid this? BOOL CFP::OnInitDialog() { CDC *pDC = GetDC(); //Get DC for Dialog's Client Area pDCTmp->CreateCompatibleDC(pDC); //Create Compatible DC(pDCTmp) for Dialog's Client Area(pDC) //pDCTmp is a member Variable pBgBitmap->CreateCompatibleBitmap (pDC, 800, 640 ); //Create Compatible Bitmap(pBgBitmap) for Dialog's Client Area(pDC) //pBgBitmap->LoadBitmap(IDB_FORESTIMAGE); pDCTmp->SelectObject(pBgBitmap); ReleaseDC(pDC); } void CFacePlate::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; rect = rScreen; //rScreen is Client rect pDCTmp->FillRect(rect,&CBrush(RGB(152, 185, 192))); //pDCTmp member variable is used to draw . . . . //Custom drawings using the pDCTmp }
-
I'm a little confused by some of your code but generally I think you will have to override WM_ERASEBKGND so that the background is not erased by the default processing. Do not call the base class; just return TRUE.
Yes. done that. But still its flickering. It seems the problem is in the thread which calls the Invalidate for the Dialog. Please help to find out what is wrong or how to correct the problem
-
Yes. done that. But still its flickering. It seems the problem is in the thread which calls the Invalidate for the Dialog. Please help to find out what is wrong or how to correct the problem
But why do you call Invalidate if you haven't changed anything in the View?
-
Yes. done that. But still its flickering. It seems the problem is in the thread which calls the Invalidate for the Dialog. Please help to find out what is wrong or how to correct the problem
1. Do you have to call Invalidate() from a thread? Could you use a timer in the dialog and only update what is needed instead of invalidating the whole dialog? 2. Are your dialog controls embedded in a TabControl/TabPage? If so you will have to create a custom TabControl and TabPage to override the OnEraseBackground() in those and paint the background in a temp dc as well. 3. I'm not sure about this, but you may have to set the dialog style WS_CLIBCHILDREN to include them in the dialogs clip region. Look at definition in msdn.
-
Yes. done that. But still its flickering. It seems the problem is in the thread which calls the Invalidate for the Dialog. Please help to find out what is wrong or how to correct the problem
-
In my MFC application, I am doing a custom drawing in my dialog class OnPaint(). I have also created a few controls in dialog template. I have also resized and repositioned some of the controls in the dialog code. When I run my application, the controls are flickering. Please suggest me how to avoid this? BOOL CFP::OnInitDialog() { CDC *pDC = GetDC(); //Get DC for Dialog's Client Area pDCTmp->CreateCompatibleDC(pDC); //Create Compatible DC(pDCTmp) for Dialog's Client Area(pDC) //pDCTmp is a member Variable pBgBitmap->CreateCompatibleBitmap (pDC, 800, 640 ); //Create Compatible Bitmap(pBgBitmap) for Dialog's Client Area(pDC) //pBgBitmap->LoadBitmap(IDB_FORESTIMAGE); pDCTmp->SelectObject(pBgBitmap); ReleaseDC(pDC); } void CFacePlate::OnPaint() { CPaintDC dc(this); // device context for painting // TODO: Add your message handler code here CRect rect; rect = rScreen; //rScreen is Client rect pDCTmp->FillRect(rect,&CBrush(RGB(152, 185, 192))); //pDCTmp member variable is used to draw . . . . //Custom drawings using the pDCTmp }
I am not a real fan of MFC but I can tell you what windows API is doing. Dialog backgrounds paint by WM_CTLCOLORDLG, WM_CTLCOLORSTATIC it used to be WM_CTLCOLOR and I suspect MFC still uses WM_CTLCOLOR because it has a couple of bugs. Usually what you want to do is return a NULL_BRUSH with GetStockObject(NULL_BRUSH); So this one if you paint the entire dialog then return a NULL_BRUSH WM_CTLCOLORDLG message - Windows applications | Microsoft Docs[^] MFC may use this one and again return a NULL_BRUSH WM_CTLCOLOR message - Windows applications | Microsoft Docs[^] That means it doesn't paint the background before it sends you an WM_PAINT :-) If you want/need to transparent overlay the bitmap you have to use a transparent colour to stop flashing
void CDemoDialog::OnPaint()
{
CPaintDC dc(this); // device context for painting// transfer the bitmap into paint DC using a transparent color
dc.TransparentBlt(
10, 10, bmp.bmWidth, bmp.bmHeight, // destination coordinates and sizes
&pDCTmp, // source DC .. your DC with bitmap
0, 0, bmp.bmWidth, bmp.bmHeight, // source coordinates and sizes
RGB(255, 0, 0)); // transparent color}
In vino veritas