Archive for July, 2008
How to capture the screenshot of window?
4![]()
We’re familear with PrintScreen and Alt+PrintScreen shortcuts. They are used to take the screenshot of entire desktop or a particular window. Well, Is it possible to take the snapshot of a particular control or a particular window by yourself?
![]()
You can use the api – PrintWindow(). If you provide the window handle, the screenshot of the window will be drawn to the provided device context. See the screenshot application which captures the screenshot of calculator application.

See the code snippet.
void CScreenShotDlg::OnPaint()
{
// device context for painting
CPaintDC dc(this);
// Get the window handle of calculator application.
HWND hWnd = ::FindWindow( 0, _T( "Calculator" ));
// Take screenshot.
PrintWindow( hWnd,
dc.GetSafeHdc(),
0 );
}
![]()
The PrintWindow() is available only from XP onwards. So for getting this compiled, you’ve to add _WIN32_WINNT=0×0501 to preprocessor definitions in project settings.
Also check WM_PRINT message, which is more or less same as PrintWindow().
![]()
Targeted Audience – Beginners.
How to identify the module load order?
0![]()
When an application starts, windows loads the necessary dll files to the system memory – “in required order”. But how to get the dll load order of perticular application? You’ve already seen – When we start the application via debugger, it will output the module load order info to the watch window. Have a look at it,
Loaded 'ntdll.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\kernel32.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\dbghelp.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\msvcrt.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\advapi32.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\rpcrt4.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\mfc42.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\user32.dll', no matching symbolic information found. Loaded 'C:\Windows\System32\gdi32.dll', no matching symbolic information found.
Seems Good! But, is there any easy method?
![]()
Yes! Take menu item, Debug -> Modules. And it will show the dialog of all loaded modules. By default the modules are sorted in load order. See the screenshot.

![]()
Now you don’t need to jump into that watch window for searching the module load order.
![]()
Targeted Audience – Beginners.
How to locate the process which owns the window by using window handle?
0![]()
“Goto Process” menu item in Task Manager is one of the frequently used stuff. isn’t it?
Its shows a list of windows currently displayed in desktop and by using “Goto Process”, we can find the process which owns the window. Have a look at it and feel refreshed! Ever thought of doing the same? How can we identify the process which owns the window by using window handle?

![]()
You can use the api – GetWindowThreadProcessId(). If you provide the window handle, if will return back the Id of the process which owns that window. Have a look at code snippet.
// Get the Id of Process which owns Windows Desktop.
DWORD dwProcessId = 0;
GetWindowThreadProcessId( ::GetDesktopWindow(),
&dwProcessId );
![]()
Pretty good! nah?
![]()
Targeted Audience – Intermediate.
How to watch a range of values inside huge array?
0![]()
Assume you’ve an array with 1000 elements in it and you want to watch elements from 500 ~ 510, what will you do? One method is you can watch the array directly which will list all the 1000 variables and you’ve to scroll up to 500th element and have to watch. Is there any trick to watch just the range of 10 elements between 500 to 510?
![]()
Yes! Basically the idea is, you’ve to add – (Array+RangeStart)+RangeCount to the watch window. But there are different scenarios and obviously the trick varies slightly. See examples below.
1) If you have dynamically allocated array.
Check the code snippet.
// Array ptr with 1000 elements.
int* pArray = new int[1000];
// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
pArray[ ArrayIndex ] = 1;
}
In this case for watching range 500 ~ 510, you’ve to add – (pArray+500),10 to watch window. See the screenshot.

2) If you have array on stack.
If you’re array is on stack, if you add your array to watch window, the debugger automatically expands all elements, since it knows the max size of array. But we just want to see the range. See the code snippet.
// Array with 1000 elements.
int Array[1000] = { 0 };
// Initialize range 500 to 509 as 1.
for( int ArrayIndex = 500; ArrayIndex < 510; ++ArrayIndex )
{
Array[ ArrayIndex ] = 1;
}
In this case for watching range 500 ~ 510, you’ve to add – ((int*)Array+500),10 to watch window. See the screenshot.

![]()
Watch window is a Treasure chest! isn’t it?
![]()
Targeted Audience – Intermediate.
How to check whether a file exists of not?
2![]()
While reading configuration file, while reading some setting file, every where we’ve to check whether the file exists or not before opening and starts reading. The simple technique is to open the file by using file apis or to search for the file by using FindFirstFile(). Or is there any simple api to do so?
![]()
You can use the api – PathFileExists(). Check the following code snippet.
#include "shlwapi.h"
...
// File path.
CString csFile = _T( "C:\\Autumn Leaves.jpg");
if( PathFileExists( csFile ))
{
// Yes! the file exists.
}
![]()
Don’t forget to add shlwapi.lib to your project settings.
![]()
Targeted Audience – Beginners.
How to restrict mouse cursor inside window?
2![]()
every windows application is a citizen of Windows OS and the mouse is shared between them. If your applications shows some critical dialog to get response, but its quite possible that your dialog can be skipped by user and take another application window. How can you restrict the mouse cursor inside your application window?
For instance, the following application – CursorJail. How can you close your mouse pointer into the dialog prison?

![]()
You can use the api – ClipCursor(). See the following code snippet.
// Get window rect. RECT WindowRect; GetWindowRect( &WindowRect ); // Restrict the cursor ClipCursor( &WindowRect ); ... // Free the cursor. ClipCursor( 0 );
Well, need the code snippet for drawing prison?
// Get client rect.
CRect ClientRect;
GetClientRect( &ClientRect );
const int SteelBarCount = 5;
int xOffset = ClientRect.Width() / SteelBarCount;
dc.Draw3dRect( 0,0, ClientRect.Width(), ClientRect.Height(), 0, 0 );
// Draw steel bars.
for( int SteelBarIndex = 1; SteelBarIndex < SteelBarCount; ++SteelBarIndex )
{
dc.MoveTo( xOffset * SteelBarIndex, 0 );
dc.LineTo( xOffset * SteelBarIndex, ClientRect.Height());
}
dc.MoveTo( 0,0 );
dc.LineTo( ClientRect.Width(), ClientRect.Height());
![]()
You can use this clip feature to restrict the cursor between different dialogs of same application. You can hide a lot of GUI bugs. Try it
![]()
Targeted Audience – Beginners.
