Solved

Setting printer orientation to landscape

Posted on 1997-05-23
4
1,223 Views
Last Modified: 2013-11-19
I have an dialog-based application that creates a plot in a CFrameWnd object and a landscape orientation.  Most printers, however, are setup for portrait output by default.  My application can bring up the print dialog box, but unless the user manually changes the orientation to landscape, the plot doesn't display properly.  IF THE USER SELECTES LANDSCAPE THE PRINTOUT IS CORRECT.  I would like to set the orientation in the printer Properties dialog box to landscape automatically.

The CPrintDialog allows for various PD_xxx flags, but orientation is not one of them.  Orientation is buried in the DEVMODE structure which I cannot seem to "get to" until AFTER the dialog box returns.  I can then query dmOrientation, but setting it to 2 (landscape) has no effect on the printout.

Here is the portion of code that prints the plot:
void CXPlot::OnPlotFilePrint()  // CXPlot derived from CFrameWnd
// create a device context and a print dialog box, attach the two,
//  and pass the device context to the draw routine to "redraw" the plot
{
    CDC dc;                         // get a device context
    CPrintDialog dlg(FALSE);        // create a print dialog box
    if (dlg.DoModal() == IDOK)      // display the print dialog box
    {
        dc.Attach(dlg.GetPrinterDC());  // attach the printer dc
        DOCINFO di;
        ::ZeroMemory(&di, sizeof(DOCINFO));
        di.cbSize = sizeof(DOCINFO);
        di.lpszDocName = "My Plot";
        dc.StartDoc(&di);
        dc.StartPage();
        this->DrawSetupPrint(&dc);      // setup scaling
        this->Draw(&dc);                // draw the plot on the printer
        dc.EndPage();
        dc.EndDoc();
    }
}

I have tried calling the following code from various points in the above:
void CMyApp::SetPrinterOrientation(UINT orientation)
{
    PRINTDLG dlg;           // get a PRINTDLG structure
    DEVMODE FAR *pDevMode;  // pointer to a DEVMODE structure
    dlg.lStructSize = (DWORD)sizeof(PRINTDLG);
    if (this->GetPrinterDeviceDefaults(&dlg))       // a CWinApp function
    {
        if ((pDevMode = (DEVMODE FAR *)::GlobalLock(m_hDevMode)) != NULL)
        {
            pDevMode->dmOrientation = orientation;
            ::GlobalUnlock(m_hDevMode);
        }
    }
}

The displayed orientation is always Portrait.  There must be a way I can automatically set the orientation to Landscape, isn't there?

Thanks

Bill Stevener
0
Comment
Question by:stevener
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 150 total points
ID: 1301748
Yes - my Program forces Landscape orientation with the following code...

void CMyWinApp::SetLandscape() {
      // Get default printer settings.
      CPrintDialog dlg(FALSE);
      if (GetPrinterDeviceDefaults(&dlg.m_pd)) {
            // Lock memory handle.
            LPDEVMODE pDevMode = (LPDEVMODE)::GlobalLock(m_hDevMode);
            // Change printer settings in here.
            if (pDevMode) pDevMode->dmOrientation = DMORIENT_LANDSCAPE;
            // Unlock memory handle.
            ::GlobalUnlock(m_hDevMode);
      }
}

eMail me for formatted code at Roger_Onslow@compsys.com.au

0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1301749
BTW: do this at the start of (or before calling) your OnPlotFilePrint.  I call my routine in my InitInstance()

0
 

Author Comment

by:stevener
ID: 1301750
Roger,

Thanks for your response.

The routine you suggested, CMyWinApp::SetLandscape() is nearly identical to the routine I posted in my original question, CMyApp::SetPrinterOrientation(UINT orientation).  Unfortunately, neither work as desired.

As I step through each statement with the debugger, I can verify that I am getting a valid DEVMODE structure, and that dmorientation is being set to DMORIENT_LANDSCAPE (2), and that it retains the value for the life of the application (when I invoke OnPlotFilePrint several times in the same run of the application, dmorientation is 1 the 1st time, 2 each succeeding time).  When the print dialog box appears and I select the "Properties" button, the orientation is always displayed as "Portrait", regardless of the value for dmorientation in the DEVMODE structure.  If I bypass the "Properties" button and select OK, the printout is in portrait mode.  If I select Landscape from the Properties page, the printout is landscape.

Is there anything else I need to do after setting the orientation to Landscape?  SetPrinterDeviceDefaults()?

BTW, I have tried this when the plot window is created, even before anything is drawn into it, and at the beginning of my OnPlotFilePrint routine.

Bill Stevener
0
 

Author Comment

by:stevener
ID: 1301751
Although he didn't solve the problem, he was quick and on the right track.  Someone from one of the newsgroups DID solve the problem by pointing out that in my OnPlotFilePrint code, I needed to add:
    AfxGetApp()->GetPrinterDeviceDefaults(&dlg.m_pd)
between setting the orientation and the call to DoModal().  Adding this line solved the problem.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
conditional code and condition difference 9 87
fizzArray2 challenge 1 112
Thin secure Windows 10 5 116
Work with App store 7 123
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question