Click here to Skip to main content
Click here to Skip to main content

Capturing Window Controls and Modifying their properties

By , 19 Feb 2005
 

Sample Image - windowspy.gif

Introduction

I was interested to have a tool such as Spy++ in Microsoft Visual Studio, but I'd like that the tool can modify window controls. For instance, some times, we need to enable some inactive controls in a program during run-time. MS Spy++ does not have any facilities to do this, and so I made it by myself.

This program is useful to obtain a window handle, caption, class name, style, and size. Moreover, it facilitates changing caption name, style, and extended style of victim window control in run-time load.

The application is a Win32 project without using MFC. It hooks the target window by WindowFromPoint() user32 API. There is an algorithm to seek on child and parent window to capture child windows inside a program by using GetWindowRect() and GetWindow() in a tricky way. It applies GetWindowLong() to obtain window properties and SetWindowLong() to modify them.

Hooking target window

We can hook victim window through WM_MOUSEMOVE mouse event:

case WM_MOUSEMOVE:
if (bSeeking)
{
  HWND hWindowUnderTheMouse;
  //HWND hWindowUnderTheMouseChild;
  points = MAKEPOINTS(lParam); 
  point.x=points.x;
  point.y=points.y;
  ClientToScreen(hDlg,&point);
  hWindowUnderTheMouse = WindowFromPoint(point);
  if((hwndMain==hWindowUnderTheMouse)||
    (hwndMain==GetParent(hWindowUnderTheMouse)))
  {
    break;
  }
  //------------------------------
  hChildFound=NULL;
  SeekDumpWindow(hWindowUnderTheMouse,point);
  if((IsWindow(hChildFound))&&(hChildFound!=NULL))
  {
    hWindowUnderTheMouse=hChildFound;
  }
  //------------------------------
  if(hWindowUnderTheMouse != hSeekedWindow)
  {
    //
    FrameWindow(hSeekedWindow);
    hSeekedWindow = hWindowUnderTheMouse;
    FrameWindow(hSeekedWindow);
    // update the twin window
    strcpy(szSeekedWindow,"0x");
    _itoa((DWORD)hSeekedWindow,szSeekedWindow+2,16);
    CharUpperBuff(szSeekedWindow+2,(DWORD)strlen(szSeekedWindow));
    SetDlgItemText(hDlg,IDC_WINDOWHANDLE,szSeekedWindow);
    GetWindowInfo(hSeekedWindow);
  }
}
else
{
  points = MAKEPOINTS(lParam); 
  point.x=points.x;
  point.y=points.y;
}
break;

It seeks the correct window target by searching through its parent and its children by SeekDumpWindow() which is defined in <DumpSeek.h> inside the program.

Modifying target window

It is very easy to modify the victim window by its handle. If you have the window handle, you can obtain and change styles very easily by GetWindowLong() and SetWindowLong() and also window caption by using SetWindowText() and SendDlgItemMessage(). By looking inside UpdateFlags() in the program, you will find the code which performs this to modify the target window:

if(TopMost) 
{
  dwExtendedStyle|=WS_EX_TOPMOST; 
  SetWindowPos(hHandle, 
  HWND_TOPMOST, 0, 0, 0, 0,
  SWP_NOSIZE|SWP_NOMOVE);
}
else
{
  dwExtendedStyle&=~WS_EX_TOPMOST;
  SetWindowPos(hHandle, 
  HWND_NOTOPMOST, 0, 0, 0, 0,
  SWP_NOSIZE|SWP_NOMOVE);
}
//----------------------------------------------
if(BACKdwID!=dwID) SetWindowLong(hHandle, GWL_ID, dwID);
if(BACKdwStyle!=dwStyle)SetWindowLong(hHandle, GWL_STYLE, dwStyle);
if(BACKdwExtendedStyle=dwExtendedStyle)
SetWindowLong(hHandle, GWL_EXSTYLE, dwExtendedStyle);
if(Visible)
{
  ShowWindow(hHandle,SW_HIDE);
  ShowWindow(hHandle,SW_SHOW);
}
else
{
  ShowWindow(hHandle,SW_SHOW);
  ShowWindow(hHandle,SW_HIDE);
}
UpdateWindow(GetParent(hHandle));

The code to change caption depends on the type of window control. To change caption of Edit Control and Box Control, it has to use SendDlgItemMessage() instead of SetWindowText() in other window controls:

if((strstr(_szClassName,"EDIT")==NULL)&&(strstr(_szClassName,"BOX")==NULL))
{
  SetWindowText(hHandle,szTitle);
}
else
{
  HWND hWndParent=GetParent(hHandle);
  SendDlgItemMessage(hWndParent,dwID,WM_SETTEXT,0,(LPARAM)szTitle);
}

