繁体   English   中英

透明度和鼠标事件WinForms

[英]Transparency and Mouse Events WinForms

我目前有一个具有完全透明背景的表格。 目前,当用户将鼠标悬停在窗体上的控件上时,我必须显示出现在论坛顶部的框。

屏幕截图!

将鼠标悬停在PictureBox上会正确触发MouseEnter事件,并将按钮的Visible状态设置为true,而MouseLeave事件将其设置为false。 这些按钮本身具有相同的MouseEnterMouseLeave事件,但是每当我单击按钮时,Winforms都会将鼠标事件传递到透明窗体上任何空格下的窗体(按钮中使用的图像也是透明的)下,当表单认为鼠标同时“按下”了按钮或表单时,它们消失了。 有谁知道阻止事件传递的任何方法?

你问一些代码? 你得到一些代码:)

// Form Constructor!
// map = picturebox, this = form, move = first button, attach = second button
public Detached(PictureBox map)
{
    InitializeComponent();
    doEvents(map, this, this.attach, this.move);
}

// doEvents method! I use this to add the event to all controls
// on the form!
void doEvents(params Control[] itm)
{
    Control[] ctls = this.Controls.Cast<Control>().Union(itm).ToArray();
    foreach (Control ctl in ctls)
    {
        ctl.MouseEnter += (s, o) =>
        {
            this.attach.Visible = true;
            this.move.Visible = true;
        };
        ctl.MouseLeave += (s, o) =>
        {
            this.attach.Visible = false;
            this.move.Visible = false;
        };
    }
}

感谢Hans Passant为我指出正确的方向。 我最终创建了一个线程,每50毫秒检查一次鼠标是否在边界内。

public Detached(PictureBox map)
{
    Thread HoverCheck = new Thread(() =>
    {
        while (true)
        {
            if (this.Bounds.Contains(Cursor.Position))
            {
                ToggleButtons(true);
            }
            else
            {
                ToggleButtons(false);
            }
            Thread.Sleep(50);
        }
    });
    HoverCheck.Start();
}

void ToggleButtons(bool enable)
{
    if (InvokeRequired)
    {
        Invoke(new MethodInvoker(() => ToggleButtons(enable)));
        return;
    }

    this.attach.Visible = enable;
    this.move.Visible = enable;
    this.pictureBox1.Visible = enable;
}

谢谢 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM