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++)

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()?


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.
oliverUKAuthor Commented:
Can you post an example of the first option please.
// TestDlg.h : header file

class CTestDlg : public CDialog

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

// TestDlg.cpp : implementation file

#define WM_USER_MYIMAGE WM_APP + 1    // add this line

    ON_MESSAGE(WM_USER_MYIMAGE, OnMessageMyImage)    // add this line

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)

    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.
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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

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

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

    if ( ! b )
        b = TRUE;
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.