views:

618

answers:

4

I'm doing some small changes to C++ MFC project. I'm .NET developer so Windows programming is new to me.

I need to launch some method right after CDialog is completely shown (painted) for the first time, but only once.

How can I do this? In .NET I would handle Form.Shown event.

Do I need to handle some message? Which? Do I need to override some CDialog method? Or is there no easy way? I'm thinking of handling WM_ACTIVATE and then using a flag to ensure I call another method only once.

+1  A: 

Found the answer here: Waiting until the dialog box is displayed before doing something

Short story:
INT_PTR CALLBACK
DlgProc(HWND hwnd, UINT uiMsg, WPARAM wParam, LPARAM lParam)
{
  switch (uiMsg) {

  case WM_INITDIALOG:
    return TRUE;

  case WM_WINDOWPOSCHANGED:
    if ((((WINDOWPOS*)lParam)->flags & SWP_SHOWWINDOW) &&
        !g_fShown) {
      g_fShown = TRUE;
      PostMessage(hwnd, WM_APP, 0, 0);
    }
    break;


  case WM_APP:
      MessageBox(hwnd,
                 IsWindowVisible(hwnd) ? TEXT("Visible")
                                       : TEXT("Not Visible"),
                 TEXT("Title"), MB_OK);
      break;

  case WM_CLOSE:
   EndDialog(hwnd, 0);
   break;
  }

  return FALSE;
}

If you're using MFC like I am you'll need to map WM_WINDOWPOSCHANGED and then use ON_MESSAGE to map WM_APP. See this CodeProject article for more details.

Ornus
A: 

Another approach I've used a number of times with great success is to use a timer. Set it for 10m0s. It'll only fire after the dialog is shown.

Joel Lucsy
Timer might not fire in certain conditions. Article I'm refering to (http://blogs.msdn.com/oldnewthing/archive/2006/09/25/770536.aspx) covers that.
Ornus
A: 

Joel,

Timer is not guaranteed to fire after painting. WM_PAINT message has the lowest priority. If for some unusual reason, the 10ms elapsed before the WM_PAINT message had a chance to be processed (e.g. long processing in WM_INITDIALOG), OnTimer() will fire before OnPaint().

Serge - appTranslator
A: 

Hell put the code in OnPaint() and thow a bool m_fullyInitilized in your class. I like the timer too.. Though I usually go with 100ms. I also move all my initilization code out of the oninit, in these cases.. Just to protect against too much processing in there.

baash05