HWND hWnd =
AfxGetMainWnd()->m_hWnd
GetTopWindow
函数功能:该函数检查与特定父窗口相联的子窗口z序(Z序:垂直屏幕的方向,即叠放次序),并返回在z序顶部的子窗口的句柄。
函数原型:HWND
GetTopWindow(HWND
hWnd);
参数:
hWnd:被查序的父窗口的句柄。如果该参数为NULL,函数返回Z序顶部的窗口句柄。
返回值:
如果函数成功,返回值为在Z序顶部的子窗口句柄。如果指定的窗口无子窗口,返回值为NULL。
GetForegroundWindow
函数功能:该函数返回当前系统的前台窗口的窗口句柄。
函数原型:HWND
GetForegroundWindow(VOID)
返回值:函数返回前台窗回的句柄。
GetActiveWindow
函数功能:该函数可以获得与调用该方法的线程的消息队列相关的活动窗口的窗口句柄(就是取得当前进程的活动窗口的窗口句柄)。
函数原型:HWND
GetActiveWindow(VOID)
返回值:返回值是与调用线程的消息队列相关的活动窗口的句柄。否则,返回值为NULL。
GetSafeHwnd
函数功能:获取某个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数。
通过下面的例子来看其理由:
CWnd
*pwnd = FindWindow(“ExploreWClass”,NULL)//希望找到资源管理器
HWND hwnd =
pwnd->m_hwnd//得到洞汪它的HWND
这样的代码当开始得到的pwnd为空的时候就会出现一个“General protection
error”,并关闭应用程序,因为一般不能对一个NULL指针访问其成员,如果用下面的代码:
CWnd *pwnd =
FindWindow(“ExploreWClass”,NULL)//希望找到资源管理器
HWND hwnd =
pwnd->GetSafeHwnd()
//得到它的HWND
就不会出现问题,因为尽管当pwnd是NULL时,GetSafeHwnd仍然可以用,只是返回NULL
IsWindowVisible
函数功能:该函数获得给定窗档拦口的可视状态。
函数原型:BOOL
IsWindowVisible(HWND hWnd);
参数;
hWnd:被测试窗口的句柄。
返回值:
如果指定的窗口及其父窗口具有WS_VISIBLE风格,返回值为非零;如果指定的窗口及其父窗口不具有WS_VISIBLE风格,返回值为零。由于返回值表明了窗口是否具有Ws_VISIBLE风格,因此,即使该窗口被其他窗口遮盖,函数返回值也为非零。
备注:
窗口的可视状态由WS_VISIBLE位指示。当设置了WS_VISIBLE位,窗口就可显示,而且只要窗口具有WS_VISIBLE风格,任何画在窗口的信息都将被显示。
IsWindow:
函数功能:该函数确定给定的窗口句柄是否标示一个已存在的窗口。
函数原型:BOOL
IsWindow(HWND hWnd);
参数:
hWnd:被测试窗口的句柄。
返回值:
如果窗口句柄标识了一个已存在的窗口,返回值为TURE;如果窗口句柄未标识一个已存在窗口,返回值为FALSE。
FindWindow:
HWND
FindWindow(LPCSTR lpClassName,LPCSTR lpWindowName
)
参数:
lpClassName
指向一个以null结尾的、用来指定类名的字符串或一个可以确定类名字符串的原子。如果这个参数是一个原子,那么它必须是一个在调用此函数前已经通过GlobalAddAtom函数创建好的全局原子。这个原子(一个16bit的值),必须被放置在lpClassName的低位字节中,lpClassName的高位字节置零。
lpWindowName
指向一个以null结尾的、用来指定窗口名(即窗口标题)的字符串。如果此参数为NULL,则匹配所有窗口名。
返回值:
如果函数执行成功,则返回值是拥有指定窗口类名或窗口名的窗口的句柄。
如果函数执纳蠢仔行失败,则返回值为
NULL
。可以通过调用GetLastError函数获得更加详细的错误信息。
SetWindowText:
至于窗口标题的改变,我们可以通过SetWindowText来实现
注:如果窗口本身属性是不显示标题的,这个函数的调用不会影响窗口属性。
//Set
title for application’s main frame window .
AfxGetMainWnd ( ) ->
SetWindowText (_T("Application title") )
//Set title for View’s MDI child
frame window .
GetParentFrame ( ) ->SetWindowText ("_T ("MDI Child Frame
new title") )
//Set title for dialog’s push button control.
GetDigitem
(IDC_BUTTON) ->SetWindowText (_T ("Button new title ") )
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]static extern IntPtr FindWindow(string lpClassName,string lpWindowName)
[DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
extern static IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow)
[STAThread]
static void Main(string[] args)
{
string path = "..\\..\\..\\AUT3\\bin\\Debug\\AUT3.exe"
Process p = Process.Start(path)
if (p==null)
Console.WriteLine("Warning:process may already exist")
Console.WriteLine("Finding main window handle")
IntPtr mwh = FindMainWindowHandle("Form1", 100, 25)
Console.WriteLine("Handle to main window is " + mwh)
//有伍液册名字控件句柄
Console.WriteLine("腔宏Findding handle to textbox1")
IntPtr tb = FindWindowEx(mwh, IntPtr.Zero, null, "<enter color>")
if (tb == IntPtr.Zero)
throw new Exception("Unable to find textbox1")
else
Console.WriteLine("Handle to textbox1 is " + tb)
Console.WriteLine("Findding handle to button1")
IntPtr butt = FindWindowEx(mwh, IntPtr.Zero, null, "button1")
if (butt == IntPtr.Zero)
throw new Exception("Unable to find button1")
else
Console.WriteLine("Handle to button1 is " + butt)
//没有名字或者重名控件
Console.WriteLine("埋胡Findding handle to listbox1")
IntPtr lb = FindWindowByIndex(mwh,3)
if (lb == IntPtr.Zero)
throw new Exception("Unable to find listbox1")
else
Console.WriteLine("Handle to listbox1 is " + lb)
}
方法:
//通过索引查找相应控件句柄
static IntPtr FindWindowByIndex(IntPtr hwndParent,int index)
{
if (index == 0)
{
return hwndParent
}
else
{
int ct = 0
IntPtr result = IntPtr.Zero
do
{
result = FindWindowEx(hwndParent,result,null,null)
if (result != IntPtr.Zero)
{
++ct
}
} while (ct<index&&result!=IntPtr.Zero)
return result
}
}
//获得待测程序主窗体句柄
private static IntPtr FindMainWindowHandle(string caption,int delay,int maxTries)
{
IntPtr mwh = IntPtr.Zero
bool formFound = false
int attempts = 0
while (!formFound &&attempts <maxTries)
{
if (mwh == IntPtr.Zero)
{
Console.WriteLine("Form not yet found")
Thread.Sleep(delay)
++attempts
mwh = FindWindow(null, caption)
}
else
{
Console.WriteLine("Form has been found")
formFound = true
}
}
if (mwh == IntPtr.Zero)
throw new Exception("Could not find main window")
else
return mwh
}
1、GetWindowLong是获得有关指定窗口的信息,这个信息包括窗口扩展风格、标识、父句柄、风格等;通过第二个参数在控制的2、GWL_EXSTYLE 获得扩展窗口风格。
GWL_HINSTANCE 获得应用事例的句柄迟空。袜盯
GWL_HWNDPARENT 如果父窗口存在,获得父窗口句柄。
GWL_ID 获得窗口标识。
GWL_STYLE 获得窗口风格。
GWL_USERDATA 获得与窗口有关的32位值。每一个窗口均有一个由创建该窗口的应用程序使用的32位值。
GWL_WNDPROC 获得窗口过程的地址告旦和,或代表窗口过程的地址的句柄。
同样SetWindowLong也需要通过参数设定不同的信息;
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)