5,659,336 members and growing! (20,663 online)
Email Password   helpLost your password?
Web Development » Applications & Tools » Tools with source code     Intermediate

Capturing Window Controls and Modifying their properties

By Ashkbiz Danehkar

A Spy tool program like MS Spy++ that lets you capture window controls and modify their properties. Useful for learning window handles and their properties.
VC6, VC7.1, VC8.0, C++Windows, NT4, Win2K, WinXP, Win2003, VistaVS.NET2003, VS6, Visual Studio, Dev

Posted: 10 Feb 2005
Updated: 19 Feb 2005
Views: 78,070
Bookmarked: 96 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
43 votes for this Article.
Popularity: 7.68 Rating: 4.70 out of 5
2 votes, 4.8%
1
0 votes, 0.0%
2
0 votes, 0.0%
3
3 votes, 7.1%
4
37 votes, 88.1%
5

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

About the Author

Ashkbiz Danehkar



Occupation: Other
Location: United Kingdom United Kingdom

Other popular Applications & Tools articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 35 (Total in Forum: 35) (Refresh)FirstPrevNext
GeneralOne PblmmemberNaveen.R19:23 10 Sep '07  
QuestionSpy++memberBourtree10:26 7 Mar '07  
GeneralNice article, but cannot compile with VS.Net 2005memberyjagota10:38 18 Feb '07  
QuestionCan it capture a label in vb6 program?memberpctimhk9:24 30 Sep '06  
GeneralGoogle Earth ToolbarmemberTodd Wilder17:55 22 Jul '06  
GeneralExtra OrdinarymemberAsheshVashi20:56 9 May '06  
GeneralMsDosmemberroberto1979es5:15 8 Feb '06  
Generalvb.netmemberroberto1979es12:10 7 Feb '06  
GeneralRe: vb.netmemberAshkbiz Danehkar13:07 7 Feb '06  
GeneralRe: vb.netmemberTutu21:51 7 Jun '06  
Generalwindows controlsmemberB!Z23:13 16 Jan '06  
GeneralModifing Window Title namememberVitoto12:10 7 Dec '05  
GeneralRe: Modifing Window Title namememberAshkbiz Danehkar13:19 7 Feb '06  
GeneralFound bug and fix - nice appmemberdrudru20:54 2 Aug '05  
GeneralRe: Found bug and fix - nice appmemberAshkbiz Danehkar13:09 7 Feb '06  
Generalmay be a bugmemberAmilan7:17 29 Jun '05  
GeneralJust what I wantmemberispring1:30 24 Jun '05  
GeneralXP related questionmemberCodeBuddy268:59 12 Jun '05  
GeneralRe: XP related questionmemberJackyFrost15:02 26 Aug '05  
GeneralPorting to WindowsCEmemberhgode1:55 9 Jun '05  
GeneralRe: Porting to WindowsCEmemberhgode3:40 9 Jun '05  
GeneralRe: Porting to WindowsCEmemberfoobar76:57 15 Oct '05  
GeneralRe: Porting to WindowsCEmemberAshkbiz Danehkar13:22 7 Feb '06  
GeneralRe: Porting to WindowsCE [modified]memberPrabhat.Singh2:01 30 Sep '07  
GeneralWhere is the input focus?memberFree to Go10:35 21 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 19 Feb 2005
Editor: Smitha Vijayan
Copyright 2005 by Ashkbiz Danehkar
Everything else Copyright © CodeProject, 1999-2008
Web13 | Advertise on the Code Project