0

嗨,我试图获得像玻璃一样的透明形式,它可以使点击和每个鼠标事件传递到玻璃后面的窗户或物品。

所以这是我用 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);
    }
  }
}

此代码的问题是整个窗口通过不透明度变得透明,但控制此类按钮或滑块不保留可点击性。

所以我需要帮助让它变得更好。

1)保留控件完全不透明度(不需要但很重要)

2)保留控制可点击性和可操作性(必须)

我接受任何解决方案,甚至将项目更改为 WPF,如果这有助于获得结果。

谢谢你的时间。

4

1 回答 1

1

尝试设置 ClickThroughForm 的Form.TransparencyKey 属性以匹配表单 BackColor。

当 ClickThroughForm 设置为 TopMost 而不是另一个 Form 时,我对此进行了测试,我可以触发 Button 事件并且 TrackBar 控件似乎运行正常。

注意:使用此方法,由于 ClickThroughForm 的透明度,它不能捕获鼠标事件,如果这是一个要求,那么您可以忽略此答案。

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

希望这对您有所帮助。

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

有关详细信息,请参阅以下内容:接受答案如何工作?

于 2011-09-03T00:27:15.870 回答