Solved

Print screen using win32

Posted on 2008-10-23
2
539 Views
Last Modified: 2013-12-04
Hello,
I have to print a page to pdf file format i am able to do it but the output is coming like when we do print screen manually i dont want the whole screen normally if we give print onlt text comes not menu bars and title bars i want it like that only.I dont know where  to change.I think i should make changes in GetSystemMetrics () function but what i should give i dont know.
void PrintScreen (HWND hWnd)
{
	HDC hScreenDC;
	if ((hScreenDC = GetDC (hWnd)) != NULL) {
		HDC hMemDC;
		if ((hMemDC = CreateCompatibleDC (hScreenDC))!= NULL) {
			HBITMAP hFullScreenBitmap;
 
			int iScreenWidth  = GetSystemMetrics (SM_CXFULLSCREEN);		   
			int iScreenHeight = GetSystemMetrics (SM_CYFULLSCREEN);		    			
			if ((hFullScreenBitmap = CreateCompatibleBitmap (hScreenDC, iScreenWidth, iScreenHeight)) != NULL) {
				HBITMAP hDefaultBitmap = (HBITMAP) SelectObject (hMemDC, hFullScreenBitmap);
 
				// From screen to memory:
				BitBlt (hMemDC, 0, 0, iScreenWidth, iScreenHeight, hScreenDC, 0, 0, SRCCOPY);
				SelectObject (hMemDC, hDefaultBitmap);
				SelectObject (hMemDC, hFullScreenBitmap);
 
				// Printing:
				PRINTDLG pd = {0}; // Reset on declare
 
				pd.lStructSize = sizeof (pd);
				pd.Flags = PD_RETURNDEFAULT | PD_ALLPAGES | PD_RETURNDC;
				pd.nFromPage = 1;
				pd.nToPage = 1;
				pd.nCopies = 1;
 
				if (PrintDlg (&pd)) {
					if (pd.hDC) {
						// Scaling to fit on a printer page:
						double dblScreenRatio = (double) iScreenWidth / (double) iScreenHeight;
						int xPrnPixelsPerPage = GetDeviceCaps (pd.hDC, PHYSICALWIDTH);
						int xPrnPixelsMargin = GetDeviceCaps (pd.hDC, PHYSICALOFFSETX);
						int yPrnPixelsMargin = GetDeviceCaps (pd.hDC, PHYSICALOFFSETY);
						int printedWidth = xPrnPixelsPerPage - xPrnPixelsMargin * 2;
						int printedHeight = (int) ((double) printedWidth / dblScreenRatio);
 
						DOCINFO di = {0};
 
						di.cbSize = sizeof (di);
						di.lpszDocName = "Print Full Screen";
 
						StartDoc (pd.hDC, &di);
						StartPage (pd.hDC);
 
						// FINALLY!
						StretchBlt (pd.hDC,
									xPrnPixelsMargin, yPrnPixelsMargin, printedWidth, printedHeight,
									hMemDC, 0, 0, iScreenWidth, iScreenHeight, SRCCOPY);
 
						EndPage (pd.hDC);
						EndDoc (pd.hDC);
						DeleteDC (pd.hDC);
					}
					// Clean up after print dlg:
					if (pd.hDevMode) {
						GlobalFree (pd.hDevMode);
					}
					if (pd.hDevNames) {
						GlobalFree (pd.hDevNames);
					}
				}
				SelectObject (hMemDC, hDefaultBitmap);
				DeleteObject (hFullScreenBitmap);
			}
			DeleteDC (hMemDC);
		}
		ReleaseDC (NULL, hScreenDC);
	}
}

Open in new window

0
Comment
Question by:Sohela
[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 Comments
 
LVL 49

Accepted Solution

by:
DanRollins earned 500 total points
ID: 22791773
Rather than starting with the entire screen, why not start with the rectangle of your source window?  Assuming that you have the hWnd of your application program, you can get the coordinates of the inner part (not including the title bar, scrollbars, etc) by using:
   RECT rClient;
   GetClientRect(  hWnd,  &rClient );
Now use rClient.top and .left as the top left corner of the source, and calculate the width by subtracting top and left from .bottom and .right.
0
 

Author Comment

by:Sohela
ID: 22793412
Thanks for replying but ater using this also i am getting the same whole screen is getting printed not with title bars ,menu bars and all.I dont know where more should i make changes.

0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

735 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