Click here to Skip to main content
15,861,172 members
Articles / Programming Languages / C++
Article

Capturing Window Controls and Modifying their properties

Rate me:
Please Sign up or sign in to vote.
4.94/5 (56 votes)
19 Feb 2005GPL32 min read 208.5K   7.6K   157   43
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.

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)


Written By
Germany Germany
Ashkbiz Danehkar studied electrical engineering and computational science at the University of Rostock, Germany, where he obtained a Master of Science in Computational Engineering in the special field of Electrical Engineering in 2007. He worked as a software and hardware developer for some private limited companies until 2005, mostly focusing on industrial automation and microcontroller programming. During 2005–2006, he worked part-time remotely as a software reverse engineer for Panda Security (Bilbao, Spain). His master's thesis in 2007 was about the development of a microcontroller-based measurement system using an embedded system equipped with a real-time operating system (RTOS) and an AVR microcontroller to monitor the neuromuscular blockade and control the anesthesia.

Comments and Discussions

 
Questionsimply to thank you Pin
Ali Poostchian2-Oct-18 10:32
Ali Poostchian2-Oct-18 10:32 
QuestionTo most Pin
Nguyen Luong Son20-Oct-14 1:03
Nguyen Luong Son20-Oct-14 1:03 
QuestionManually enter handle Pin
Nguyen Luong Son28-Aug-14 17:33
Nguyen Luong Son28-Aug-14 17:33 
QuestionStay on top .... Pin
Member 51837411-Oct-13 0:41
Member 51837411-Oct-13 0:41 
GeneralMy vote of 5 Pin
Yiannis Spyridakis10-May-12 1:06
Yiannis Spyridakis10-May-12 1:06 
QuestionGood job! Pin
DH00081-Mar-12 3:53
DH00081-Mar-12 3:53 
GeneralInstance of ClassName Pin
charleslambert1424-Mar-11 5:12
charleslambert1424-Mar-11 5:12 
GeneralGreat! Pin
yulinxie16-May-10 4:22
yulinxie16-May-10 4:22 
GeneralOne Pblm Pin
Naveen10-Sep-07 18:23
Naveen10-Sep-07 18:23 
QuestionSpy++ Pin
Bourtree7-Mar-07 9:26
Bourtree7-Mar-07 9:26 
GeneralNice article, but cannot compile with VS.Net 2005 Pin
yjagota18-Feb-07 9:38
yjagota18-Feb-07 9:38 
QuestionCan it capture a label in vb6 program? Pin
pctimhk30-Sep-06 8:24
pctimhk30-Sep-06 8:24 
GeneralGoogle Earth Toolbar Pin
Todd Wilder22-Jul-06 16:55
Todd Wilder22-Jul-06 16:55 
GeneralExtra Ordinary Pin
AsheshVashi9-May-06 19:56
AsheshVashi9-May-06 19:56 
GeneralMsDos Pin
roberto1979es8-Feb-06 4:15
roberto1979es8-Feb-06 4:15 
Generalvb.net Pin
roberto1979es7-Feb-06 11:10
roberto1979es7-Feb-06 11:10 
GeneralRe: vb.net Pin
Ashkbiz Danehkar7-Feb-06 12:07
Ashkbiz Danehkar7-Feb-06 12:07 
GeneralRe: vb.net Pin
Corneliu Tusnea7-Jun-06 20:51
Corneliu Tusnea7-Jun-06 20:51 
Generalwindows controls Pin
B!Z16-Jan-06 22:13
B!Z16-Jan-06 22:13 
GeneralModifing Window Title name Pin
Vitoto7-Dec-05 11:10
Vitoto7-Dec-05 11:10 
GeneralRe: Modifing Window Title name Pin
Ashkbiz Danehkar7-Feb-06 12:19
Ashkbiz Danehkar7-Feb-06 12:19 
GeneralFound bug and fix - nice app Pin
drudru2-Aug-05 19:54
drudru2-Aug-05 19:54 
GeneralRe: Found bug and fix - nice app Pin
Ashkbiz Danehkar7-Feb-06 12:09
Ashkbiz Danehkar7-Feb-06 12:09 
Generalmay be a bug Pin
Amilan29-Jun-05 6:17
Amilan29-Jun-05 6:17 
GeneralJust what I want Pin
bugtwo24-Jun-05 0:30
bugtwo24-Jun-05 0:30 

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.