Click here to Skip to main content
Licence 
First Posted 27 Apr 2002
Views 255,765
Bookmarked 107 times

Capture Screen to Clipboard including dropdown menu

By | 12 Mar 2003 | Article
Capture screen image to clipboard including dropdown menu, combobox lists etc

Sample Image

Introduction

There are many sharewares that capture screen and copy image to clipboard or save as a file. Some of them charge more than $15 and can't capture dropdown menu/list. The problem is when user "active CaptureScreen" program, all other windows become inactive and close their file menu or dropdown list. This simple program can capture any screen image including dropdown menu or combobox list.

Basic idea

Since clicking on "PrintScreen" will send an entire window screen to the clipboard, we can read "entire window screen" from clipboard and then let users copy any portion of screen to clipboard. To implement this we need

  1. Hook keyboard: when user clicks "PrintScreen", application will be notified.
  2. Read image from clipboard.
  3. Copy image and paste to clipboard.

[DLL] Hook keyboard

To hook system wide keyboard event, we need a DLL. The demo source code HScr2Clpbrd_DLL.cpp is pretty much similar to Joseph[2] demo program. The big difference is that Joseph[2] has a pretty neat header file that I don't use it. In case it's first time you create a simple DLL file, you may follow this: VC++ -> File Menu -> New -> Win32 Dynamic-link Library -> A simply DLL project -> finish. you will get a DLLMain() method.
Change

BOOL APIENTRY DllMain( HANDLE hModule,DWORD  ul_reason_for_call, <BR>LPVOID lpReserved)
to
BOOL APIENTRY DllMain( HINSTANCE hInstance,DWORD  ul_reason_for_call,<BR>LPVOID lpReserved)
And implement your InstallKBHook() and UnInstallKBHook() that simply use API: SetWindowsHookEx() and UnhookWindowsHookEx(), to hook/unhook keyboard event.
hookKeyBoard = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardHook,hInst,0);
BOOL bUnHook = UnhookWindowsHookEx(hookKeyBoard);
You may want to pay a little attention to the declaration of
__declspec(dllexport) BOOL InstallKBHook(HWND hWnd);
__declspec(dllexport) BOOL UnInstallKBHook(HWND hWnd);
Because we will export those methods to the application, we declare them as __declspec(dllexport); and, on the other hand, application need to import them (See HScr2ClpDlg.cpp)
__declspec(dllimport) BOOL InstallKBHook(HWND hWnd);
__declspec(dllimport) BOOL UnInstallKBHook(HWND hWnd);

If you like to know more about API Hook, please refer Joseph[2], Ivo[3], and Kyle[4] for more detail.

[DLL] Post message to application

So far, we can InstallKBHook() and UnInstallKBHook() but we need to notify our application that "PrintScreen" has been clicked. We need to register our window message

UWM_ClickPrnScrn = ::RegisterWindowMessage(<BR>"UWM_ClickPrintScreen_B2ABC742-0A63-49c3-9ACB-CF0068027A66");
Once we have our Window Message, we can Post Message to notify our application that user clicks "PrintScreen"
if( (nCode >=0) && (nCode == HC_ACTION) && (wParam == VK_SNAPSHOT))
	::PostMessage(hWndReceiver,UWM_ClickPrnScrn,0,0);
It's pretty simple to hook system wide keyboard. Isn't it? Please refer Joseph[1] for more, wide and deep, detail

[Apps] Read image from clipboard

When DLL post message, application need to receive the message. Since it's our, user defined, window message, we need to manually Map it.

1. in header file, HScr2ClpDlg.h, declare this method

afx_msg BOOL OnClickPrintScreen(WPARAM wParam, LPARAM lParam);

2. in cpp file, HScr2ClpDlg.cpp, message map add this ON_REGISTERED_MESSAGE(UWM_ClickPrnScrn, OnClickPrintScreen)
BEGIN_MESSAGE_MAP(CHScr2ClpDlg, CDialog)
	ON_REGISTERED_MESSAGE(UWM_ClickPrnScrn, OnClickPrintScreen)
	//{{AFX_MSG_MAP(CHScr2ClpDlg)
	...
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
In the definition of OnClickPrintScreen(), we first verify using IsClipboardFormatAvailable(CF_BITMAP) that the clipboard contains BITMAP data and then open (using OpenClipboard()) clipboard. If so far is fine, attach a CBitmap to clipboard BITMAP Handle
HBITMAP hBm;
CBitmap cBm;
hBm = (HBITMAP)GetClipboardData(CF_BITMAP);
...
if(!cBm.Attach(hBm))
...
Since we have a CBitmap now, we can Copy it to our application's panel and let user "select" any image she/he needs
CClientDC dcScreen(this);
CDC dcMem;
dcMem.CreateCompatibleDC(&dcScreen);
CBitmap* pOldBitmap = dcMem.SelectObject(&cBm);
dcScreen.BitBlt(0,0,bBm.bmWidth,bBm.bmHeight ,&dcMem,0,0,SRCCOPY);
dcMem.SelectObject(pOldBitmap);

[Apps] Copy image and paste to clipboard

We copy user's "selection" from client area and paste to clipboard

