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.
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
- Hook keyboard: when user clicks "PrintScreen", application will be notified.
- Read image from clipboard.
- 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 demo program. The big difference is that Joseph 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
BOOL APIENTRY DllMain( HANDLE hModule,DWORD ul_reason_for_call, <BR>LPVOID lpReserved)
BOOL APIENTRY DllMain( HINSTANCE hInstance,DWORD ul_reason_for_call,<BR>LPVOID lpReserved)
And implement your
that simply use API:
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
; and, on the other hand, application need to import
__declspec(dllimport) BOOL InstallKBHook(HWND hWnd);
__declspec(dllimport) BOOL UnInstallKBHook(HWND hWnd);
If you like to know more about API Hook, please refer Joseph, Ivo, and Kyle for more detail.
[DLL] Post message to application
So far, we can
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))
It's pretty simple to hook system wide keyboard. Isn't it? Please refer Joseph for more, wide and deep
[Apps] Read image from clipboard
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,
, message map
In the definition of
, we first verify using
that the clipboard contains BITMAP data and then open (using
clipboard. If so far is fine, attach
to clipboard BITMAP Handle
hBm = (HBITMAP)GetClipboardData(CF_BITMAP);
Since we have a
now, we can Copy
it to our application's panel and let user "select" any image she/he needs
CBitmap* pOldBitmap = dcMem.SelectObject(&cBm);
[Apps] Copy image and paste to clipboard
We copy user's "selection" from client area and paste to clipboard
if(rect.IsRectEmpty() || rect.IsRectNull())
if( !bitmap.CreateCompatibleBitmap(&dcScrn,rect.Width(),<BR> rect.Height()))
CBitmap* pOldBitmap = memDc.SelectObject(&bitmap);
memDc.BitBlt(0,0,rect.Width(),rect.Height(),&dcScrn,rect.left ,<BR> rect.top ,SRCCOPY );
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. 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.
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.
: "Message Management", Joseph M. Newcomer, 05/17/2000
: "Hooks and DLLs", Joseph M. Newcomer, 04/01/2001
: "API hooking revealed", Ivo Ivanov, 04/06/2002
: "Win32 Hooks", Kyle Marsh, 07/29/1993, MSDN
: "Tray Calendar", Chris Maunder, 04/06/2002