MFC - execute code right after dialog is shown (.NET equivalent of Form.Shown)

寵の児 提交于 2019-12-12 12:04:58

问题


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:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/269462/mfc-execute-code-right-after-dialog-is-shown-net-equivalent-of-form-shown

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!