我正在尝试创建一个仍会响应鼠标移动和单击事件的透明表单,以显示我们公司的徽标,直到用户单击鼠标,但我遇到了一些困难,因为有时表单和徽标不会'根本不会出现,在其他时候它们会出现,但背景是黑色的。
这是我如何称呼我的表格:
MouseAlertForm maf = new MouseAlertForm(Win32.GetCursorPosition());
maf.Show();
maf.Show()
导致表单根本不显示(与 相同Show(this)
),并maf.ShowDialog()
导致黑色背景。这是MouseAlertForm
.
public sealed partial class MouseAlertForm : Form
{
private Image Logo;
public Point MouseLocation { get; private set; }
public MouseAlertForm(Point location)
{
InitializeComponent();
MouseLocation = location;
// Allow transparent backgrounds.
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
// Set up this form to be maximum size, with no borders, and have a nearly transparent background.
this.TopMost = true;
this.DoubleBuffered = true;
this.ShowInTaskbar = false;
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
this.BackColor = Color.FromArgb(1,255,255,255);
// Load the Image.
var assemlby = Assembly.GetAssembly(new AssemblyTypeLinker().GetType());
if (assemlby != null)
Logo = Image.FromStream(assemlby.GetManifestResourceStream("MyProgram.MyLogo.png"));
}
/// <summary>
/// Update the current mouse location, and invalidate the control causing a re-draw.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
MouseLocation = e.Location;
Invalidate();
}
/// <summary>
/// Release our logo, and close the form.
/// </summary>
/// <param name="e"></param>
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
Logo.Dispose();
this.Close();
}
/// <summary>
/// Clear what was previously drawn, and if we have a mouse location, draw the logo in that area.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Clear the background back to a nearly transparent white.
e.Graphics.Clear(Color.FromArgb(1, 255, 255, 255));
if (MouseLocation != Point.Empty)
{
// Draw our logo in the spot of the current mouse location.
e.Graphics.DrawImage(Logo, MouseLocation.X - 100, MouseLocation.Y - 100, 200, 200);
}
}
}
编辑:似乎即使在我认为是背景的情况下也会创建黑色背景。我将我BackColor
的设置为完整的 255,并将其设为蓝色,然后将 alpha 分量慢慢降低到 0,只留下我看到的黑色背景。这是 ShowDialog 的特点吗?这个背景是从哪里来的?