在forms窗体上初始化画了几个图形,我现在点击一个按钮想改变其中一个的颜色,可总是报参数无效错误。
我知道是因为没有调用paint():方法,可现在不知道在在Click里怎么调用。

解决方案 »

  1.   

    图形画在什么上面的啊?一般花在Image上,用PICTUREBOX显示
      

  2.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace 画矩形
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Pen mypen=new Pen(Color.Black);
            private void button1_Click(object sender, EventArgs e)
            {
                mypen.Color = Color.Blue;
                draw();
            }        private void draw()
            {
                int x = 30;
                int y = 40;
                int width = 90;
                int height = 90;
                Graphics g = this.CreateGraphics();
                g.DrawRectangle(mypen, new Rectangle(x, y, width, height));
            }        private void button2_Click(object sender, EventArgs e)
            {
                mypen.Color = Color.Red;
                draw();
            }
        }
    }
      

  3.   

     RectangleF rcF2 = listRe1[2];
                    SolidBrush redBrush = new SolidBrush(Color.Red);
                    try
                    {
                        m_pGraphic.FillEllipse(redBrush, rcF2);
                    }
                    catch (System.Exception ex)
                    {
                    }                //m_pGraphic.DrawEllipse(Pens.Orange, GetRectFromRectF(rcF2));
                    Font titleFont = new Font("宋体", 0.6F * m_nHeightTitle * m_dScale, GraphicsUnit.Pixel);
                    StringFormat strFormat = new StringFormat();
                    strFormat.Alignment = StringAlignment.Center;
                    strFormat.LineAlignment = StringAlignment.Center;
                    m_pGraphic.DrawString("sss".ToString(), titleFont, Brushes.Black, rcF2, strFormat);
    我是在以前的代码上改的。用的是RectangleF 。总是报参数无效,查了查好像是因为Form1_Paint方法的原因。可不知道该怎么调用它。把方法写在Form1_Paint里。在窗体加载时就画上了。
      

  4.   

    没有搞明白说得啥子。
    要是问怎么调用Form_Paint直接调用Invalidate方法或Refresh发送WM_Paint方法就行了,要是窗体加载时就画上了,弄一个标志符不就完了。
      

  5.   

    写了个小例子,你将颜色控制和绘图分开,绘图的时候根据参数绘图,按钮控制颜色就行了不必搞那么复杂:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace 绘图
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        Pen p;
            SolidBrush s;
            private void draw(int x,int y,int width,int height)
            {
                Graphics g = this.CreateGraphics();
                g.DrawRectangle(p, new Rectangle(x, y, width, height));
                g.FillRectangle(s, x, y, width, height);
            }
            private void button1_Click(object sender, EventArgs e)
            {
                p = new Pen(Color.Green,5);
                s = new SolidBrush(Color.Red);
                draw(40,50,50,50);
            }        private void button2_Click(object sender, EventArgs e)
            {
                p = new Pen(Color.Red, 5);
                s = new SolidBrush(Color.Yellow);
                draw(150, 50, 50, 50);
            }
        }
    }