• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 283
  • Last Modified:

Using a CCLient DC class to Grab the Dialog Box device context

     CClientDC dlgDC(this);
      
      for(int x=0;x<300;x++)
      {
            for(int y=0;y<300;y++)
            {
                  
                  dlgDC.SetPixel(x,y,COLORREF(RGB(100,120,130)));
            }
      }

If I place this in the OnInitDialog() of a child dialog nothing happens.
If I put the code in a OnButton function in the child dialog class it works..
How can I get it to work through the OnInitDialog()?

Thanks

Oliver
0
oliverUK
Asked:
oliverUK
  • 5
  • 2
2 Solutions
 
AlexFMCommented:
OnInitDialog is executed before dialog is shown.
If you want to execute some code immidiately after dialog is shown, you can use one of the following options:

1) Post user-defined message from OnInitDialog to dialog itself and execute code in the message handler
2) Set timer in OnIntiDialog to some short time (like 20 ms) and execute code in OnTimer function. Don't forget to kill timer after this.
0
 
oliverUKAuthor Commented:
Can you post an example of the first option please.
0
 
AlexFMCommented:
// TestDlg.h : header file

class CTestDlg : public CDialog
{
    ...

    // Generated message map functions
    //{{AFX_MSG(CTestDlg)
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    //}}AFX_MSG
    LRESULT OnMessageMyImage(WPARAM wParam, LPARAM lParam);     // add this line
    DECLARE_MESSAGE_MAP()
};

// TestDlg.cpp : implementation file

#define WM_USER_MYIMAGE WM_APP + 1    // add this line

BEGIN_MESSAGE_MAP(CTestDlg, CDialog)
    //{{AFX_MSG_MAP(CTestDlg)
    ON_WM_SYSCOMMAND()
    ON_WM_PAINT()
    ON_WM_QUERYDRAGICON()
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_USER_MYIMAGE, OnMessageMyImage)    // add this line
END_MESSAGE_MAP()

BOOL CTestDlg::OnInitDialog()
{
    ...

    PostMessage(WM_USER_MYIMAGE, 0, 0);     // add this line

    return TRUE;  // return TRUE  unless you set the focus to a control
}

LRESULT CTestDlg::OnMessageMyImage(WPARAM wParam, LPARAM lParam)
{
    MessageBox(_T("Hi"));

    return 0;
}

PostMessage is executed asynchronously, and you see message box when dialog is shown. Replace PostMessage with SendMessage - and message box appears before dialog is shown (just for information).
BTW, this way is much better than timer. I am using timer sometimes in such situations because this requires less typing, and I am lazy.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
oliverUKAuthor Commented:
Thanks but when I implemented the code I saw the MessageBox always before the dialog is shown.....
0
 
AlexFMCommented:
Please show your code.
0
 
AlexFMCommented:
Another way:

void CTestDlg::OnPaint()
{
    // code generated by Wizard
    ///

    // add this to the end of function:
    static BOOL b = FALSE;

    if ( ! b )
    {
        MessageBox(_T("Hi"));
        b = TRUE;
    }
}
0
 
_mb_Commented:
I recommend to you to put your drawing routines in the WM_PAINT message handler (OnPaint) or use the WM_WM_ERASEBKGND handler (OnEraseBkgnd) to colorize the dialog. Otherwise you lose your drawings, if the dialog must again be drawn (for example, if it was covered by another window).
Therefore it does not work in OnInitDialog. Drawings from here are overwritten by the standard painting routines.
0
 
AlexFMCommented:
Actually, all my recommendations are valid for any code except drawing.
Follow _mb_'s advice and move this code ot OnPaint or OnEraseBkgnd function.
BTW, this code may be effectively replaced with single call to FillSolidRect function.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now