为什么我的用户控件绘画事件不只在一个控件上填充矩形? [英] Why isn't my user control paint event filling my rectangle only on one control?

查看:71
本文介绍了为什么我的用户控件绘画事件不只在一个控件上填充矩形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白自己在做什么错。
对于我创建的两个控件之一(第一个),它起作用。但是对于第二个,不是吗?
我已经尝试了很多方法来解决此问题,但是我没有更接近解决方案的任何想法吗?

I can't understand what I'm doing wrong. For one of the two controls I create (the first one) it works. But for the second one, it doesn't? I've tried many things to fix this but I haven't come any closer to a solution any ideas?

会发生什么

创建它们的代码:

    public MessageLogView()
    {
        MessageBubble bubble = new MessageBubble("Hey there Steve I love you", DateTime.Today, Color.FromArgb(255,255,255), new Padding(10, 10, 10, 10));

        bubble.Location = new Point(5, 5);

        this.Controls.Add(bubble);

        MessageBubble bubble2 = new MessageBubble("K, good for you", DateTime.Today, Color.FromArgb(220, 248, 198), new Padding(10, 10, 10, 10));

        bubble2.Location = new Point(5, 5 + bubble.BubbleHeight + 5);

        this.Controls.Add(bubble2);

    }

用户控件:

 public partial class MessageBubble : UserControl
{
    private string Text;
    private DateTime Date;
    private Color BubbleColor = Color.FromArgb(220, 248, 198);
    private Size stringSize, datestringSize;
    public Font textFont { get; set; } = new Font("Arial", 12, FontStyle.Regular);
    public Font dateFont { get; set; } = new Font("Arial", 9, FontStyle.Bold);
    public Color textColor { get; set; } = Color.Black;
    public Color dateColor { get; set; } = Color.FromArgb(180, 208, 158);
    public int cornerRadius { get; set; } = 5;

    public int BubbleHeight { get
        {
            return this.Padding.Top + this.Padding.Bottom + stringSize.Height + 5 + datestringSize.Height;
        }
    }
    public int BubbleWidth
    {
        get
        {
            return this.Padding.Left + this.Padding.Right + stringSize.Width;
        }
    }

    public MessageBubble(string text, DateTime date, Color color, Padding padding)
    {
        InitializeComponent();
        Text = text;
        this.BubbleColor = color;
        Date = date;
        stringSize = this.CreateGraphics().MeasureString(Text, this.textFont).ToSize();
        datestringSize = this.CreateGraphics().MeasureString(Date.ToString("hh:mm tt"), this.dateFont).ToSize();
        this.Padding = padding;
        this.Width = this.BubbleWidth;
        this.Height = this.BubbleHeight;
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        e.Graphics.FillRectangle(new SolidBrush(this.BubbleColor), Bounds);
        e.Graphics.DrawString(this.Text, this.textFont, new SolidBrush(this.textColor), new Point(this.Padding.Left,this.Padding.Top));
        e.Graphics.DrawString(this.Date.ToString("hh:mm tt"), this.dateFont, new SolidBrush(this.dateColor), new Point(this.Width - datestringSize.Width - Padding.Right, this.Padding.Top + stringSize.Height + 5));
        Console.WriteLine($"Drawn: {this.Text} - {this.Bounds.X}, {this.Bounds.Y} - {this.BubbleWidth}, {this.BubbleHeight}/{this.Bounds.Width}, {this.Bounds.Height} - {this.BubbleColor.ToString() }");
    }
}


推荐答案

奇怪的行为是由于使用 Bounds 而不是 ClientRectangle 。它们是不同的:

The strange behavior is because of using Bounds instead of ClientRectangle. They are different:


  • 界限:控件的大小和位置相对于父控件

  • ClientRectangle :表示控件的工作区的矩形。
  • >
  • Bounds: Size and location of the control relative to the parent control.
  • ClientRectangle: The rectangle that represents the client area of the control.

在填充矩形时使用 ClientRectangle

当您放置一个(100,100)作为其大小的控件时,在(10,10),客户端矩形将是(0,0,100,100)边界将为(10、10、100、100)

When you put a control having (100, 100) as its size on the point (10,10) on the parent, client rectangle will be (0, 0, 100, 100) and Bounds will be (10, 10, 100, 100).

注意:您需要处理GDI +对象,否则不久将面临 GDI泄漏。在使用中创建和使用它们:

Note: You need to dispose the GDI+ objects, otherwise you will be faced with GDI leak soon. Create and use them in a using:

using (var brush = new SolidBrush(this.BubbleColor))
    e.Graphics.FillRectangle(brush, ClientRectangle);

这篇关于为什么我的用户控件绘画事件不只在一个控件上填充矩形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