标签:style blog http io ar color os sp for
TopWnd为一款精小实用的绿色软件,可以置顶大部分的窗口,一键置顶,一键取消!
贴上下载地址:
我一直都在实用这个小工具,今日好奇,决定一探其内部究竟如何实现。
网上搜索一番,没有找到这个小工具的源码。
无奈只好一步步的自己去实现它。
最终用MFC+全局钩子成功模仿了一个相差无几的程序。
在dll程序中,自定义一个消息,如下所示:
BOOL WINAPI StartHookMouse(HWND hWnd) { g_hWnd=hWnd; hhkMouse=::SetWindowsHookEx(WH_MOUSE_LL,LowLevelMouseProc,g_hInstDll,0); if (hhkMouse==NULL) { return FALSE; } else { return TRUE; } }
LRESULT CALLBACK LowLevelMouseProc( int nCode, // hook code WPARAM wParam, // message identifier LPARAM lParam // mouse coordinates ) { //有鼠标消息时,将其发给主程序 if (g_hWnd!=NULL&&nCode==HC_ACTION) { ::SendMessage(g_hWnd,WM_MYMSG,wParam,lParam); } return CallNextHookEx(hhkMouse,nCode,wParam,lParam); }
LRESULT CHookMouseTestDlg::OnMyMsg(WPARAM wParam,LPARAM lParam){}
1 //加载钩子DLL 2 g_hInstDll=LoadLibrary(_T("MouseHook.dll")); 3 if(g_hInstDll==NULL) 4 { 5 AfxMessageBox(_T("no MouseHook.dll")); 6 return; 7 } 8 9 typedef BOOL (CALLBACK *StartHookMouse)(HWND hWnd); 10 StartHookMouse StartHook; 11 12 StartHook=(StartHookMouse)::GetProcAddress(g_hInstDll,"StartHookMouse"); 13 if(StartHook==NULL) 14 { 15 AfxMessageBox(_T("func StartHookMouse not found!")); 16 return; 17 } 18 19 //将本程序的句柄传给 钩子DLL,用来通信 20 if (StartHook(this->m_hWnd)) 21 { 22 SetDlgItemText(IDC_STATE,STATE1); 23 } 24 else 25 { 26 SetDlgItemText(IDC_STATE,STATE2); 27 }
typedef VOID (CALLBACK *StopHookMouse)(); StopHookMouse StopHook; g_hInstDll = LoadLibrary(_T("MouseHook.dll")); StopHook = (StopHookMouse)::GetProcAddress(g_hInstDll,"StopHookMouse"); if( StopHook == NULL ){ SetDlgItemText(IDC_STATE,STATE2); } StopHook(); if(g_hInstDll != NULL){ ::FreeLibrary(g_hInstDll); }
1 //启动置顶 2 BOOL openORclose = FALSE; 3 void CTopWndDlg::OnBnClickedOpen() 4 { 5 OpenHook(); 6 GetDlgItem(ID_OPEN)->EnableWindow(FALSE); 7 openORclose = TRUE; 8 } 9 10 //恢复置顶 11 void CTopWndDlg::OnBnClickedClose() 12 { 13 OpenHook(); 14 GetDlgItem(ID_CLOSE)->EnableWindow(FALSE); 15 openORclose = FALSE; 16 }
1 //处理来钩子的信息,置顶与恢复的实现 2 LRESULT CTopWndDlg::OnMyMsg(WPARAM wParam,LPARAM lParam){ 3 CString str; 4 static PMSLLHOOKSTRUCT mouseHookStruct; 5 mouseHookStruct = (PMSLLHOOKSTRUCT)lParam; 6 if( wParam == WM_LBUTTONDOWN){ 7 HWND hWnd=::WindowFromPoint(mouseHookStruct->pt); 8 TCHAR title[MAX_PATH]; 9 ::GetWindowText(hWnd,title,MAX_PATH); 10 if(openORclose){ 11 // HWND_TOPMOST 窗口置顶 SWP_NOMOVE | SWP_NOSIZE 不改变窗口大小和位置 12 ::SetWindowPos( hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); 13 GetDlgItem(ID_OPEN)->EnableWindow(TRUE); 14 str.Format(_T("%s %s"),STATE3,title); 15 SetDlgItemText(IDC_STATE, str); 16 } 17 else{ 18 ::SetWindowPos( hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE ); 19 GetDlgItem(ID_CLOSE)->EnableWindow(TRUE); 20 str.Format(_T("%s %s"),STATE4,title); 21 SetDlgItemText(IDC_STATE,str); 22 } 23 CloseHook(); 24 } 25 return 1; 26 }
标签:style blog http io ar color os sp for
原文地址:http://www.cnblogs.com/3chimenea/p/4127584.html