在父窗体中检测鼠标单击,但在子窗体之外 [英] detect mouse click in parent form, but outside child form

查看:22
本文介绍了在父窗体中检测鼠标单击,但在子窗体之外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个 UI,其中父窗体通过 showDialog() 加载子窗体.每当我单击子窗体之外但父窗体内的任何位置时,子窗体都会关闭.单击父窗体之外的任何位置只会导致正常的alt-tab"操作.我该怎么做?

i am trying to implement a UI whereby the parent form loads a child form through showDialog(). the child form would be closed whenever i click anywhere outside the child form, but inside the parent form. clicking anywhere outside the parent form would only cause a normal "alt-tab" action. how do i do this?

推荐答案

如果表单中没有任何控件(例如,如果您正在查看图片).然后你就可以捕获鼠标了:

If you don't have any controls in the form (if you're viewing a picture for example). Then you can just capture the mouse:

    protected override void OnLoad( EventArgs e )
    {
        base.OnLoad( e );
        this.Capture = true;
    }

然后,如果点击在表单之外,您只需检查 OnMouseDown.

And after that, you just check in OnMouseDown if the click is outside your form.

否则,可以使用此代码:

Othewise, this code could be used:

    protected override void WndProc(ref Message m)
    {
        if ( m.Msg==0x86 && (int)m.WParam==0 )
            if ( this.DialogResult==DialogResult.None )
                this.DialogResult = DialogResult.OK;
        base.WndProc (ref m);
    }

它在 Windows XP 中运行良好,但在 Windows 7 中它也发出哔哔声,我还没有调查原因.

It worked great in Windows XP, but in Windows 7 it sounds a beep too, and I haven't investigated why.

这篇关于在父窗体中检测鼠标单击,但在子窗体之外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