Search code examples
c++visual-studiowinapiprintingwindows-ce

Problem with CreateDC of printer after call PageSetupDlg() function


I am working with project in C++ WinAPI for WindowsCE 2013. IDE which I use is Visual Studio 2013 I firstly initialize a PageSetupDialog in my application.. Screen below: enter image description here

Unfortunately I still have problem with start printing. I have assumption that problem is in CreateDC that the values from DialogBox are not forwarded to CreateDC function. I read in that documentation that PageSetupDlg function return DEVMODE structure, but I have no idea how to forward that to Create DC function. Thanks for Your help in this matter. I put definition of responsible function:

BOOL OknoDrukowania(HWND hWnd)
{
    PAGESETUPDLG psd;   
    DEVMODE dm;// common dialog box structure
    // Initialize PAGESETUPDLG
    ZeroMemory(&psd, sizeof(psd));
    psd.lStructSize = sizeof(psd);
    psd.hwndOwner = hWnd;
    psd.hDevMode = NULL; // Don't forget to free or store hDevMode.
    psd.hDevNames = NULL; // Don't forget to free or store hDevNames.
    psd.Flags = PSD_INTHOUSANDTHSOFINCHES | PSD_MARGINS ;
    psd.rtMargin.top = 1000;
    psd.rtMargin.left = 1250;
    psd.rtMargin.right = 1250;
    psd.rtMargin.bottom = 1000;
    //psd.lpfnPagePaintHook = PaintHook;
 
    if (PageSetupDlg(&psd) == TRUE)
    {
        HDC hDC;
        hDC = CreateDC(NULL, dm.dmDeviceName, NULL, NULL);
        StartDoc(hDC, NULL);
        StartPage(hDC);
        Ellipse(hDC, 500, 500, 1000, 1000);
        Ellipse(hDC, 1000, 800, 1500, 1300);
        Ellipse(hDC, 800, 1000, 1800, 2000);
        EndPage(hDC);
        EndDoc(hDC);
        DeleteDC(hDC);  // check paper size and margin values here.
    }
    return TRUE;
 
}

Solution

  • Problem was solved by this fragment of code:

    memcpy(&dm, (DEVMODE *)(psd.hDevMode), sizeof(DEVMODE));
            lstrcpy(DriverName, ((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wDriverOffset)));
            lstrcpy(DeviceName, ((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wDeviceOffset)));
            lstrcpy(OutputName, ((TCHAR *)((BYTE *)psd.hDevNames + ((DEVNAMES *)psd.hDevNames)->wOutputOffset)));
            HDC hDC;
            hDC = CreateDC(DriverName, DeviceName, OutputName, &dm);