0

我正在尝试创建一个仍会响应鼠标移动和单击事件的透明表单,以显示我们公司的徽标,直到用户单击鼠标,但我遇到了一些困难,因为有时表单和徽标不会'根本不会出现,在其他时候它们会出现,但背景是黑色的。

这是我如何称呼我的表格:

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

4

1 回答 1

0

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

其次,我不知道为什么,但是以前使用BackColoraTransparencyKey导致MouseMoveandMouseClick事件没有被注册,但是我再次相信,因为我覆盖它们而不是附加到它们,尽管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);
        }
    }
于 2012-12-06T18:07:52.970 回答