创建一个半或透明窗口形式trasparent鼠标事件除了控件添加到表单 [英] Create a semi or transparent Window Form trasparent to mouse events except for Controls added to the form

查看:215
本文介绍了创建一个半或透明窗口形式trasparent鼠标事件除了控件添加到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我试图让一个透明的形式像玻璃,可以使点击率和每一个鼠标事件传递给窗口或项目的玻璃后面。

Hi I was trying to get a transparent form like a glass which could enable clickthrough and every mouse event to pass to the windows or items behind the glass.

所以,这是我与WindowForms写的代码:

So this is the code I wrote with WindowForms:

namespace ClickThroughMe
{
public partial class ClickThroughForm : Form

{
    private int currentWindowStyle;

    public ClickThroughForm()

    {
        InitializeComponent();
    }

    private void ClickThroughForm_Load(object sender, EventArgs e)

    {
        // Grab the Extended Style information for this window and store it.

        currentWindowStyle = WindowLibrary.User32Wrappers.GetWindowLong(this.Handle, User32Wrappers.GWL.ExStyle);

        // Set our window to "transparent", or invisible to the mouse.

        SetFormToTransparent();

        // Make our window the top-most form.

        this.TopMost = true;       
    }

    private void SetFormToTransparent()

    {
        // This creates a new extended style for our window, making it transparent to the mouse.

        User32Wrappers.SetWindowLong(this.Handle, User32Wrappers.GWL.ExStyle,

                                    (User32Wrappers.WS_EX) currentWindowStyle | 

                                     User32Wrappers.WS_EX.Layered |

                                     User32Wrappers.WS_EX.Transparent);
    }
  }
}



这段代码的问题是这整个窗口得到透明的通过混浊,但控件,按钮和滑块不保留可点击

The problem with this code is that whole window get transparent through opacity but controls such buttons or sliders do not retain clickability.

所以我需要帮助做的更好。

1)保留控制完全不透明(不是必要的,但重要的)

1)Retain controls Full Opacity (Not needed but important)

2)的保留控制可点击和操作性(MUST)

我接受任何解决办法,甚至改变该项目WPF 如果这可以帮助获得的结果。

I accept any solution, even changing the project to WPF if this can help getting the result.

感谢您的时间。

推荐答案

尝试设置的 Form.TransparencyKey物业的ClickThroughForm的匹配形式的背景色。

Try setting the Form.TransparencyKey Property of the ClickThroughForm to match the forms BackColor.

我测试的时候ClickThroughForm设置为最顶层了另一种形式,我可以开火键事件和的TrackBar控制似乎正常工作。

I tested this when the ClickThroughForm was set to TopMost over another Form and I could fire Button events and the TrackBar control seemed to function correctly.

注意:使用这种方法没有鼠标事件可以通过ClickThroughForm捕获由于其透明度,如果这是一个要求,那么你就可以忽略这个答案

Note: Using this method no mouse events can be captured by the ClickThroughForm due to its transparency, if this is a requirement then you can disregard this answer.

ClickThroughForm类

public class ClickThroughForm : Form
{
    private System.ComponentModel.IContainer components = null;

    public ClickThroughForm()
    {
        InitializeComponent();
    }

    private void InitializeComponent()
    {
        this.SuspendLayout();
        // 
        // ClickThroughForm
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(300, 300);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "ClickThroughForm";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "ClickThroughForm";

        //Set the TransparencyKey to match the default BackColor of the Form
        this.TransparencyKey = System.Drawing.SystemColors.Control;

        this.ResumeLayout(false);

    }

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }
}



希望这可以帮助你。

Hope this helps you.

我注意到你是新用户,如果你在网站上问这个或任何其他问题提供你正在寻找的答案,记得要接受的答案。

I notice you are a new user, If this or any other questions you ask on the site provide the answers you are looking for, remember to accept the answers.

有关详情请参阅以下内容:如何接受答案的工作?

See the following for more information: How does accepting an answer work?

这篇关于创建一个半或透明窗口形式trasparent鼠标事件除了控件添加到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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