我能在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;
}
}
}我该怎么改,各位帮帮忙,我新手,谢了
还有就是画出来的图,怎么即时保存在窗体上,一但窗体状态发生变化,如最小化,画的东西就全部没得了,下面是我的代码
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;
}
}
}我该怎么改,各位帮帮忙,我新手,谢了
解决方案 »
- UDP 网络链接的奇怪问题
- C#新建cs中为啥datagridview没数据的?
- 怎么用反射得到属性的对象
- LoginFrm-->MainForm怎么传不过值,望高手给我看看,谢谢了!!!!!!!!
- 有关水平滚动条的问题
- 哪位大哥帮我详细讲解一下VS2003自带的一个例子啊?我看不懂啊。
- 请教成员变量和属性的用法区别,谢谢。
- 求Web控件,象Menu控件那种效果的。
- 文件属性问题
- 如何让一个应用程序同时实现Remoting的服务器端和客户端?服务器端只有RemotingConfiguration.Configure ("Server.exe.config");一句,怎
- C# FTP 怎么下载文件目录(无限级)包括文件
- 急!!!!!如何去掉小数点后面的0???
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;
}
将你的画图代码写到承载图像的控件的paint函数里,在控件变化时图像不会消失。
2.如何在panel上画图?
画在那个控件上取决于你使用那个控件创建画笔,譬如:你使用的是this.CreateGraphics,那么此时是画在this上的,‘this’就是窗体,如果使用this.panel.CreateGraphics创建画笔那么此时的图像就是画在panel上的。