透明度和鼠标事件WinForms [英] Transparency and Mouse Events WinForms

查看:193
本文介绍了透明度和鼠标事件WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前拥有一个完全透明背景的表单。目前,当用户悬停在表单上的控件上时,我必须显示在论坛顶部的图片框。





将鼠标悬停在PictureBox上可触发 MouseEnter 事件正确设置按钮可见状态为true,而 MouseLeave 事件设置为false。这些按钮本身具有相同的 MouseEnter MouseLeave 事件,但Winforms将鼠标事件传递到窗体上的任何空间形式是透明的(我的图像使用在按钮中也是透明的)每当我去点击按钮,它们消失,因为表单认为鼠标有左两个按钮或表单。有没有人知道任何方式阻止事件的传递?



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

  // Form Constructor! 
// map = picturebox,this = form,move =第一个按钮,attach =第二个按钮
public Detached(PictureBox map)
{
InitializeComponent();
doEvents(map,this,this.attach,this.move);
}

// doEvents方法!我使用这个将事件添加到窗体上的所有控件
//上!
void doEvents(params Control [] itm)
{
Control [] ctls = this.Controls.Cast< Control>()。Union(itm).ToArray();
foreach(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;
};
}
}


解决方案

给汉斯·帕斯特指着我正确的方向。我最终创建一个线程,检查鼠标是否在每50ms的边界。

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

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

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

感谢:)


I currently have a form that has a completely transparent background. At the moment I have to picture boxes that appear at the top of the forum when a user hovers over a control on the form.

Hovering over the PictureBox triggers the MouseEnter event properly and sets the buttons Visible state to true and the MouseLeave event sets it to false. The buttons themselves have the same MouseEnter and MouseLeave events but as Winforms passes mouse events to the form under any space on the form that is transparent (my images used in the buttons are transparent around them as well) whenever I go to click the buttons, they disappear as the form thinks that the mouse has "left" both the buttons or the form. Does anyone know ANY way of stopping the pass-through of the events?

Some code you ask? Some code you get :)

// 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;
        };
    }
}

解决方案

Thanks to Hans Passant for pointing me in the correct direction. I ended up creating a thread that checks if the mouse is in the bounds every 50ms.

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;
}

Thanks :)

这篇关于透明度和鼠标事件WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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