谁来帮我看看我的C#小程序
悬赏分:20 - 离问题结束还有 14 天 6 小时
编了一个画直线的小程序,我的思想是这样的,点了画直线按钮后,再按下鼠标就开始画线,不要松开鼠标键拖动的过程中会产生一条直线。松开鼠标后直线确定。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DrawLine
{
public class Form1 : Form
{
bool stmove=false;//判断MouseMove操作是否开始
int x1,y1,x2,y2;//声明线的起点和终点坐标
bool start=false;
Graphics g;
Button btn;
Pen pen;
public Form1()
{
this.SuspendLayout();
pen = new Pen(Color.Red, 2);
btn = new Button();
btn.Text = "画直线";
this.Controls.Add(btn);
this.ResumeLayout();
btn.Click += new EventHandler(btn_Click);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
private void btn_Click(object sender,EventArgs e)
{
start = true;
g = this.CreateGraphics();
}
private void Form1_MouseDown(object sender,MouseEventArgs e)
{
if(start)
{
stmove = true;
x1=e.X;
y1=e.Y;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (stmove)
{
g.Clear(this.BackColor);
x2 = e.X;
y2 = e.Y;
g.DrawLine(pen, x1, y1, x2, y2);
}
}
private void Form1_MouseUp(object sender,MouseEventArgs e)
{
if(start)
{
y2 = e.Y;
x2 = e.X;
g.DrawLine(pen,x1,y1,x2,y2);
stmove = false;
}
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
我发现一个问题就是画完第一调直线后再画第2条,第一条就没了,怎么解决这个问题。
悬赏分:20 - 离问题结束还有 14 天 6 小时
编了一个画直线的小程序,我的思想是这样的,点了画直线按钮后,再按下鼠标就开始画线,不要松开鼠标键拖动的过程中会产生一条直线。松开鼠标后直线确定。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace DrawLine
{
public class Form1 : Form
{
bool stmove=false;//判断MouseMove操作是否开始
int x1,y1,x2,y2;//声明线的起点和终点坐标
bool start=false;
Graphics g;
Button btn;
Pen pen;
public Form1()
{
this.SuspendLayout();
pen = new Pen(Color.Red, 2);
btn = new Button();
btn.Text = "画直线";
this.Controls.Add(btn);
this.ResumeLayout();
btn.Click += new EventHandler(btn_Click);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
this.MouseUp += new MouseEventHandler(Form1_MouseUp);
this.MouseMove += new MouseEventHandler(Form1_MouseMove);
}
private void btn_Click(object sender,EventArgs e)
{
start = true;
g = this.CreateGraphics();
}
private void Form1_MouseDown(object sender,MouseEventArgs e)
{
if(start)
{
stmove = true;
x1=e.X;
y1=e.Y;
}
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (stmove)
{
g.Clear(this.BackColor);
x2 = e.X;
y2 = e.Y;
g.DrawLine(pen, x1, y1, x2, y2);
}
}
private void Form1_MouseUp(object sender,MouseEventArgs e)
{
if(start)
{
y2 = e.Y;
x2 = e.X;
g.DrawLine(pen,x1,y1,x2,y2);
stmove = false;
}
}
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
我发现一个问题就是画完第一调直线后再画第2条,第一条就没了,怎么解决这个问题。
解决方案 »
- 这样的ListBox功能是怎么设置的?
- 请教 程序运行时,如果检测到程序已经有一个实例在运行了,就退出,并激活运行的实例的问题
- Access数据库使用NZ函数遇到的问题~~~
- 关于循环的问题
- winform里,如何让8位的PNG图片透明?用picturebox控件,不能透明,请各位指教!
- 写数据时,如果关键字重复,应该采用什么形式?
- 熟正则表达式的兄弟帮忙写段代码.
- stephenZL接分,答应了解决问题后600分
- 在运行ISS中的ASP.NET文件的时候出现这样错误码------- 分析器错误信息: File or assembly name Localization, or one of its dependenci
- Devexress控件集里面的AspxGrid取得选中行问题
- 新手求助!不使用MIS打印通,如何将DataGrid中的数据打印出来,急!!
- 为什么要用“实现类 继承 抽象类 继承 接口”的模式呀,谢谢。
//...namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} Bitmap faceBitmap; // 实际绘制线条的位图
Graphics faceGraphics; // 位图的图像
Point downPoint = Point.Empty; // 鼠标按下的坐标,如果为Empty则没有按下
private void Form1_Load(object sender, EventArgs e)
{
faceBitmap = new Bitmap(ClientSize.Width, ClientSize.Height);
faceGraphics = Graphics.FromImage(faceBitmap);
faceGraphics.FillRectangle(Brushes.White, faceGraphics.ClipBounds);
DoubleBuffered = true; // 避免闪烁
} private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.DrawImage(faceBitmap, new Point(0, 0));
} private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left) return;
downPoint = e.Location;
} private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (downPoint == Point.Empty) return;
Graphics vGraphics = Graphics.FromHwnd(Handle);
vGraphics.DrawImage(faceBitmap, new Point(0, 0));
vGraphics.DrawLine(Pens.Black, downPoint, e.Location);
} private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (downPoint == Point.Empty) return;
faceGraphics.DrawLine(Pens.Black, downPoint, e.Location);
Graphics vGraphics = Graphics.FromHwnd(Handle);
vGraphics.DrawImage(faceBitmap, new Point(0, 0));
downPoint = Point.Empty;
}
}
}
{
Line.Draw(g);
}
{
public Form5()
{
InitializeComponent();
} private ArrayList drawingList = new ArrayList();
private Line currentLine = null;
private void Form5_MouseDown(object sender, MouseEventArgs e)
{
currentLine = new Line(new Point(e.X, e.Y));
} private void Form5_MouseUp(object sender, MouseEventArgs e)
{
if (currentLine != null)
{
currentLine.MouseMove(new Point(e.X,e.Y));
drawingList.Add(currentLine);
currentLine = null;
this.Invalidate();
}
} private void Form5_MouseMove(object sender, MouseEventArgs e)
{
if (currentLine != null && e.Button == MouseButtons.Left)
{
currentLine.MouseMove(new Point(e.X, e.Y));
this.Invalidate(); }
} private void Form5_Paint(object sender, PaintEventArgs e)
{
foreach (Line line in drawingList)
{
line.Draw(e.Graphics);
}
if (currentLine != null)
{
currentLine.Draw(e.Graphics);
}
}
}
public class Line
{
private Point startPoint = new Point(0,0);
private Point endPoint= new Point(0,0);
public void MouseMove(Point p)
{
endPoint = p;
}
public Line(Point p)
{
startPoint = p;
}
public void Draw(Graphics g)
{
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.DrawLine(Pens.Black, startPoint, endPoint);
}
}