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
Member
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

 
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 
Very helpful
QuestionGood job!memberDH00081 Mar '12 - 3:53 
That is a very nice program, I really enjoyed reading the source code. Well done.
 
Question: Would it be possible to modify a sub-window so that I can move it outside of the main window?
 
Thanks.
GeneralInstance of ClassNamemembercharleslambert1424 Mar '11 - 5:12 
Hi,
 
very good work you did with this.
 
I'd like to know if it is possible to get the instance of the ClassName on a form.
 
Example there is 3 TButtons on a form, I'd like to get the instance of each of them.
 
Just like: TButton1, TButton2, TButton3.
 
Regards,
 
- Charles
GeneralGreat!memberyulinxie16 May '10 - 4:22 
thx
GeneralOne PblmmemberNaveen.R10 Sep '07 - 18:23 
Hi,
 
Me too was trying to create some application like this and in between I had a problem in finding the window just below the mouse. So I searched the net and finally reached here. But your application also have same problem. Check the following secnario.
 
In a dialog place a frame and label control one over the other( the label should be kep inside the frame ).
Arrange the tab order so that frame is first and label is second. Now your application cannot identify the label which is inside the frame. But SPY++ does. I have found some other applications like WinID also finds this window. Do you have any solutions?
 

QuestionSpy++memberBourtree7 Mar '07 - 9:26 
Is it possible to programatically access the functionality of Spy++ - is there an interface to it that I could access. I would like to write an application that integrates at the UI level with a standard 'out of the box' application and was looking to use the content returnd by Spy++ to do so.
 
Thanks

GeneralNice article, but cannot compile with VS.Net 2005memberyjagota18 Feb '07 - 9:38 
Can u help me out. There were some errors, I did removed them but the executable doesnt executes, in debug or release configuration and even as a release standalone exe. Sigh | :sigh:
QuestionCan it capture a label in vb6 program?memberpctimhk30 Sep '06 - 8:24 
I can get the textbox caption. However, I cannot get a label caption in a form. It only can get the caption from a form. How can i get a caption of a label within a form?
 
Thank you for you help.
 
Tim
GeneralGoogle Earth ToolbarmemberTodd Wilder22 Jul '06 - 16:55 
I am trying to disable the google earth toolbar, and when I click "disable" on the toolbar it goes invisible (assuming thats showwindow()), but when you click the area the toolbar is still active. what other API functions go into your disable? great tool though
 

GeneralExtra OrdinarymemberAsheshVashi9 May '06 - 19:56 
OMG | :OMG:
Blush | :O

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

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