LRESULT CALLBACK HOOKProc( int nCode, WPARAM wParam, LPARAM lParam )
{
PCWPSTRUCT wc = (PCWPSTRUCT) lParam;
HWND hWnd = wc->hwnd;
if( hWnd )
{
char ClassName[MAX_PATH] = "";
GetClassName( hWnd, ClassName, MAX_PATH ); //去掉窗口类名
if( strcmp( ClassName, "Button" ) == 0 )
{
CWnd *pWnd = CWnd::FromHandle( hWnd );
if ( pWnd->GetStyle() & BS_RADIOBUTTON ) //判断单选按钮
{
return 1;
}
if ( pWnd->GetStyle() & BS_CHECKBOX ) //判断复选框
{
return 1;
}
WNDPROC WndProc;
WndProc = (WNDPROC) GetWindowLong( hWnd, GWL_WNDPROC );
CDrawButton *pButton=(CDrawButton*)GetWindowLong(hWnd,GWL_USERDATA);
if (pButton != NULL&& pButton->m_Flag==1 )
{
SetWindowLong(hWnd,GWL_USERDATA,0); //设置窗口信息
//设置窗口对话框函数地址
SetWindowLong( hWnd, GWL_WNDPROC, (LONG)pButton->m_OldProc );
pButton->m_OldProc = NULL;
delete pButton;
}
else if (pButton == NULL )
{
if( WndProc !=DefWindowProc1 )
{
pButton = new CDrawButton;
pButton->m_OldProc = WndProc;
SetWindowLong(hWnd,GWL_USERDATA,(long)pButton); //设置窗口信息
WndProc = (WNDPROC) SetWindowLong( hWnd, GWL_WNDPROC, (LONG) DefWindowProc1); //设置窗口函数地址
}
}
}
}
return CallNextHookEx( hWndHook, nCode, wParam, lParam );
}
定义一个安装钩子的函数RunHook,代码如下:
BOOL RunHook( HMODULE hModule, DWORD dwThreadID)
{
hWndHook = SetWindowsHookEx(
WH_CALLWNDPROC, (HOOKPROC) HOOKProc, hModule, dwThreadID ); //安装钩子
return TRUE;
}
定义一个卸载钩子的函数StopHook,代码如下:
BOOL StopHook()
{
UnhookWindowsHookEx(hWndHook); //卸载钩子
return TRUE;
}