BOOL CHScr2ClpDlg::CopyRect2Clipboard()
{
	CRect rect(m_ptFirst,m_ptLast);
	rect.NormalizeRect();	
	if(rect.IsRectEmpty() || rect.IsRectNull())
		return FALSE;	

	//CDC *pDc = GetWindowDC();//include Non-Client area.
	CClientDC dcScrn(this);
	CDC memDc;
	if(!memDc.CreateCompatibleDC(&dcScrn))
		return FALSE;	
	CBitmap bitmap;
	if( !bitmap.CreateCompatibleBitmap(&dcScrn,rect.Width(),<BR>            rect.Height()))
		return FALSE;
	CBitmap* pOldBitmap = memDc.SelectObject(&bitmap);
	memDc.BitBlt(0,0,rect.Width(),rect.Height(),&dcScrn,rect.left ,<BR>                     rect.top ,SRCCOPY );		
	if(OpenClipboard())
	{
		EmptyClipboard();
		SetClipboardData(CF_BITMAP,bitmap.GetSafeHandle());
		CloseClipboard();
	}
	memDc.SelectObject(pOldBitmap);
	return TRUE;
}

Summary

This tiny program is very simple because it doesn't have any complicated coding. You can find many articles showing you how to Copy images to the clipboard, Hook keyboard, or, Capture mouse movement... from CodeProject.com or other web sites. 

However, there is one cool/neat "mailto button" on the demo program panel; it's, IMHO, the most difficult coding in this tiny utility. Please refer Chris Maunder's "Tray Calendar", Chris[5]. The idea of this program is "put people's idea together and have fun"; and, of course, I need to capture screen for my work. I may enhance it that provide a "Preview" panel which has "Copy", "Save as file", and "Discard" options for user when I have time. If anyone can post your enhancement, I would love to update the article, and thank you.

Known bug

If you run program and leave it on screen, don't minimize it to taskbar, and click "PrintScreen", apps will set itself as MostTop Window as expected. But "minimize" and "close" buttons will not work until you set it to non-top Z-order. In other words, you need to click another window to make apps "inactive" and click apps itself to make it "active"; therefore, "minimize" and "close" buttons will work as usual. On the other hand, if you "minimize" apps to taskbar and "click "PrintScreen", everything just works fine.

Reference

[1]: "Message Management", Joseph M. Newcomer, 05/17/2000
[2]: "Hooks and DLLs", Joseph M. Newcomer, 04/01/2001
[3]: "API hooking revealed", Ivo Ivanov, 04/06/2002
[4]: "Win32 Hooks", Kyle Marsh, 07/29/1993, MSDN
[5]: "Tray Calendar", Chris Maunder, 04/06/2002

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Y. Huang



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionHow can i get Print screen without using keyboard interrupt. Pinmemberrajajay823:09 18 Sep '06  
QuestionHow do I copy selected text into clipboard ? Pinmembersupermango1:02 23 Apr '06  
Generalcopy a CWnd to the clipboard... Pinmemberd00_ape3:27 28 Mar '06  
Questionhow to use SetDIBitsToDevice ???? Pinmemberjags_vc18:23 17 Mar '05  
hello???
 
i want to know how SetDIBitsToDevice works ???
I have tryied it to display bitmap DIB data to directly to a
CPaintDC object,but displayed bitmap is cutting bitmap that is not a full bitmap so can any You tell how SetDIBitsToDevice works or any other idea????
 
Actually i am trying to transfer image of current Desktop to another PC i have alresdy done it .but in that i have used file (.bmp) to transfer image.
Now i have to transfer Desktop image without creating file...i got 90% success but only this is the problem so....
 
Regards,
jagdish.
Questionhow can i read captured image from memory? Pinmemberjags_vc1:34 7 Mar '05  
AnswerRe: how can i read captured image from memory? PinsussFromAuthor4:41 7 Mar '05  
GeneralRe: how can i read captured image from memory? Pinmemberjags_vc18:12 8 Mar '05  
GeneralRe: how can i read captured image from memory? PinsussAnonymous4:35 9 Mar '05  
GeneralRe: how can i read captured image from memory? Pinsussjags_ddit18:08 9 Mar '05  
Generalexclude self app Pinmember9999kkk17:53 27 Feb '04  
GeneralRe: exclude self app PinsussY. Huang7:19 1 Mar '04  
QuestionIs it possible to make a hook to the printscreen function, not the clipboard? PinmemberMiguel Lopes6:40 15 Oct '03  
AnswerRe: Is it possible to make a hook to the printscreen function, not the clipboard? PinsussAnonymous8:07 15 Oct '03  
Generalwhy keyboard hook PinmemberNowwin220:54 2 Oct '03  
GeneralRe: why keyboard hook PinsussY. Huang4:35 3 Oct '03  
Questionwhen using gdi+,how can i do that? Pinmembercowen22:41 25 Jul '03  
AnswerRe: when using gdi+,how can i do that? PinsussY. Huang8:27 31 Jul '03  
QuestionBug Fix ? PinsussStealthy11:27 7 Nov '02  
AnswerRe: Bug Fix ? PinsussY. Huang12:37 7 Nov '02  
GeneralRe: Bug Fix ? PinsussDirk Brouns23:35 7 Nov '02  
GeneralRe: Bug Fix ? PinsussY. Huang4:49 8 Nov '02  
GeneralRe: Bug Fix ? PinsussDirk Brouns2:48 9 Nov '02  
GeneralRe: Bug Fix ? PinsussY. Huang4:17 11 Nov '02  
GeneralRe: Bug Fix ? PinsussDirk Brouns4:26 11 Nov '02  
AnswerRe: Bug Fix ? PinmemberY. Huang7:57 17 Jan '03  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 13 Mar 2003
Article Copyright 2002 by Y. Huang
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid