供参考:获取的新窗口你自己要判断此窗口是否最顶层窗口,也就是说GetParent()==NULL/**************************************************************************** CBTProc ****************************************************************************/ HHOOK g_hCBTProc=NULL; LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam, LPARAM lParam) { if (nCode<0 ) return CallNextHookEx(g_hCBTProc,nCode,wParam,lParam); //传递钩子信息 switch ( nCode ) { case HCBT_CREATEWND: { //At the time of the HCBT_CREATEWND notification, the window has been created, //but its final size and position may not have been determined and its parent //window may not have been established. It is possible to send messages to the //newly created window, although it has not yet received WM_NCCREATE or WM_CREATE //messages. It is also possible to change the position in the Z order of the newly //created window by modifying the hwndInsertAfter member of the CBT_CREATEWND structure. HWND hWnd=(HWND)wParam; TCHAR ClassName[30]={0}; ::GetClassName(hWnd,ClassName,sizeof(ClassName)/sizeof(TCHAR)); trace(ClassName); if (_tcscmp(ClassName,_T("Notepad"))==0) { } //修改窗口属性,对于部分窗口没有作用,因为有些窗口的位置是之后调用SetWindowPos来确定的 //这里的x,y,cx,cy对应的是CreateWindow里面的参数 // (((CBT_CREATEWND*)lParam)->lpcs)->x=0;
WH_CALLWNDPROC
WH_GETMESSAGE
hook 写在DLL里,这样就能拦截 所有进程创建窗口的消息了
CBTProc
****************************************************************************/
HHOOK g_hCBTProc=NULL;
LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam, LPARAM lParam)
{
if (nCode<0 )
return CallNextHookEx(g_hCBTProc,nCode,wParam,lParam); //传递钩子信息 switch ( nCode )
{
case HCBT_CREATEWND:
{
//At the time of the HCBT_CREATEWND notification, the window has been created,
//but its final size and position may not have been determined and its parent
//window may not have been established. It is possible to send messages to the
//newly created window, although it has not yet received WM_NCCREATE or WM_CREATE
//messages. It is also possible to change the position in the Z order of the newly
//created window by modifying the hwndInsertAfter member of the CBT_CREATEWND structure. HWND hWnd=(HWND)wParam;
TCHAR ClassName[30]={0};
::GetClassName(hWnd,ClassName,sizeof(ClassName)/sizeof(TCHAR));
trace(ClassName);
if (_tcscmp(ClassName,_T("Notepad"))==0)
{ } //修改窗口属性,对于部分窗口没有作用,因为有些窗口的位置是之后调用SetWindowPos来确定的
//这里的x,y,cx,cy对应的是CreateWindow里面的参数
// (((CBT_CREATEWND*)lParam)->lpcs)->x=0;
}
break;
case HCBT_DESTROYWND:
{
trace("HCBT_DESTROYWND");
}
break;
}
return CallNextHookEx(g_hCBTProc,nCode,wParam,lParam); //传递钩子信息
} extern "C" _declspec(dllexport) void SetCBTProcHook()
{
g_hCBTProc=SetWindowsHookEx(WH_CBT,CBTProc,GetModuleHandle("mydll"),0);
if ( g_hCBTProc )
{
trace("SetWindowsHookEx CBTProc succeed");
}
else
trace("SetWindowsHookEx CBTProc failed");}extern "C" _declspec(dllexport) void UnCBTProcHook()
{
if ( UnhookWindowsHookEx(g_hCBTProc) )
{
trace("UnhookWindowsHookEx CBTProc succeed");
}
else
trace("UnhookWindowsHookEx CBTProc failed");
}