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 ) //判斷復(fù)選框
{
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); //設(shè)置窗口信息
//設(shè)置窗口對話框函數(shù)地址
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); //設(shè)置窗口信息
WndProc = (WNDPROC) SetWindowLong( hWnd, GWL_WNDPROC, (LONG) DefWindowProc1); //設(shè)置窗口函數(shù)地址
}
}
}
}
return CallNextHookEx( hWndHook, nCode, wParam, lParam );
}
定義一個安裝鉤子的函數(shù)RunHook,代碼如下:
BOOL RunHook( HMODULE hModule, DWORD dwThreadID)
{
hWndHook = SetWindowsHookEx(
WH_CALLWNDPROC, (HOOKPROC) HOOKProc, hModule, dwThreadID ); //安裝鉤子
return TRUE;
}
定義一個卸載鉤子的函數(shù)StopHook,代碼如下:
BOOL StopHook()
{
UnhookWindowsHookEx(hWndHook); //卸載鉤子
return TRUE;
}