自定义用户控件GUI重绘

时间:2014-12-01 03:21:32

标签: c# user-interface user-controls

我有一个自定义用户控件,我在触发onPaint函数刷新时遇到问题

public void addColor(ColorTypes.RGB rgb)
{
    _color_list.Add(rgb);
    this.Invalidate();
}

protected override void OnPaint(PaintEventArgs pe)
{
    // Call the OnPaint method of the base class.
    base.OnPaint(pe);

    Graphics g = pe.Graphics;

    Size rect_size = new Size(this.ClientSize.Width / _color_list.Count, this.ClientSize.Height);
    int count = 0;
    foreach (ColorTypes.RGB rgb in _color_list)
    {
        Point p = new Point(count * rect_size.Width, 0);
        SolidBrush myBrush = new SolidBrush(ColorTypes.RGBtoColor(rgb));
        g.FillRectangle(myBrush, new Rectangle(p, rect_size));
        myBrush.Dispose();
        count++;
    }
}

addColor由此对象的父级调用。当_color_list更新时,我希望OnPaint函数再次触发并使用新颜色重绘图形。

编辑:解决了它。

就我而言,父对象在构造时执行此操作:

        myUserControl= new myControl();
        myUserControl.Show();  

劫持.Designer.cs文件中正确构造的对象:

private void InitializeComponent()
{
    this.button1 = new System.Windows.Forms.Button();
    this.button2 = new System.Windows.Forms.Button();
    this.myUserControl= new myControl();
    this.SuspendLayout();
    ...

0 个答案:

没有答案