问题
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.
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.
So I need help to make it better.
1)Retain controls Full Opacity (Not needed but important)
2)Retain controls Clickability and Operativity (MUST)
I accept any solution, even changing the project to WPF if this can help getting the result.
Thanks for you time.
回答1:
Try setting the Form.TransparencyKey Property of the ClickThroughForm to match the forms BackColor.
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.
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 Class
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?
来源:https://stackoverflow.com/questions/7290331/create-a-semi-or-transparent-window-form-trasparent-to-mouse-events-except-for-c