Link to home
Start Free TrialLog in
Avatar of davinder101
davinder101

asked on

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





Avatar of AlexFM
AlexFM

Please show your code: OnCtlColor and OnEraseBkgnd.
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;
}
Avatar of davinder101

ASKER

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


 









Return TRUE from OnEraseBkgnd.
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

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.
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

ASKER CERTIFIED SOLUTION
Avatar of AlexFM
AlexFM

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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.
The link to download demo is:
        http://www.protostech.info/demo.zip

Waiting for ur suggestions
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.
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

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.
 
Yes it is done.

Thax a lot

My Best Regards for u.