Windows itself does not provide an implementation for the entire Desktop Global modal dialog box, which needs to be implemented on its own.
Two methods:
1, pop up a full-screen transparent dialog box, and then on its basis to pop-up modal box
2. Disable all functions of the keyboard and mouse when the mouse is outside the popup box
The first bad thing is that it may have a flashing effect, affect the experience, and let's change the password box above it, and take into account the release of the window.
The second is relatively convenient, but requires more than one dynamic library to implement global hooks.
The author only realizes the second method.
Implementation method:
Use this function to determine whether or not to make mouse 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;}
Create a new timer to invoke the hooks in the dynamic library within the timer:
void Cmfc_mousemovetest2dlg::ontimer (UINT nidevent) {//Todo:add your message handler code here and/or call Defaultsethoo K (! Isinwindow ()); Cdialog::ontimer (nidevent);}
The code of the hook:
#include <windows.h> #include <stdio.h> #define MYAPI extern "C" _declspec (dllexport) //export function declaration, extern " C "to be placed at the front hhook hhook = NULL; LRESULT CALLBACK mouseproc (int code, WPARAM WPARAM, LPARAM LPARAM) {if (Code >= 0) return 1; The message no longer passes through the next hook and is no longer sent to the destination window Elsereturn CallNextHookEx (hhook, Hc_action, WParam, LParam);} MyApi int sethook (BOOL bhook) {if (TRUE = = Bhook) {if (NULL = = hhook) {hhook = SetWindowsHookEx (Wh_mouse, Mouseproc, Getmodu Lehandlea ("G_hook_dll.dll"), 0); if (NULL = = hhook) {return-1;}}} Else{if (hhook) {if (UnhookWindowsHookEx (hhook) = = FALSE) {return-1;} Else{hhook = NULL;}}} return 0;}
Sample project Source code
Windows MFC Global Modal implementation