您好,我试图获得像玻璃一样的透明形式,它可以使点击和每个鼠标事件传递到玻璃后面的窗口或项目。
这是我用 Windows 窗体编写的代码:
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);
}
}
}
这段代码的问题是整个窗口通过不透明度变得透明,但按钮或滑块等控件不保留可点击性。
So I need help to让它变得更好。
1)保留控件完全不透明(不需要但很重要)
2)保留控件的可点击性和可操作性 (MUST)
我接受任何解决方案,甚至将项目更改为 WPF
尝试设置Form.TransparencyKey 属性 http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey.aspxClickThroughForm 的值以匹配表单 BackColor。
当 ClickThroughForm 在另一个窗体上设置为 TopMost 时,我对此进行了测试,我可以触发 Button 事件,并且 TrackBar 控件似乎可以正常工作。
Note:使用此方法,由于其透明度,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);
}
}
希望这对您有帮助。
我注意到您是新用户,如果您在网站上提出的此问题或任何其他问题提供了您正在寻找的答案,请记住接受答案。
请参阅以下内容了解更多信息:接受答案如何运作? http://msdn.microsoft.com/en-us/library/system.windows.forms.form.transparencykey.aspx
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)