4

I am using a MFC wizard with CPropertyPages. Is there any way of calling a function after the page is shown? At the moment the function starts when I hit the "Next"-button of the previous page.

I tried to call the function from OnShowWindow, OnCreate, OnSetActive, DoModal, but none of them worked.

Thanks for your help!

Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
Dave Chandler
  • 651
  • 6
  • 17

1 Answers1

2

Usually it's enough to override OnSetActive(). However, this method is called before the CPropertyPage is made visible and focused. If you have to perform a task after the page is shown, you have to post your own message in OnSetActive:

// This message will be received after CMyPropertyPage is shown
#define WM_SHOWPAGE WM_APP+2

BOOL CMyPropertyPage::OnSetActive() {
    if(CPropertyPage::OnSetActive()) {
        PostMessage(WM_SHOWPAGE, 0, 0L); // post the message
        return TRUE;
    }
    return FALSE;
}

LRESULT CMyPropertyPage::OnShowPage(UINT wParam, LONG lParam) {
    MessageBox(TEXT("Page is visible. [TODO: your code]"));
    return 0L;
}

BEGIN_MESSAGE_MAP(CMyPropertyPage,CPropertyPage)
    ON_MESSAGE(WM_SHOWPAGE, OnShowPage) // add message handler
    // ...
END_MESSAGE_MAP()
Anonymous Coward
  • 6,186
  • 1
  • 23
  • 29
  • This is strange, when I replace the message box with my code, the page doesn't appear. The msgbox seems to make the page visible. – Dave Chandler Nov 02 '12 at 13:29
  • 1
    You could try to call `UpdateWindow();` before or after your code. If that doesn't work i would have to know more about what the code in your OnShowPage() does. Maybe it conflicts with the PropertyPage. – Anonymous Coward Nov 02 '12 at 13:44
  • Yay, UpdateWindow did the trick! Thanks a lot! My wizard deletes a directory, after clicking on "Next". But before deleting, I print something like "Deleting process started" in a ListBox. But the window always "froze" a few seconds when I clicked on "Next" (because the deleting took a few seconds) and after the process, the ListBox was shown, which was pretty useless. Now the first entry of the ListBox is shown, _then_ the directory gets deleted. So the user knows what is happening. – Dave Chandler Nov 02 '12 at 14:05
  • @DaveChandler: why not actually handle the deletion of the directory in a thread, which, once done, posts a message to the HWND (careful: to an HWND and *not* to an MFC CWnd - you can't pass MFC objects around across threads) that causes it to continue? – Nik Bougalis Nov 05 '12 at 06:23
  • Nevertheless I had to start the thread _after_ the dialog is shown. I want the user to see the process of deletion in real-time. – Dave Chandler Nov 05 '12 at 13:33