我能在Form上画图了,但是画在其他地方就不行了
还有就是画出来的图,怎么即时保存在窗体上,一但窗体状态发生变化,如最小化,画的东西就全部没得了,下面是我的代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        bool isdrawing  = false;
       
        Point endPoint;        private void Form1_Load(object sender, EventArgs e)
        {
            this.Cursor = new Cursor(@"pen_m.cur");
        }
    
    //在Form上画,画得起
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((isdrawing = !isdrawing) == true)
                {
                    startPoint = new Point(e.X,e.Y);
                    endPoint = new Point(e.X,e.Y);
                }
            }
        }        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if(isdrawing)
            {
               
                Graphics g = this.CreateGraphics();
                g.DrawLine(new Pen(Color.Red,2),endPoint,new Point(e.X,e.Y));
                endPoint.X = e.X;
                endPoint.Y = e.Y;
            }
        }        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            isdrawing = false;
        }
        //下面的代码是在panel1上画,画不起,郁闷,panel1上画线,画出来的图显示在form上了
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if ((isdrawing = !isdrawing) == true)
                {
                    startPoint = new Point(e.X, e.Y);
                    endPoint = new Point(e.X, e.Y);
                }
            }
        }        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (isdrawing)
            {
                Graphics g = this.CreateGraphics();
                g.DrawLine(new Pen(Color.Red, 2), endPoint, new Point(e.X, e.Y));
                endPoint.X = e.X;
                endPoint.Y = e.Y;
            }
        }        private void panel1_MouseUp(object sender, MouseEventArgs e)
        {
            isdrawing = false;
        }
    }
}我该怎么改,各位帮帮忙,我新手,谢了

解决方案 »

  1.   

    //参考
                if (isdrawing)
                {
                    Graphics g = Graphics.FromHwnd(panel1.Hanele);
                    //                             ~~~~~~~~~~~~~
                    g.DrawLine(new Pen(Color.Red, 2), endPoint, new Point(e.X, e.Y));
                    endPoint.X = e.X;
                    endPoint.Y = e.Y;
                }
      

  2.   

    1.如何保存图片?
        将你的画图代码写到承载图像的控件的paint函数里,在控件变化时图像不会消失。
    2.如何在panel上画图?
        画在那个控件上取决于你使用那个控件创建画笔,譬如:你使用的是this.CreateGraphics,那么此时是画在this上的,‘this’就是窗体,如果使用this.panel.CreateGraphics创建画笔那么此时的图像就是画在panel上的。