Close Modal Form when mouse click outside form area

痴心易碎 提交于 2019-12-12 01:34:34

问题


I would like to close a modal form when the user clicks outside (anywhere on the computer desktop) the modal form. How can we do this as a modal form is not meant to lose focus.


回答1:


You need to hook mouse (and keyboard if required) and capture their events. Then check if the click happened outside the form (and area). If yes, flag a sign which can be read by the model form that it can close down.

Algo:

  1. Hook mouse click event.

  2. When callback function is called, check for the click position - if it's inside your form or not (you might need to translate the locations to Desktop locations - I hope you know how to!)

  3. If the point is outside the form, set a flag (boolean or anything that makes you happy). Make sure the form can read the flag somehow.

  4. Trigger an event for form to capture. In it's handler read the flag status. If true, close/unload the form.

This page will tell you technical details and functions.




回答2:


I don't think you need to make it modal... then you can take siride's option of closing it on the Deactivate event.

The reason you don't need to make it modal: The first time you display it, it will have the focus and be topmost. Modal prevents you from clicking somewhere else, but you want to be able to click somewhere else... and when you do, the form goes away, so there are no modal needs.




回答3:


Here is the exact solution for your question:

http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C#



来源:https://stackoverflow.com/questions/3856301/close-modal-form-when-mouse-click-outside-form-area

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!