Click here to Skip to main content
15,903,689 members
Articles / Desktop Programming / MFC

Screen capture, window resizing utility with source code

Rate me:
Please Sign up or sign in to vote.
4.77/5 (50 votes)
1 Jul 20055 min read 268.6K   8.1K   124   69
This utility uses Lim Bio Liong's excellent Spy++ style Window Finder code to build a screen capture utility (featuring text capture as well as bitmaps) and window resizer/mover. It also demonstrates stay on top and expanding/contracting (i.e. more detail / less detail) dialog boxes.

New - 25th June 2005

  • Added a 'Center' button, which centers the found window on the desktop. (Useful for recording application demos etc..)
  • Now remembers its last position on the desktop and starts there next time.
  • Previous changes

Sample Image - winscraper.gif

Contracted view


Lim Bio Liong's excellent CodeProject article (MS Spy++ style Window Finder) does all the hard work relating to picking a window to work with for this utility. I claim no credit whatever for this.

However, Lim Bio Liong's sample project launches the window finder dialog from an SDI application and is provided principally to assist in demonstrating the window finder code.

As a developer, I wanted a simple, free tool that would do all of the following:

  • 'Scrape' the text off a dialog box control or message box, so that it can be pasted as text into an email or whatever. (How often have I dismissed the error box without accurately noting down its contents?)
  • Capture images of controls or windows from my application for documentation purposes etc..
  • Resize my application or web browser windows to a specific size for testing alternate screen resolutions or consistent documentation of my application.

That's what the tool presented in this article sets out to do. As a full time developer I expect to make a lot of use of it from now on!

Along the way, the source code also demonstrates 'stay on top' functionality, and the dialog box expands and contracts to show more or less detail.


The first thing I wanted to do was to turn the original SDI app + dialog box into a dialog box application, as the SDI bit was inappropriate to my design.

As an MFC developer, the easiest way was:

  1. Create an MFC dialog box application.
  2. Remove the app wizard created dialog box cpp and h files from the project.
  3. Remove the reference to the dialog box h file in the application cpp file.
  4. Remove the standard dialog box launch code from the application's InitInstance() and replace it with the code needed to launch the Win32 dialog, i.e.:

long lRet = (long)DialogBox


(HINSTANCE)m_hInstance, // handle to application instance
(IDD_DIALOG_SEARCH_WINDOW), // identifies dialog box template 
(HWND)NULL, // handle to owner window 
(DLGPROC)SearchWindowDialogProc // pointer to dialog box procedure 

It was also necessary to move some of the global declarations out of the SDI app file and into the windowfinder.cpp file. The result is that the application's InitApp() initializes the resources required for the dialog and then launches the dialog.

Scraping Text from Dialog and Message Boxes

The code in DisplayInfoOnFoundWindow() in windowfinder.cpp receives a handle to the selected window. With this handle, it is simple to get the window's text:

char winTxt[5000];
memset(winTxt, 0, 5000);
SendMessage(hwndFoundWindow, WM_GETTEXT, 5000, (LPARAM)winTxt);

(The MSDN documentation states that this method is preferable to GetWindowText() when getting text from a window outside your application.)

However, some controls do not yield their contents in this way. So, for example, to get the text from a listbox control, we use the following:

if(strcmp(szClassName, "ListBox") == 0)
    CListBox lb;                // Instantiate a MFC listbox object
           // Attach the listbox's HWND to our MFC object
    for(count=0; count < nItems; count++)
        lb.GetText(count, strItem);
        strcat(winTxt, strItem);
    lb.Detach();   // Must remember to detach,
                     // otherwise when lb goes out of scope
                      // it will destroy the listbox prematurely!

