Windows MFC 全域模態 實現

來源:互聯網
上載者:User

標籤:mfc 鉤子 模態

windows本身沒有提供針對整個案頭全域模態對話方塊的實現方式,需要自己實現。

兩種方法:

1、彈出一個全螢幕透明的對話方塊,再在它的基礎上彈出模態框

2、當滑鼠在彈出框外時,禁用鍵盤、滑鼠所有功能


第一種不好的是可能有閃一下的效果,影響體驗;而且還要讓修改密碼框在它上面,還要考慮到這個視窗的釋放。

第二種相對來說較方便,但是需要多一個動態庫來實現全域鉤子。


筆者只實現了第二種方法。


實現方法:

// 用此函數判斷是否在內,以決定是否使能滑鼠BOOL CMFC_MouseMoveTest2Dlg::IsInWindow(){CPoint curPoint;::GetCursorPos(&curPoint);CRect wndRect;this->GetWindowRect(wndRect);if ( curPoint.x < wndRect.right && curPoint.x > wndRect.left && curPoint.y < wndRect.bottom && curPoint.y > wndRect.top ){return TRUE;}return FALSE;}
建立一個定時器,在定時器內調用動態庫裡的鉤子:

void CMFC_MouseMoveTest2Dlg::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultSetHook(!IsInWindow());CDialog::OnTimer(nIDEvent);}
鉤子的代碼:

#include <windows.h>#include <stdio.h>#define MYAPI extern "C" _declspec(dllexport)  //匯出函式宣告,extern "C"要放在最前面HHOOK hHook = NULL;LRESULT CALLBACK MouseProc(int code, WPARAM wParam, LPARAM lParam){if (code >= 0)return 1;                              //訊息不再傳遞個下一個HOOK子程,也不會再發送給目的視窗elsereturn CallNextHookEx(hHook, HC_ACTION, wParam, lParam);}MYAPI int SetHook(BOOL bHook){if( TRUE == bHook ){if( NULL == hHook){hHook = SetWindowsHookEx(WH_MOUSE, MouseProc, GetModuleHandleA("G_Hook_Dll.dll"), 0);if (NULL == hHook){return -1;}}}else{if(hHook){if (UnhookWindowsHookEx(hHook) == FALSE){return -1;}else{hHook = NULL;}}}return 0;}

樣本工程源碼


Windows MFC 全域模態 實現

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.