Link to home
Start Free TrialLog in
Avatar of moosach
moosach

asked on

How to get the IE Window to set Landscape Printing ?

Hi everybody,

I want to set the Landscape or portrait mode of the printer with printing
over IE4 or 5. It doesnt work as in MSDN descripted.
Or do I use the wrong window?
I tryed to use the GetHwnd() from the IE4, but I always get
an exception. So I used the wnd of the following class:

 <<webbrowser2.cpp>>  <<webbrowser2.h>>
see below ...

But again no luck. How I can get the right hwnd to set the paper orientation ?

Any help is appreciated ...

Thanks

Toni

......................


      SetLandscapeDevMode(wnd->m_hWnd, printermode);
      lpDispatch = ((CWebBrowser2 *)wnd)->GetDocument();
            
      if (lpDispatch != NULL)      {
            lpDispatch->QueryInterface(IID_IOleCommandTarget,
(void**)&lpOleCommandTarget);
            lpDispatch->Release();
            // print contents of web browser control
            lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT,
OLECMDEXECOPT_DONTPROMPTUSER,´NULL,NULL);
            lpOleCommandTarget->Release();
      }


LPDEVMODE SetLandscapeDevMode(HWND hWnd, int mode)
{
   HANDLE      hPrinter;
   LPDEVMODE   pDevMode;
   DWORD       dwNeeded, dwRet;
   char pDevice[256];

   try
   {
            CPrintDialog *printDlg =
                  new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC,
NULL);
 
            // Dont Display Windows print dialog box.
            printDlg->GetDefaults();
            CString prnName = printDlg->GetDeviceName();
            strcpy(      pDevice, prnName );
            prnName.ReleaseBuffer();
            delete printDlg;

         /* Start by opening the printer */
         if (!OpenPrinter(pDevice, &hPrinter, NULL))
               return NULL;

         /*
            * Step 1:
            * Allocate a buffer of the correct size.
            */
         dwNeeded = DocumentProperties(hWnd,
               hPrinter,       /* handle to our printer */
               pDevice,        /* Name of the printer */
               NULL,           /* Asking for size so */
               NULL,           /* these are not used. */
               0);             /* Zero returns buffer size. */
         pDevMode = (LPDEVMODE)malloc(dwNeeded);

         /*
            * Step 2:
            * Get the default DevMode for the printer and
            * modify it for our needs.
            */
         dwRet = DocumentProperties(hWnd,
               hPrinter,
               pDevice,
               pDevMode,       /* The address of the buffer to fill. */
               NULL,           /* Not using the input buffer. */
               DM_OUT_BUFFER); /* Have the output buffer filled. */
         if (dwRet != IDOK)
         {
               /* if failure, cleanup and return failure */
               free(pDevMode);
               ClosePrinter(hPrinter);
               return NULL;
         }

         /*
            * Make changes to the DevMode which are supported.
            */
         if (pDevMode->dmFields & DM_ORIENTATION)
         {
                  /* if the printer supports paper orientation, set
it*/
                  if (mode == 1)
                        pDevMode->dmOrientation =
DMORIENT_LANDSCAPE;
                  else
                        pDevMode->dmOrientation = DMORIENT_PORTRAIT;
                  
         }

      //   if (pDevMode->dmFields & DM_DUPLEX)
      //   {
               /* if it supports duplex printing, use it */

      //       pDevMode->dmDuplex = DMDUP_HORIZONTAL;
      //   }

         /*
            * Step 3:
            * Merge the new settings with the old.
            * This gives the driver a chance to update any private
            * portions of the DevMode structure.
            */
         dwRet = DocumentProperties(hWnd,
               hPrinter,
               pDevice,
               pDevMode,       /* Reuse our buffer for output. */
               pDevMode,       /* Pass the driver our changes. */
               DM_IN_BUFFER |  /* Commands to Merge our changes and */
               DM_OUT_BUFFER); /* write the result. */

         /* Done with the printer */
         ClosePrinter(hPrinter);

         if (dwRet != IDOK)
         {
               /* if failure, cleanup and return failure */
               free(pDevMode);
               return NULL;
         }
   }
   catch(...)
   {
   }

   /* return the modified DevMode structure */
   return pDevMode;

}
Avatar of wylliker
wylliker

Changing the Page Setup is difficult in IE 4 and 5.  Under 3 you could change it with client side script.

I have come across an ActiveX component that does what you want and there is a corresponding article on MSDN.

This Activex control will launch the Page Setup dialog in a hidden window and manipulate the various values - WITHOUT - changing the end users current settings.  This means that you can set up landscape/portrait on a per page basis with just a few lines of client-side script and this control.

I am not affiliated with this company but found their product (free for simple print settings you pay for advanced) would solve our need to turn off and/or change the header and footer settings on a per page basis.

The control is here - there is a working DEMO on their site:

http://www.meadroid.com/scriptx/

The related MSDN article is here:

http://msdn.microsoft.com/workshop/author/script/dhtmlprint.asp



Avatar of moosach

ASKER

Hi,

pretty good answer ! That will help !
How to give you the points now :-) ?

CU

Toni
I will re-submit my comment above as an answer.  You need only accept and grade it.

With a Rich Edit Control you need to use the EM_EXSETSEL message.

Set up a CHARRANGE structure

CHARRANGE SelRange;
SelRange.cpMin = 0;
SelRange.cpMax = 0;

SendMessage(hwndRichEdit, EM_EXSETSEL, 0, (LPARAM) &SelRange);


Give it a try and let me know
Sorry about that - posted a comment to a different question as an answer.

Please reject this and I will re-submit my answer properly.

Again, sorry.
Avatar of moosach

ASKER

No Problem :-) !!
ASKER CERTIFIED SOLUTION
Avatar of wylliker
wylliker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial