用alpha覆盖组件,当其下的控件重绘时重绘

发布于 2024-11-03 05:04:51 字数 2395 浏览 0 评论 0原文

我创建了一个自定义控件,我想用它来根据需要使用状态信息覆盖表单的一部分。它应该显示文本并具有背景颜色,具体取决于信息类型。这是仍然不完整的代码。

public partial class StatusPanel : UserControl
{
    public enum PanelStyle
    {
        Info,
        Warning,
        Error
    }

    public PanelStyle Style { get; set; }

    public StatusPanel()
    {
        InitializeComponent();

        this.imgGreen = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
        using(Graphics g = Graphics.FromImage(this.imgGreen))
        {
            g.Clear(Color.Transparent);
            Brush bg = new SolidBrush(Color.FromArgb(200, Color.Green));
            g.FillRectangle(bg, 0, 0, 256, 256);
        }

        this.imgYellow = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
        using(Graphics g = Graphics.FromImage(this.imgYellow))
        {
            g.Clear(Color.Transparent);
            Brush bg = new SolidBrush(Color.FromArgb(10, Color.Yellow));
            g.FillRectangle(bg, 0, 0, 256, 256);
        }
    }

    protected readonly Font font = new Font("Arial", 12.0F);

    protected readonly Brush textBrush = new SolidBrush(Color.Black);

    protected readonly Image imgGreen;

    protected readonly Image imgYellow;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do not draw background
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        Image img = GetImage();

        e.Graphics.DrawImage(img, 0, 0, this.Width, this.Height);

        e.Graphics.DrawString("ABC", this.font, this.textBrush, 1.0F, 1.0F);
    }

    protected Image GetImage()
    {
        return (this.Style == PanelStyle.Info) ? this.imgGreen : this.imgYellow;
    }
}

这工作得很好。但是,当我在窗体上放置一些按钮并将其中一个控件放在它们前面时,当鼠标移到按钮上并且突出显示效果重新绘制它们时,它们将“过度绘制”。

如何通知我的组件由于底层控件已重绘而需要重绘?

I created a custom control which I want to use to overlay part of my form with status information on demand. It should display a text and have a background color depending on the type of information. Here is the still incomplete code.

public partial class StatusPanel : UserControl
{
    public enum PanelStyle
    {
        Info,
        Warning,
        Error
    }

    public PanelStyle Style { get; set; }

    public StatusPanel()
    {
        InitializeComponent();

        this.imgGreen = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
        using(Graphics g = Graphics.FromImage(this.imgGreen))
        {
            g.Clear(Color.Transparent);
            Brush bg = new SolidBrush(Color.FromArgb(200, Color.Green));
            g.FillRectangle(bg, 0, 0, 256, 256);
        }

        this.imgYellow = new Bitmap(256, 256, PixelFormat.Format32bppArgb);
        using(Graphics g = Graphics.FromImage(this.imgYellow))
        {
            g.Clear(Color.Transparent);
            Brush bg = new SolidBrush(Color.FromArgb(10, Color.Yellow));
            g.FillRectangle(bg, 0, 0, 256, 256);
        }
    }

    protected readonly Font font = new Font("Arial", 12.0F);

    protected readonly Brush textBrush = new SolidBrush(Color.Black);

    protected readonly Image imgGreen;

    protected readonly Image imgYellow;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT
            return cp;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // do not draw background
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
        e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear;
        e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

        Image img = GetImage();

        e.Graphics.DrawImage(img, 0, 0, this.Width, this.Height);

        e.Graphics.DrawString("ABC", this.font, this.textBrush, 1.0F, 1.0F);
    }

    protected Image GetImage()
    {
        return (this.Style == PanelStyle.Info) ? this.imgGreen : this.imgYellow;
    }
}

This works quite fine. But when I put some buttons on a form and one of this controls infront of them, they will "overdraw" when the mouse moves over the button and the highlight effects redraws them.

How will my component be notified that it needs to redraw because underlying control have redrawn?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

澉约 2024-11-10 05:04:51

Parent.Control 下的所有控件注册一个绘制处理程序,并为 Parent.ControlAdded 注册您的绘制处理程序,

如下所示:

private void myDummyUserControl_Load(object sender, EventArgs e)
{
    var uc = (DummyUserControl)sender;
    uc.Parent.ControlAdded += new ControlEventHandler(Parent_ControlAdded);
    foreach (Control c in uc.Parent.Controls)
    {
        if (uc == c)
            continue;
        c.Paint += new PaintEventHandler(c_Paint);
    }
}

void c_Paint(object sender, PaintEventArgs e)
{
    //checks & paint stuff here
}

void Parent_ControlAdded(object sender, ControlEventArgs e)
{
    e.Control.Paint += new PaintEventHandler(c_Paint);
}

//edit

not certain if you需要使用递归来遍历子控件树,并且还需要在这些控件上添加处理程序......

register a paint-handler to all controls under Parent.Control and a handler for Parent.ControlAdded that registers your paint-handler

something like this:

private void myDummyUserControl_Load(object sender, EventArgs e)
{
    var uc = (DummyUserControl)sender;
    uc.Parent.ControlAdded += new ControlEventHandler(Parent_ControlAdded);
    foreach (Control c in uc.Parent.Controls)
    {
        if (uc == c)
            continue;
        c.Paint += new PaintEventHandler(c_Paint);
    }
}

void c_Paint(object sender, PaintEventArgs e)
{
    //checks & paint stuff here
}

void Parent_ControlAdded(object sender, ControlEventArgs e)
{
    e.Control.Paint += new PaintEventHandler(c_Paint);
}

//edit

not sure if you need to use recursion to go down the tree of child controls and need to add handlers on those too ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文