各位大侠好,
初学C#,编个程序打算在窗口加载时,画一个方块,代码如下。
可调试时,为什么显示不出方块?
求高手指点!!在线等~    public partial class test : Form
    {
        public test()
        {
            InitializeComponent();
        }
        SolidBrush blkBrush = new SolidBrush(Color.Black);        private void test_Load(object sender, EventArgs e)
        {
            this.CreateGraphics().FillRectangle(blkBrush, new Rectangle(300, 200, 40, 40));
        }
    }

解决方案 »

  1.   

    你不在OnPaint里画,一刷新就没了...当然看不见...
      

  2.   


    那请问能不能在鼠标点击事件中调用paint事件呢?
    比如在
    private void btnStart_MouseClick(object sender, MouseEventArgs e)
    中调用
     private void test_Paint(object sender, PaintEventArgs e)
    ?我想实现在点击一个按钮后,也出现图形
      

  3.   

    用 this.Invalidate() 触发Paint事件重绘。
      

  4.   

    可以。private void btnStart_MouseClick(object sender, MouseEventArgs e)
    {
        test_Paint(this, new PaintEventArgs());
    }
      

  5.   


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WinAppSideBar
    {
        public partial class FrmBrush : Form
        {
            public FrmBrush()
            {
                InitializeComponent();
            }        private void btnLine_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen pen = new Pen(Color.Red, 2);
                g.Clear(this.BackColor);
                g.DrawLine(pen, 10, 10, 10, 20);
            }
            private void btnArc_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen pen = new Pen(Color.Red, 2);
                g.Clear(this.BackColor);
                g.DrawArc(pen, 10, 10, 200, 100, 60, 90);
            }        private void btnRect_Click(object sender, EventArgs e)
            {
                Graphics g = this.CreateGraphics();
                Pen pen = new Pen(Color.Red, 2);
                g.Clear(this.BackColor);
                g.DrawRectangle(pen, 10, 10, 200, 100); 
            }        //画弧
            //public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAgngle);
            //pen       :Pen  
            //x           :外接矩形左上角   x   的坐标
            //y           :外接矩形左上角   y   的坐标
            //width   :外接矩形宽
            //height:外接矩形高
            //startAngle:开始角度(以度为单位)  
            //sweepAngle;延伸角度(以度为单位)           //画直线
            //public void DrawLine(Pen pen, int x1, int y1, int x2, int y2);
            //pen       :Pen  
            //x1           :起点x   的坐标
            //y1           :起点y   的坐标
            //x2           :终点x   的坐标
            //y2           :终点y   的坐标         //public void DrawRectangle(Pen pen, int x, int y, int width, int height);
            //pen       :Pen  
            //x           :矩形左上角   x   的坐标
            //y           :矩形左上角   y   的坐标
            //width   :矩形宽
            //height:矩形高 
        }
    }