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;

}
LVL 1
moosachAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wyllikerCommented:
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



0
moosachAuthor Commented:
Hi,

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

CU

Toni
0
wyllikerCommented:
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
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

wyllikerCommented:
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.
0
moosachAuthor Commented:
No Problem :-) !!
0
wyllikerCommented:
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 

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.