Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

Controls from dialogbox disappearing?

Hi

In my MFC dialog based application, i am using a JPEG file as a background for my dialog box whose code i have given in the
OnEraseBkgnd() in the dialog class.

Also i have few static controls on my dialog box -- a static text, 2 buttons & a list control to be precise. In order to have the background of these controls as transparent i have written the neccessary code inside the OnCtlColor().

When the application is run, everything is perfect but as soon as i click on the column divider in the list control & drag it to increase or decrease column size, the buttons & the static text disapperars.

Now when i bring the mouse over the button it is displayed.

What cude be the possible reason for such behaviour.

Waiting for suggestions





0
davinder101
Asked:
davinder101
  • 8
  • 7
1 Solution
 
AlexFMCommented:
Please show your code: OnCtlColor and OnEraseBkgnd.
0
 
AlexFMCommented:
Code fragments from my project which does the same:

HBRUSH CMyDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
    HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);

    // I want all static controls to be transparent, except of this control (in the case you need this):
    if ( pWnd == &m_static1 )    
    {
        return hbr;
    }

    // make all other static controls transparent so that we will see background bitmap
    if ( nCtlColor == CTLCOLOR_STATIC )
    {
        pDC->SetBkMode(TRANSPARENT);
        return m_NullBrush;                          // this is null brush created by CreateStockObject(NULL_BRUSH);              
    }

    return hbr;
}

BOOL CMyDlg::OnEraseBkgnd(CDC* pDC)
{
    CRect rect;
    GetClientRect(&rect);

    CBrush* old_brush = (CBrush*) pDC->SelectObject(&m_BackgroundBrush);   // this is background brush
    pDC->Rectangle(&rect);
    pDC->SelectObject(old_brush);

    return TRUE;
}
0
 
davinder101Author Commented:
Please have a look at the code snippet:

HBRUSH CUnusedSpace::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
  HBRUSH hbr;

      if(nCtlColor == CTLCOLOR_STATIC)
      {
            pDC->SetBkMode(TRANSPARENT);
            pDC->SetTextColor(RGB(0, 0, 0));
            
            hbr = (HBRUSH)GetStockObject( NULL_BRUSH );
      }
      else
      {
            hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
      }
      
      return hbr;
}


//WM_ERASEBKND message is not available for this dialog. So i have to hard code it here.
BOOL CUnusedSpace::OnEraseBkgnd(CDC* pDC)
{
      BOOL rVal = FALSE;

      AddPicture(m_hWnd, ID_WIPE_SCREEN, 0, 0);//SEE Below for description of AddPicture().

      return rVal;
}

////////////////////////////////////////////////////////////////////////////////////////////////////

AddPicture() basically makes use of the <<IPicture>> interface to show the JPG image as dialog b ackground.
Functions like:
   CreateStreamOnHGlobal();
   OleLoadPicture();

are inturn called to load the jpg image.

Please suggest what wrong is happeinig. The static controls are disappearing.

Wai


 









0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
AlexFMCommented:
Return TRUE from OnEraseBkgnd.
0
 
davinder101Author Commented:
I have tried returning TRUE but the results are same.

I dont understand why when i run the application the buttons & the static text disapperars as soon as i click on the column divider in the list control & drag it to increase or decrease column size.

Waiting for suggestions

Regards

0
 
AlexFMCommented:
One more detail I see: you pass m_hWnd to AddPicture function instead of pDC. AddPicture must draw to pDC and not to create it's own DC.
0
 
davinder101Author Commented:
Add Picture is not creating its own dc. It is retrieving it from m_hWnd by using the function GetDC(m_hWnd).

I can send the entire project if reqd.

Waiting for suggestions

0
 
AlexFMCommented:
Yes, this is exactly what I mean - you shouln't use GetDC. Try to replace it with pDC or pDC->m_hDC from caller function. If this will not help, make this project available for download in zip archive, without output files and browsing information, and post link to it here.
0
 
davinder101Author Commented:
What i have just found is that when i click on the column divider & drags it to the left in order to decrease the column size,
the OnEraseBkgnd() is called the picture is again refreshed but OnCtlColor() is not called so the static controls gets hidden.

I think if i forcefully invoke the ObCtlColor(), the problem will be solved.

Waiting for ur suggestions

0
 
AlexFMCommented:
I still think that you need to replace HWND parameter to HDC - just to be sure that it is OK.
You cannot call OnCtrColor directly, but try to invalidate child controls instead.
0
 
davinder101Author Commented:
The link to download demo is:
        http://www.protostech.info/demo.zip

Waiting for ur suggestions
0
 
AlexFMCommented:
Well, the answer is in one of my previous posts:
you pass m_hWnd to AddPicture function instead of pDC. AddPicture must draw to pDC.

I made the following test:

BOOL CDemoDlg::OnEraseBkgnd(CDC* pDC)
{
    BOOL rVal = FALSE;      

    //AddPicture(m_hWnd, ID_SCREEN, 0, 0,pDC->m_hDC);

    CRect rect;
    GetClientRect(&rect);
    pDC->FillSolidRect(&rect, RGB(255, 0, 0));

    return TRUE;
}

OK, dialog is red and controls don't disappear. Other test:

BOOL CDemoDlg::OnEraseBkgnd(CDC* pDC)
{
    BOOL rVal = FALSE;      

    //AddPicture(m_hWnd, ID_SCREEN, 0, 0,pDC->m_hDC);

    CDC* pDC1 = GetDC();

    CRect rect;
    GetClientRect(&rect);
    pDC1->FillSolidRect(&rect, RGB(255, 0, 0));

    ReleaseDC(pDC1);

    return TRUE;
}

Now controls are erased. Change AddPicture first parameter from HWND to HDC and pass pDC->m_hWnd to it.
0
 
davinder101Author Commented:
I have tried to do the same in the loadpic.cpp class but failed as the dialog's background gets ttransparent & the jpg is not loaded as its background.

Can u suggest/make changes in the loadpic.cpp class as it will solve my problem.

Waiting for ur suggestions

0
 
davinder101Author Commented:
You can see that i have passed pDC->m_hWnd as the last argument in the AddPicture() & used it where ever GetDC() is used but the image is not loaded as the dlg back ground.
 
0
 
davinder101Author Commented:
Yes it is done.

Thax a lot

My Best Regards for u.

0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 8
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now