当鼠标在表单区域外单击时,关闭模态表单 [英] Close Modal Form when mouse click outside form area
问题描述
当用户在外部(在计算机桌面上的任何地方)单击模式窗体时,我想关闭模式窗体.作为模态形式,我们如何做到这一点并不意味着失去关注.
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.
推荐答案
您需要钩住鼠标(和键盘)(如果需要)并捕获其事件.然后检查单击是否发生在表单(和区域)之外.如果是,请标记一个可以通过模型表格读取的标志,以将其关闭.
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.
算法:
-
挂钩鼠标单击事件.
Hook mouse click event.
调用回调函数时,请检查单击位置-是否在表单内(您可能需要将位置转换为桌面位置-希望您知道!)
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!)
如果该点位于表单之外,则设置一个标志(布尔值或使您满意的任何东西).确保表单可以以某种方式读取标志.
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.
触发一个事件以捕获表单.在它的处理程序中读取标志状态.如果为true,请关闭/卸载表单.
Trigger an event for form to capture. In it's handler read the flag status. If true, close/unload the form.
这篇关于当鼠标在表单区域外单击时,关闭模态表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!