// // MsgBoxEx - how to customize a standard message box // Written by J Brown 2002 // Freeware //#include <windows.h> #include <tchar.h>TCHAR szContents[] = _T("A simple message box"); TCHAR szTitle[] = _T("Test");HHOOK hMsgBoxHook;LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam) { TCHAR ach[40]; HWND hwnd; HWND hwndButton; if(nCode < 0) return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); switch(nCode) { case HCBT_ACTIVATE: // Get handle to the message box! hwnd = (HWND)wParam; SetWindowText(hwnd, _T("Intercepted!")); hwndButton = GetDlgItem(hwnd, IDOK); SetWindowText(hwndButton, _T("Click me!")); break; } return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); }// // Wrapper around the standard MessageBox function // int MsgBoxEx(HWND hwnd, TCHAR *szText, TCHAR *szCaption, UINT uType) { int retval; // Install a window hook, so we can intercept the message-box // creation, and customize it hMsgBoxHook = SetWindowsHookEx( WH_CBT, CBTProc, NULL, GetCurrentThreadId() // Only install for THIS thread!!! ); // Display a standard message box // It doesn't matter that we have no parent window or a // message-loop, because MessageBox has it's own message loop. retval = MessageBox(hwnd, szText, szCaption, uType); // remove the window hook UnhookWindowsHookEx(hMsgBoxHook); return retval; } int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) { MsgBoxEx(NULL, szContents, szTitle, MB_OK | MB_ICONINFORMATION); return 0; }
for example:
BOOL CALLBACK EnumWindowsProc(HWND hwnd,LPARAM lParam)
{
char lpWinTitle[256];
::GetWindowText(hwnd,lpWinTitle,256-1);
CString m_strTitle;
m_strTitle.Format("%s",lpWinTitle);
if(m_strTitle.Find("报警信息配置平台")!=-1)
{
SendMessage(hwnd,WM_CLOSE,0,0);
return FALSE;
}
return TRUE;
}
BOOL CApp::InitInstance()
{
...
EnumWindows((WNDENUMPROC)EnumWindowsProc,NULL);
...
}
// MsgBoxEx - how to customize a standard message box
// Written by J Brown 2002
// Freeware
//#include <windows.h>
#include <tchar.h>TCHAR szContents[] = _T("A simple message box");
TCHAR szTitle[] = _T("Test");HHOOK hMsgBoxHook;LRESULT CALLBACK CBTProc(int nCode, WPARAM wParam, LPARAM lParam)
{
TCHAR ach[40];
HWND hwnd;
HWND hwndButton; if(nCode < 0)
return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam); switch(nCode)
{
case HCBT_ACTIVATE: // Get handle to the message box!
hwnd = (HWND)wParam; SetWindowText(hwnd, _T("Intercepted!")); hwndButton = GetDlgItem(hwnd, IDOK);
SetWindowText(hwndButton, _T("Click me!")); break;
} return CallNextHookEx(hMsgBoxHook, nCode, wParam, lParam);
}//
// Wrapper around the standard MessageBox function
//
int MsgBoxEx(HWND hwnd, TCHAR *szText, TCHAR *szCaption, UINT uType)
{
int retval; // Install a window hook, so we can intercept the message-box
// creation, and customize it
hMsgBoxHook = SetWindowsHookEx(
WH_CBT,
CBTProc,
NULL,
GetCurrentThreadId() // Only install for THIS thread!!!
); // Display a standard message box
// It doesn't matter that we have no parent window or a
// message-loop, because MessageBox has it's own message loop.
retval = MessageBox(hwnd, szText, szCaption, uType); // remove the window hook
UnhookWindowsHookEx(hMsgBoxHook); return retval;
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int
nShowCmd)
{
MsgBoxEx(NULL, szContents, szTitle, MB_OK | MB_ICONINFORMATION);
return 0;
}
你可以使用WH_CALLWND,监控WM_SHOWWINDOW message,这样可以监控到所有的窗口
my code:
LRESULT __declspec(dllexport)__stdcall CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
PCWPSTRUCT pcw=(PCWPSTRUCT) lParam;
if(nCode>=0 && pcw && pcw->hwnd)
{
// if(pcw->message==WM_LBUTTONDOWN)
// return 0;
if(pcw->message==WM_SHOWWINDOW)
{
//HWND hparent=GetTopParent(pcw->hwnd);
HWND hwnd=(HWND)pcw->hwnd;
}
}
}
你可以使用WH_CALLWND,监控WM_SHOWWINDOW message,这样可以监控到所有的窗口
my code:
LRESULT __declspec(dllexport)__stdcall CALLBACK CallWndProc(int nCode,WPARAM wParam,LPARAM lParam)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
PCWPSTRUCT pcw=(PCWPSTRUCT) lParam;
if(nCode>=0 && pcw && pcw->hwnd)
{
// if(pcw->message==WM_LBUTTONDOWN)
// return 0;
if(pcw->message==WM_SHOWWINDOW)
{
//HWND hparent=GetTopParent(pcw->hwnd);
HWND hwnd=(HWND)pcw->hwnd;
}
}
}