where are you drawing the rectangle? draw it in OnPaint

解决方案 »

  1.   

    重载OnPaint事件
    调用Invalidate();触发OnPaint事件
      

  2.   

    谢了。那我应在何时重载呢?
    比如我在Form1中画了一个长方形,点击Form1的一个按钮,弹
    出Form2。当Form2从Form1上经过时,长方形就不见了。这种情况
    我应在何时重载Form1的Onpaint事件呢?
      

  3.   

    直接重载OnPaint,或直接在OnPaint中设计你想画的图形,然后在想改变图形的时候,Invalidata()
    如果你在Form里和PictureBox里都各自画了图形,则要重载两个的OnPaint()
      

  4.   

    OnPaint中才能重画
    要不你 试试画在pictureBox控件中,Delphi中如果画在里面是可以保存而不必重画的!
      

  5.   

    To heineken(Heineken) 和 wenzm(疯之驴) 
    非常感谢你们的回答。可现在关键是何时重载呢?
      

  6.   

    namespace 推箱子的实验
    {
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.ComponentModel.IContainer components;
    private Bitmap myend=new Bitmap("end.jpg");
    private Bitmap mystop=new Bitmap("stop.jpg");
    private Bitmap mybox=new Bitmap("box.jpg");
    private Bitmap mywin=new Bitmap("115.jpg");
    private Bitmap myback=new Bitmap("back.jpg");
    Process ss=new Process();
    public static int[,] map=new int[100,100];
    public static int mapNO;
    private Load Load1;
    public static int manX,manY;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.TextBox textBox1;
    private System.Windows.Forms.CheckBox checkBox1;
    private System.Windows.Forms.PictureBox pictureBox2;
    private System.Windows.Forms.PictureBox pictureBox3;
    private Bitmap myman=new Bitmap("man.jpg");
    protected override void OnPaint(PaintEventArgs e)
    {
    Pen mypen=new Pen(Color.Red,3);
    Graphics mygr=e.Graphics;
    if (ss.isFinished==false)
    {
    draw();
    base.OnPaint(e);
    }
    else
    {
    mygr.Clea(System.Drawing.Color.Black);
    mygr.DrawImage(mywin,0,0);
    }
    } public Form1()
    {
    //
    // Required for Windows Form Designer support
    //
    InitializeComponent(); //
    // TODO: Add any constructor code after InitializeComponent call
    //
    } /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
                               if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }这是我写的程序的一段 这里面就有OnPaint的重载,看懂了吗?
    不行的话,我可以给你整个源代码,不过现在应该明白了
      

  7.   

    重载就是重载了!真么还有什么时候?你重载了onpaint每次你的图片需要刷新的时候,系统会调用onpaint,你就有机会重画了!
    ------------------------------
    我是一只小小鸟
    欢迎交流!
    尽快结帖哦!我是CSDN的菜鸟,等着你的分呢!
    MSN&Mail: [email protected]
    0A
      

  8.   

    非常感谢 wftree(wftree) 以及其他的各位高手,
    在窗体上画图已经实现了我的想法。
    在图片框上我也实现了,再次感谢大家。现在开始散分了。呵呵