// Install or uninstall thread keyboard hook void CCppWindowsHookDlg::OnBnClickedSethookthread() { CString strButtonText; GetDlgItem(IDC_SETHOOKTHREAD)->GetWindowText(strButtonText); if(strButtonText == "Set Hook(Thread)") { // Install Hook if(!SetKeyboardHook(TRUE, ::GetCurrentThreadId(), m_hWnd)) { AfxMessageBox(L"Fail to Install Hook!"); return; } GetDlgItem(IDC_SETHOOKTHREAD)->SetWindowText(L"Unset Hook(Thread)"); GetDlgItem(IDC_SETHOOK)->EnableWindow(FALSE); GetDlgItem(IDC_SETHOOKINPUT)->EnableWindow(FALSE); } else { if(!SetKeyboardHook(FALSE)) { AfxMessageBox(L"Fail to Install Hook!"); return; } GetDlgItem(IDC_SETHOOKTHREAD)->SetWindowText(L"Set Hook(Thread)"); GetDlgItem(IDC_SETHOOK)->EnableWindow(TRUE); GetDlgItem(IDC_SETHOOKINPUT)->EnableWindow(TRUE); } }
BOOL CCppWindowsHookApp::InitInstance() { // InitCommonControlsEx() is required on Windows XP if an application // manifest specifies use of ComCtl32.dll version 6 or later to enable // visual styles. Otherwise, any window creation will fail. INITCOMMONCONTROLSEX InitCtrls; InitCtrls.dwSize = sizeof(InitCtrls); // Set this to include all the common control classes you want to use // in your application. InitCtrls.dwICC = ICC_WIN95_CLASSES; InitCommonControlsEx(&InitCtrls); CWinAppEx::InitInstance(); AfxEnableControlContainer(); CCppWindowsHookDlg dlg; m_pMainWnd = &dlg; INT_PTR nResponse = dlg.DoModal(); //uninstall hook SetKeyboardHook(FALSE); SetLowKeyboardHook(FALSE); // Since the dialog has been closed, return FALSE so that we exit the // application, rather than start the application's message pump. return FALSE; }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { // Low-level keyboard hook SetKeyboardHook(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0); // Keyboard hook //SetKeyboardHook(WH_KEYBOARD, KeyboardProc, (HINSTANCE)NULL, GetCurrentThreadId()); return 0; }
// // creates a new balloon window // Parameters: // strTitle | Title of balloon // strContent | Content of balloon // ptAnchor | point tail of balloon will be "anchor"ed to // unOptions | One or more of: // : unCLOSE_ON_LBUTTON_UP | closes window on WM_LBUTTON_UP // : unCLOSE_ON_MBUTTON_UP | closes window on WM_MBUTTON_UP // : unCLOSE_ON_RBUTTON_UP | closes window on WM_RBUTTON_UP // : unCLOSE_ON_LBUTTON_DOWN | closes window on WM_LBUTTON_DOWN // : unCLOSE_ON_MBUTTON_DOWN | closes window on WM_MBUTTON_DOWN // : unCLOSE_ON_RBUTTON_DOWN | closes window on WM_RBUTTON_DOWN // : unCLOSE_ON_MOUSE_MOVE | closes window when user moves mouse past threshhold // : unCLOSE_ON_KEYPRESS | closes window on the next keypress message sent to this thread. (!!! probably not thread safe !!!) // : unDELETE_THIS_ON_CLOSE | deletes object when window is closed. Used by LaunchBalloon(), use with care // : unSHOW_CLOSE_BUTTON | shows close button in upper right // : unSHOW_INNER_SHADOW | draw inner shadow in balloon // : unSHOW_TOPMOST | place balloon above all other windows // : unDISABLE_FADE | disable the fade-in/fade-out effects (overrides system and user settings) // : unDISABLE_FADEIN | disable the fade-in effect // : unDISABLE_FADEOUT | disable the fade-out effect // pParentWnd | Parent window. If NULL will be set to AfxGetMainWnd() and anchor to screen // strURL | If not empty, when the balloon is clicked ShellExecute() will // | be called, with strURL passed in. // unTimeout | If not 0, balloon will automatically close after unTimeout milliseconds. // hIcon | If not NULL, the icon indicated by hIcon will be displayed at top-left of the balloon. // // Returns: // TRUE if successful, else FALSE // BOOL CBalloonHelp::Create(const CString& strTitle, const CString& strContent, const CPoint& ptAnchor, unsigned int unOptions, CWnd* pParentWnd /*=NULL*/, const CString strURL /*= ""*/, unsigned int unTimeout /*= 0*/, HICON hIcon /*= NULL*/) { m_strContent = strContent; SetAnchorPoint(ptAnchor, pParentWnd); m_unOptions = unOptions; m_strURL = strURL; m_unTimeout = unTimeout; if ( NULL != hIcon ) SetIcon(hIcon); pParentWnd = GetSafeOwner(pParentWnd); if (NULL == pParentWnd) return FALSE; // if no fonts set, use defaults if ( NULL == m_pContentFont ) { m_pContentFont = new CFont; if ( !m_pContentFont->CreateStockObject(DEFAULT_GUI_FONT) ) return FALSE; } // title font defaults to bold version of content font if ( NULL == m_pTitleFont ) { m_pTitleFont = new CFont; LOGFONT LogFont; m_pContentFont->GetLogFont(&LogFont); LogFont.lfWeight = FW_BOLD; if ( !m_pTitleFont->CreateFontIndirect(&LogFont) ) return FALSE; } if ( !GetClassAtom() ) // couldn't register class return FALSE; // check system settings: if fade effects are disabled or unavailable, disable here too BOOL bFade = FALSE; ::SystemParametersInfo(SPI_GETTOOLTIPANIMATION, 0, &bFade, 0); if (bFade) ::SystemParametersInfo(SPI_GETTOOLTIPFADE, 0, &bFade, 0); if (!bFade || NULL == m_fnAnimateWindow) m_unOptions |= unDISABLE_FADE; // create invisible at arbitrary position; then position, set region, and finally show // the idea with WS_EX_TOOLWINDOW is, you can't switch to this using alt+tab DWORD dwExStyle = WS_EX_TOOLWINDOW; if ( m_unOptions&unSHOW_TOPMOST ) // make topmost, if requested dwExStyle |= WS_EX_TOPMOST; if ( !CreateEx(dwExStyle, _T("BalloonHelpClass"), strTitle, WS_POPUP, CRect(0,0,10,10), pParentWnd, 0, NULL) ) return FALSE; PositionWindow(); if ( (m_unOptions&unCLOSE_ON_MOUSE_MOVE) ||(m_unOptions&unCLOSE_ON_LBUTTON_UP) ||(m_unOptions&unCLOSE_ON_LBUTTON_DOWN) ||(m_unOptions&unCLOSE_ON_MBUTTON_UP) ||(m_unOptions&unCLOSE_ON_MBUTTON_DOWN) ||(m_unOptions&unCLOSE_ON_RBUTTON_UP) ||(m_unOptions&unCLOSE_ON_RBUTTON_DOWN) ) { ::GetCursorPos(&m_ptMouseOrig); SetMouseHook(); } // these need to take effect even if the window receiving them // is not owned by this process. So, if this process does not // already have the mouse captured, capture it! if ( (m_unOptions&unCLOSE_ON_LBUTTON_UP) ||(m_unOptions&unCLOSE_ON_MBUTTON_UP) ||(m_unOptions&unCLOSE_ON_RBUTTON_UP) ) { // no, i don't particularly need or want to deal with a situation // where a balloon is being created and another program has captured // the mouse. If you need it, it shouldn't be too hard, just do it here. if ( NULL == GetCapture() ) SetCapture(); } if ( m_unOptions&unCLOSE_ON_KEYPRESS ) SetKeyboardHook(); ShowBalloon(); return TRUE; }