Windows MFC Global Modal implementation

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.