user9778277
user9778277

Reputation: 163

How do I add a property page to CPrintDialogEx

I'm trying to implement CPrintDialogEx. I have some additional needed options and I want to add another property page to the window. There are no MFC examples and trying the Win API example fails miserably. It cashes. What am I doing wrong?

CPrintDialogEx dlg;
PROPSHEETPAGE optionsPage1;
HPROPSHEETPAGE hOptionsPage;
memset(&optionsPage1, 0, sizeof(PROPSHEETPAGE));
optionsPage1.dwSize = sizeof(PROPSHEETPAGE);
optionsPage1.dwFlags = PSP_DLGINDIRECT;
optionsPage1.hInstance = AfxGetInstanceHandle();
optionsPage1.pszTemplate = MAKEINTRESOURCE(IDD_QREPORT_OPTIONS);
optionsPage1.pResource = (DLGTEMPLATE*)IDD_QREPORT_OPTIONS;
optionsPage1.hIcon = NULL;
optionsPage1.pszIcon = NULL;
optionsPage1.pszTitle = "Options";
optionsPage1.pfnDlgProc = AfxWndProc; 
optionsPage1.lParam = NULL;
dlg.m_pdex.nPropertyPages = 1;
hOptionsPage = CreatePropertySheetPage(&optionsPage1);
dlg.m_pdex.lphPropertyPages = &hOptionsPage;
if (dlg.DoModal() == IDOK)

NULL pointer crash

Upvotes: 1

Views: 226

Answers (1)

Barmak Shemirani
Barmak Shemirani

Reputation: 31599

optionsPage1.dwFlags = PSP_DLGINDIRECT;
...
optionsPage1.pResource = (DLGTEMPLATE*)IDD_QREPORT_OPTIONS;

IDD_QREPORT_OPTIONS is an integer, it should not be forced to cast in to DLGTEMPLATE*. Doing so will point pResource to some random memory address and is likely the cause of crash.

You don't need pResource anyway. Replace PSP_DLGINDIRECT with PSP_DEFAULT, this will instruct CreatePropertySheetPage to use pszTemplate.


PROPSHEETPAGE documentation:

pszTemplate

Type: LPCSTR

Dialog box template to use to create the page. This member can specify either the resource identifier of the template or the address of a string that specifies the name of the template. If the PSP_DLGINDIRECT flag in the dwFlags member is set, pszTemplate is ignored. This member is declared as a union with pResource.

Example:

INT_PTR CALLBACK dlgproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch(uMsg)
    {
    case WM_COMMAND:
        if(LOWORD(wParam) == IDC_BUTTON1)
            MessageBox(hwnd, _T("test"), 0, 0);
        return 0;
    }
    return FALSE;
}

PROPSHEETPAGE optionsPage1 = { 0 };
optionsPage1.dwSize = sizeof(PROPSHEETPAGE);
optionsPage1.dwFlags = PSP_DEFAULT | PSP_USETITLE;
optionsPage1.hInstance = AfxGetInstanceHandle();
optionsPage1.pszTemplate = MAKEINTRESOURCE(IDD_QREPORT_OPTIONS);
optionsPage1.pszTitle = _T("Options");
optionsPage1.pfnDlgProc = dlgproc;// AfxWndProc;

Upvotes: 3

Related Questions