Search code examples
c++win32guipropertysheet

Positioning wizard at the centre of the screen win32 application using propertysheet


Created two pages wizard in win32 application using Propertysheetpage. The wizard is not positioned at the center of the screen. To position the pages at the center I have written the below code for the first page(assuming 2nd page will get reflected with the position of 1st page) but it is not working. Am I doing something wrong here?

static LRESULT WINAPI sWelcomePageDlgProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

  {

   HWND hwndOwner = NULL;
   RECT rcOwner,rcDlg,rc;

   switch (uMsg) 
    {
        case WM_INITDIALOG:
             hwndOwner = GetDesktopWindow();
             GetWindowRect(hwndOwner, &rcOwner);
             GetWindowRect(hwnd, &rcDlg);
             rc.left = (rcOwner.right - rcOwner.left)/2 - (rcDlg.right - rcDlg.left)/2;
             rc.right = rc.left + (rcDlg.right - rcDlg.left);
             rc.top = (rcOwner.bottom - rcOwner.top)/2 - (rcDlg.bottom - rcDlg.top)/2;
             rc.bottom = rc.top + (rcDlg.bottom - rcDlg.top);
             SetWindowPos (hwnd, NULL, rc.left, rc.top, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
             SetWindowText(GetDlgItem(hwnd, IDC_WELCOMETEXTSTATIC), Info);
             break;

       case WM_COMMAND:
            break;

       case WM_NOTIFY:
            LPNMHDR lpnm = (LPNMHDR)lParam;

            switch (lpnm->code)
            {
              case PSN_SETACTIVE:
                PropSheet_SetWizButtons(GetParent(hwnd), PSWIZB_NEXT | PSWIZB_CANCEL);
                break;
              case PSN_WIZNEXT:
                SetWindowLongPtr(hwnd, DWLP_MSGRESULT, IDD_FINISHPAGE);
                break;
            }

      break;
   }

return 0;

}


Solution

  • The hwnd passed into your window proc is a handle to a wizard page. For a handle to the wizard itself, call GetParent( hwnd ).

    Btw., screen dimensions are also available via GetSystemMetrics(), SM_CXSCREEN / SM_CYSCREEN.