(It would be nice to capture the text from more complex controls, such as tree controls, using a similar approach but to date such an approach eludes me. Even myTreeCtrl.GetItemText(myTreeCtrl.GetRootItem() returns an empty string, for some reason.)

Capturing the Window to the Windows Clipboard

This is processed in SearchWindowDialogProc() - (search windowfinder.cpp for "if(wID == IDC_CAPTURE_BUTTON)").

A global is already set to the handle of the target window before SearchWindowDialogProc() is called, so we already have a handle to the window to capture.

We need to temporarily hide our application in case it overlaps the capture target, then we capture the screen area to a compatible bitmap and insert it into the clipboard, finally restoring our application's window.

The biggest challenge here was that I initially thought you should use the device context of the window you want to capture to create the compatible bitmap - however this led to the 'wrong' area being captured. Using "hdc=GetDC(HWND_DESKTOP)" gets things capturing correctly.

Moving and Resizing the Target Window

The code in DisplayInfoOnFoundWindow() in windowfinder.cpp receives a handle to the selected window. With this handle, it is simple to resize the target window (where h and w are the desired height and width of the target window):

GetWindowPlacement(g_hwndFoundWindow, &wp);
// Adjust width and height to required
// Set window placement
SetWindowPlacement(g_hwndFoundWindow, &wp);

Similarly, moving the window to a screen location is easily accomplished via the Get/SetWindowPlacement() Win32 functions.

Stay on Top

Making the dialog stay on top of all other windows is easy, but not documented in a sufficiently clear enough way for my simple mind to understand in the MS docs:

To set the stay on top property:

SetWindowPos( hwndDlg, HWND_TOPMOST, 0, 0, 0, 0, 

To remove it:

SetWindowPos( hwndDlg, HWND_NOTOPMOST, 0, 0, 0, 0, 

More / Less Detail on Dialog

We can make the dialog show more or less detail by resizing it using the position of a visible (or hidden) control on the dialog as a marker.

The following code block handles expanding / contracting the dialog:

GetWindowPlacement(hwndDlg, &dlgwp);
CRect rect;
UINT nBtnState=IsDlgButtonChecked(hwndDlg, IDC_EXPANDVIEW_CHECK);
if(nBtnState == BST_CHECKED) // Expanded view
// Get window placement of bottom groupbox, 
// which is the marker for the expanded view
GetWindowRect(GetDlgItem(hwndDlg, IDC_DETAILS_STATIC), &rect);
SetWindowPlacement(hwndDlg, &dlgwp);
else // Shrunk view
// Get window placement of window text static, which 
// is the marker for the expanded view
GetWindowRect(GetDlgItem(hwndDlg, IDC_WINTXT_STATIC), &rect);;
SetWindowPlacement(hwndDlg, &dlgwp);

The IDC_DETAILS_STATIC ID refers to the static groupbox labeled "Details:" at the bottom of the expanded application window.

The IDC_WINTXT_STATIC ID refers to the static groupbox labeled "Window Text" on the dialog.

As you can see, we get the initial dimensions of the dialog and marker statics using GetWindowPlacement() and GetWindowRect() and adjust the WINDOWPLACEMENT's rcNormalPosition members. Finally we use the adjusted WINDOWPLACEMENT structure to set the new size of the dialog.


Hopefully what we have here is a reasonably useful little tool for software / web site developers. Along the way, I learned a little more about Win32 (but think I will stick with MFC given the choice!). I hope you find the tool useful, even if the source code is a mixture of Lim Bio Liong's original work and my quick and dirty (but hell, they seem to work!) hacks.

Finally, if you do find the tool useful, I'd really appreciate it if you could kindly take the time to click on the link to at the top right of the tool's dialog. Oh, and also, how about giving this article a nice rating so that others can be alerted to the benefits too! Thanks!


  • Updated source and exe on 5 January 2004
  • Updated source and exe on 25 June 2005


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

Written By
CEO Data Perceptions
United Kingdom United Kingdom
Software designer and developer of Prophecy, a multi-user solution for sales forecasting and the Software Update Wizard, an automatic updates component that lets software developers add automatic updates to their Windows applications with a single line of code and which supports automatic updates, even where the logged in user does not have (write) rights to Program Files etc. folders.

I started working life as full time sales forecaster in the CPG sector. Transitioned to IT as I had an interest in software development and felt combining this with my previous business experience would be good.

After many years working for a company I developed Prophecy as a spare-time project and became self-employed in 1998, with Prophecy and the Software Update Wizard as my sole sources of income. It's been an interesting and challenging ride!

Comments and Discussions

GeneralMy vote of 5 Pin
evan36914-Jun-11 22:12
evan36914-Jun-11 22:12 
GeneralBug and improvement Pin
Vikas Misra(TCS)11-Aug-10 1:10
Vikas Misra(TCS)11-Aug-10 1:10 
GeneralRe: Bug and improvement Pin
peterboulton11-Aug-10 1:26
professionalpeterboulton11-Aug-10 1:26 
GeneralRe: Bug and improvement Pin
Vikas Misra(TCS)11-Aug-10 12:06
Vikas Misra(TCS)11-Aug-10 12:06 
GeneralRe: Bug and improvement Pin
peterboulton11-Aug-10 21:46
professionalpeterboulton11-Aug-10 21:46 
QuestionHow to capture whole screen? Pin
transoft26-Apr-09 4:35
transoft26-Apr-09 4:35 
QuestionDid any one write this utility in C# ?? Pin
Abu Alyan6-Jun-08 20:54
Abu Alyan6-Jun-08 20:54 
Questionpassword boxes Pin
kendals26-Mar-08 3:59
kendals26-Mar-08 3:59 
QuestionIs there a way to convert this to VB.Net? Pin
caeanis24-Feb-08 8:58
caeanis24-Feb-08 8:58 
AnswerRe: Is there a way to convert this to VB.Net? Pin
peterboulton25-Feb-08 0:01
professionalpeterboulton25-Feb-08 0:01 
GeneralRe: Is there a way to convert this to VB.Net? Pin
caeanis25-Feb-08 4:24
caeanis25-Feb-08 4:24 
GeneralRe: Is there a way to convert this to VB.Net? Pin
peterboulton25-Feb-08 4:33
professionalpeterboulton25-Feb-08 4:33 
QuestionGetting Visible Region Pin
kimSongIl3-Oct-07 16:14
kimSongIl3-Oct-07 16:14 
GeneralCapture Text in MS Word Pin
cat_12a19-May-07 7:40
cat_12a19-May-07 7:40 
Generalsuggestion Pin
prober820-Jul-06 17:22
prober820-Jul-06 17:22 
GeneralClips at high DPI Pin
Ed Brey16-Mar-06 3:36
Ed Brey16-Mar-06 3:36 
GeneralRe: Clips at high DPI Pin
peterboulton16-Mar-06 4:26
professionalpeterboulton16-Mar-06 4:26 
GeneralRe: Clips at high DPI Pin
Ed Brey16-Mar-06 4:46
Ed Brey16-Mar-06 4:46 
GeneralRe: Clips at high DPI Pin
peterboulton16-Mar-06 5:06
professionalpeterboulton16-Mar-06 5:06 
Generalmsdos Pin
roberto1979es8-Feb-06 4:18
roberto1979es8-Feb-06 4:18 
GeneralText Capture Pin
mike737318-Oct-05 2:34
mike737318-Oct-05 2:34 
GeneralRe: Text Capture Pin
peterboulton18-Oct-05 3:38
professionalpeterboulton18-Oct-05 3:38 
GeneralRe: Text Capture Pin
mike737318-Oct-05 3:42
mike737318-Oct-05 3:42 
GeneralVery nice tool Pin
S.H.Bouwhuis6-Jul-05 4:15
S.H.Bouwhuis6-Jul-05 4:15 
GeneralCapture of Window Longer than Screen Height Pin
lasercraft5-Jul-05 21:15
lasercraft5-Jul-05 21:15 

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

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