透明表格困境(表格显示黑色,表格根本不显示)

时间:2012-12-06 17:25:15

标签: c# .net winforms graphics mouse

我正在尝试创建一个透明的表单,它仍然会响应鼠标移动和点击事件,以显示我们公司的徽标,直到用户点击鼠标,但我有点困难,有时形式和徽标根本不会显示,有时它们会显示,但背景为黑色。

以下是我打电话给我的表格:

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的特点吗?这背景来自哪里?

1 个答案:

答案 0 :(得分:0)

这是修复。首先,因为我现在正在覆盖OnPaint,而在之前的实现中我不是,我经常使用CreateGraphics()。但是由于我正在覆盖OnPaint,它附带了它自己的图形对象,当它被无效时我会被清除,因此我不需要自己清除它。

其次,我不知道为什么,但之前使用BackColor TransparencyKey导致MouseMoveMouseClick事件未被注册,但是,我相信,因为我压倒他们反对附加他们,他们被解雇,尽管TransparencyKey

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.Lime;
        this.TransparencyKey = Color.Lime;

        // Load the Image.
        var assembly = Assembly.GetAssembly(new AssemblyTypeLinker().GetType());
        if (assembly != null)
            Logo = Image.FromStream(assembly.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);

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