最近,本人写了一个鼠标钩子,想获取任何控件的父窗体的标题,但是有些窗体的控件的父窗体不一定是窗体,有时是控件,所以需要找到父窗体。为此,我采用了while循环,利用getparent()函数,当到达桌面时(我判断桌面的句柄为NULL)停止.我认为窗体的父窗体应该是桌面了,不知是否是这样。但是结果却不是这样,当鼠标指向某个窗体的控件(多个父)时,却没有取出。很奇怪!
我也曾采用桌面的句柄((HWND)Ox00010014)来判断也是不可,到底是怎么回事?请各位高手帮忙了!
LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *)lparam;
if (nCode>=0)
{
HWND glhTargetWnd = GetParent(WindowFromPoint(pMouseHook->pt)); //取得控件的父句柄
HWND ghlParentWnd; //父窗体句柄
while(1)
{ ghlParentWnd=GetParent(glhTargetWnd);
if( ghlParentWnd == NULL || ghlParentWnd == glhTargetWnd ) //是否为桌面
break;
glhTargetWnd = ghlParentWnd;
}
if(glhTargetWnd!=glhPrevTarWnd)
{
char szCaption[256];
GetWindowText(glhTargetWnd,szCaption,100);
//acquire the caption
if(IsWindow(glhDisplayWnd))
SendMessage(glhDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szCaption);
glhPrevTarWnd=glhTargetWnd;
//save the caption
}
}
return CallNextHookEx(glhHook,nCode,wparam,lparam);
//continue to transfer the message
}
我也曾采用桌面的句柄((HWND)Ox00010014)来判断也是不可,到底是怎么回事?请各位高手帮忙了!
LRESULT WINAPI MouseProc(int nCode,WPARAM wparam,LPARAM lparam)
{
LPMOUSEHOOKSTRUCT pMouseHook=(MOUSEHOOKSTRUCT FAR *)lparam;
if (nCode>=0)
{
HWND glhTargetWnd = GetParent(WindowFromPoint(pMouseHook->pt)); //取得控件的父句柄
HWND ghlParentWnd; //父窗体句柄
while(1)
{ ghlParentWnd=GetParent(glhTargetWnd);
if( ghlParentWnd == NULL || ghlParentWnd == glhTargetWnd ) //是否为桌面
break;
glhTargetWnd = ghlParentWnd;
}
if(glhTargetWnd!=glhPrevTarWnd)
{
char szCaption[256];
GetWindowText(glhTargetWnd,szCaption,100);
//acquire the caption
if(IsWindow(glhDisplayWnd))
SendMessage(glhDisplayWnd,WM_SETTEXT,0,(LPARAM)(LPCTSTR)szCaption);
glhPrevTarWnd=glhTargetWnd;
//save the caption
}
}
return CallNextHookEx(glhHook,nCode,wparam,lparam);
//continue to transfer the message
}
建议使用Win32 API
然后用FindWindow方法找到桌面,这才是桌面呢!
以前我做的时候好象还要饶个弯子!
你去认真观察一下!