如何用鼠标画线??

xiaolifeifei 2005-09-23 08:25:11
如何用鼠标画线??最好是可以删除画 过的线
...全文
467 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaolifeifei 2005-09-23
  • 打赏
  • 举报
回复
非常感谢,能加你QQ吗?80997602
bingbingcha 2005-09-23
  • 打赏
  • 举报
回复
这个是测试过的代码..测试后可以看到效果

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;

namespace Hello_Graphics
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button button2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 312);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(336, 40);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// checkBox1
//
this.checkBox1.Location = new System.Drawing.Point(264, 8);
this.checkBox1.Name = "checkBox1";
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 48);
this.button2.Name = "button2";
this.button2.TabIndex = 2;
this.button2.Text = "button2";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(384, 373);
this.Controls.Add(this.button2);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private Point startPoint = new Point(50, 217);
private Point endPoint = new Point(50, 217);

protected override void OnPaint(PaintEventArgs e)
{

Graphics g = e.Graphics;
Pen p = new Pen(Color.Black,2);
SolidBrush sb = new SolidBrush(Color.Red);
SolidBrush sb2 = new SolidBrush(Color.Black);
Rectangle[] recs = new Rectangle[] {
new Rectangle(35,25,12,12),
new Rectangle(45,25,12,12),
new Rectangle(55,25,12,12),
};

g.FillRectangles(sb2,recs);
g.FillEllipse(sb,20,20,20,20);

g.Dispose();
sb.Dispose();
p.Dispose();
sb2.Dispose();
}


private void Form1_Load(object sender, System.EventArgs e)
{

}

private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Create a Graphics object
Graphics g1 = this.CreateGraphics();
// Create two pens
Pen linePen = new Pen(Color.Green, 1);
Pen ellipsePen = new Pen(Color.Red, 1);
startPoint = endPoint;
endPoint = new Point(e.X, e.Y);
// Draw the line from the current point
// to the new point
g1.DrawLine(linePen, startPoint, endPoint);
// If Rectangle check box is checked,
// draw a rectangle to represent the point
if(checkBox1.Checked)
{
g1.DrawRectangle(ellipsePen,
e.X-2, e.Y-2, 4, 4);
}else
{
g1.DrawEllipse(ellipsePen,
e.X-2, e.Y-2, 4, 4);
}
// Dispose of objects
linePen.Dispose();
ellipsePen.Dispose();
g1.Dispose();
}

}

private void button2_Click(object sender, System.EventArgs e)
{
startPoint.X = 50;
startPoint.Y = 217;
endPoint.X = 50;
endPoint.Y = 217;
this.Invalidate(this.ClientRectangle);

}
}
}

4,818

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