Link to home
Start Free TrialLog in
Avatar of oliverUK
oliverUK

asked on

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
Avatar of AlexFM
AlexFM

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.
Avatar of oliverUK

ASKER

Can you post an example of the first option please.
// 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.
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 )
    {
        MessageBox(_T("Hi"));
        b = TRUE;
    }
}
ASKER CERTIFIED SOLUTION
Avatar of _mb_
_mb_

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