Conclusion

I hope this tool clarifies window classes and their manipulation for people who are beginners in this field. This would be an introduction to make window Spy tools and work with handles.

Eventually, I should appreciate you if you would kindly take the time to mention your ideas, suggestions, and any bugs you may find in this program.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Ashkbiz Danehkar
Other
Australia Australia
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberYiannis Spyridakis10-May-12 1:06 
QuestionGood job!memberDH00081-Mar-12 3:53 
GeneralInstance of ClassNamemembercharleslambert1424-Mar-11 5:12 
GeneralGreat!memberyulinxie16-May-10 4:22 
GeneralOne PblmmemberNaveen.R10-Sep-07 18:23 
QuestionSpy++memberBourtree7-Mar-07 9:26 
GeneralNice article, but cannot compile with VS.Net 2005memberyjagota18-Feb-07 9:38 
QuestionCan it capture a label in vb6 program?memberpctimhk30-Sep-06 8:24 
GeneralGoogle Earth ToolbarmemberTodd Wilder22-Jul-06 16:55 
GeneralExtra OrdinarymemberAsheshVashi9-May-06 19:56 
GeneralMsDosmemberroberto1979es8-Feb-06 4:15 
Generalvb.netmemberroberto1979es7-Feb-06 11:10 
GeneralRe: vb.netmemberAshkbiz Danehkar7-Feb-06 12:07 
GeneralRe: vb.netmemberTutu7-Jun-06 20:51 
Generalwindows controlsmemberB!Z16-Jan-06 22:13 
GeneralModifing Window Title namememberVitoto7-Dec-05 11:10 
GeneralRe: Modifing Window Title namememberAshkbiz Danehkar7-Feb-06 12:19 
GeneralFound bug and fix - nice appmemberdrudru2-Aug-05 19:54 
GeneralRe: Found bug and fix - nice appmemberAshkbiz Danehkar7-Feb-06 12:09 
Generalmay be a bugmemberAmilan29-Jun-05 6:17 
GeneralJust what I wantmemberispring24-Jun-05 0:30 
GeneralXP related questionmemberCodeBuddy2612-Jun-05 7:59 
GeneralRe: XP related questionmemberJackyFrost26-Aug-05 14:02 
GeneralPorting to WindowsCEmemberhgode9-Jun-05 0:55 
GeneralRe: Porting to WindowsCEmemberhgode9-Jun-05 2:40 
GeneralRe: Porting to WindowsCEmemberfoobar715-Oct-05 5:57 
GeneralRe: Porting to WindowsCEmemberAshkbiz Danehkar7-Feb-06 12:22 
GeneralRe: Porting to WindowsCE [modified]memberPrabhat.Singh30-Sep-07 1:01 
QuestionWhere is the input focus?memberFree to Go21-Mar-05 9:35 
GeneralTiz the sweet nectar...memberMark (Code6) Belles15-Mar-05 11:54 
Gotta give you props. 5 Stars. You beat me to the punch. lol, just this weekend I was messing around with one of my other articles, and someone wanted to do Spy++ like window finding/highlighting, so I code up that feature. To let them spy on a window, and then capture it. Since you like this sort of thing, but mine's in C#.
 
http://www.codeproject.com/useritems/Screen_Capturing.asp
 
While I was working on it, I was looking at Spy thinking, you know, it'd be fun to make it edit the style bits of a window... and just today I stumble on your article!
 
I dig the MFC style property box. Good stuff. You did a better job of the cursors, I swiped them from another cursor I found on here. Pretty good stuff man, I like your style.
 
-Mark
 
Si vic pacem para bellum - If you want peace, prepare for war
GeneralSend Messages plansmemberPeter Ritchie12-Feb-05 6:39 
GeneralRe: Send Messages plansmemberAshkbiz Danehkar13-Feb-05 5:59 
GeneralWell, I must say, something innovative!memberPolite Programmer11-Feb-05 7:47 
GeneralRe: Well, I must say, something innovative!memberThatsAlok11-Feb-05 17:54 
GeneralRe: Well, I must say, something innovative!memberAshkbiz Danehkar13-Feb-05 6:19 
GeneralGreat Article!memberThatsAlok10-Feb-05 18:23 
GeneralRe: Great Article!memberAshkbiz Danehkar13-Feb-05 6:14 
GeneralRe: Great Article!memberf211-Apr-05 6:10 
GeneralRe: Great Article!memberAshkbiz Danehkar12-Apr-05 7:00 

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 19 Feb 2005
Article Copyright 2005 by Ashkbiz Danehkar
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid