Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to stop flickering of controls in my Dialog window

How to stop flickering of controls in my Dialog window

Scheduled Pinned Locked Moved C / C++ / MFC
graphicsc++tutorialquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    manoharbalu
    wrote on last edited by
    #1

    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 }

    V M L 3 Replies Last reply
    0
    • M manoharbalu

      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 }

      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      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?

      M 1 Reply Last reply
      0
      • V Victor Nijegorodov

        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?

        M Offline
        M Offline
        manoharbalu
        wrote on last edited by
        #3

        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); } } }

        1 Reply Last reply
        0
        • M manoharbalu

          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 }

          M Offline
          M Offline
          mo1492
          wrote on last edited by
          #4

          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.

          M 1 Reply Last reply
          0
          • M mo1492

            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.

            M Offline
            M Offline
            manoharbalu
            wrote on last edited by
            #5

            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

            V M 3 Replies Last reply
            0
            • M manoharbalu

              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

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #6

              But why do you call Invalidate if you haven't changed anything in the View?

              1 Reply Last reply
              0
              • M manoharbalu

                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

                M Offline
                M Offline
                mo1492
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • M manoharbalu

                  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

                  M Offline
                  M Offline
                  mo1492
                  wrote on last edited by
                  #8

                  Additionally, I don't know what pFPlate is but if it's a pointer to the dialog, could you create a function in the dialog to update what you need instead of calling Invalidate().

                  1 Reply Last reply
                  0
                  • M manoharbalu

                    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 }

                    L Offline
                    L Offline
                    leon de boer
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups