[译]捕获窗口控件并修改其属性
By robot-v1.0
本文链接 https://www.kyfws.com/applications/capturing-window-controls-and-modifying-their-prop-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1431 个词 阅读量 0[译]捕获窗口控件并修改其属性
原文地址:https://www.codeproject.com/Articles/9549/Capturing-Window-Controls-and-Modifying-their-prop
原文作者:Ashkbiz Danehkar
译文由本站 robot-v1.0 翻译
前言
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.
一个类似于MS Spy ++的间谍工具程序,可让您捕获窗口控件并修改其属性.对于学习窗口句柄及其属性很有用.
介绍(Introduction)
我对拥有Microsoft Visual Studio中的Spy ++之类的工具感兴趣,但我希望该工具可以修改窗口控件.例如,有时候,我们需要在运行时启用程序中的一些非活动控件. MS Spy ++没有执行此操作的任何功能,因此我是自己完成的.(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.)
该应用程序是不使用MFC的Win32项目.它通过以下方式钩住目标窗口(The application is a Win32 project without using MFC. It hooks the target window by) WindowFromPoint()
user32 API.有一种算法可以在子窗口和父窗口上查找,以通过使用以下方法捕获程序中的子窗口(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.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.1 VC8.0 C++ VC6 WinXP Win2003 Win2K Visual-Studio VS.NET2003 Dev 新闻 翻译