为添加到窗体的控件除外,创建一个半透明或透明窗口窗口trasparent到鼠标事件

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

所以这是我用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,如果这有助于获得结果。

谢谢你的时间。

尝试设置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); } } 

希望这对你有所帮助。

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

有关更多信息,请参阅以下内容: 接受答案的工作原理如何?