超简单的问题?如何在C#的WINFORM中画直线?使用什么控件?

dingdong0080 2005-07-05 11:35:17
初学.Net 问一个超简单的问题?如何在C#的WINFORM中画直线?使用什么控件?
...全文
506 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
俞庆平 2005-07-05
  • 打赏
  • 举报
回复
直接写的,未编译测试。
this指的是当前Form
Graphics g = this.CreateGraphics();
g.DrawLine(Pens.Black,new Point(10,10),new Point(100,100));
dingdong0080 2005-07-05
  • 打赏
  • 举报
回复
我需要的不是程序,谢谢你,我只想在windows窗体上画出直线来分割控件?
dingdong0080 2005-07-05
  • 打赏
  • 举报
回复
GDI+是个什么东西,能否请麻烦讲清楚一些,我有急用,分不够可加
张赐 2005-07-05
  • 打赏
  • 举报
回复
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//打开一个文件
private void menuItem2_Click(object sender, System.EventArgs e)
{
openFileDg.Filter = "Image Files(*.bmp;*.wmf;*.ico;*.cur;*.jgp)|*.bmp;*.wmf;*.ico;*.cur;*.jpg";
openFileDg.Multiselect = false;

if (openFileDg.ShowDialog () == DialogResult.OK )
{
//修改窗口标题
this.Text = "MyDraw\t"+openFileDg.FileName;
editFileName = openFileDg.FileName ;
theImage = Image.FromFile (openFileDg.FileName );
Graphics g = this.CreateGraphics();
g.DrawImage(theImage,this.ClientRectangle);
ig = Graphics.FromImage (theImage);
ig.DrawImage (theImage,this.ClientRectangle );
//ToolBar可以使用了
toolBar1.Enabled = true;
}
}

//新建一个文件
private void menuItem3_Click(object sender, System.EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(backColor);
toolBar1.Enabled = true;
//创建一个Bitmap
theImage = new Bitmap (this.ClientRectangle .Width ,this.ClientRectangle .Height );
editFileName = "新建文件";
//修改窗口标题
this.Text = "MyDraw\t"+editFileName;
ig = Graphics.FromImage (theImage);
ig.Clear (backColor);
}

//保存当前编辑的位图
private void menuItem4_Click(object sender, System.EventArgs e)
{
saveFileDg.Filter = "图像(*.bmp)|*.bmp";
saveFileDg.FileName = editFileName;
if (saveFileDg.ShowDialog () == DialogResult.OK )
{
theImage.Save (saveFileDg.FileName,ImageFormat.Bmp );
this.Text = "MyDraw\t"+saveFileDg.FileName;
editFileName = saveFileDg.FileName ;
}
}


private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e)
{
//根据工具的选择,变化光标。记录选择的工具,光标文件在项目所在路径\CUR目录下

switch (toolBar1.Buttons.IndexOf (e.Button ))
{
case 0:
drawTool = drawTools.Pen ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurPen.cur");
break;
case 1:
drawTool = drawTools.Line ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurLine.cur");
break;
case 2:
drawTool = drawTools.Ellipse ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurEll.cur");
break;
case 3:
drawTool = drawTools.Rectangle ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurRect.cur");
break;
case 4:
drawTool = drawTools.String ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurStr.cur");
break;
case 5:
drawTool = drawTools.Rubber ;
this.Cursor = new Cursor (@"..\..\..\CUR\CurRub.cur");
break;
case 6:
drawTool = drawTools.None ;
this.Cursor = Cursors.Default ;
break;
}
}

//鼠标按下,开始绘图
private void Form1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if ( e.Button == MouseButtons.Left )
{
//如果选择文字输入,则打开strInput窗体
if( drawTool == drawTools.String )
{
strInput inputBox = new strInput ();
inputBox.StartPosition = FormStartPosition.CenterParent ;
if (inputBox.ShowDialog ()==DialogResult.OK )
{
Graphics g = this.CreateGraphics ();
Font theFont = this.Font ;
g.DrawString (inputBox.textBox1 .Text ,theFont,new SolidBrush (foreColor),e.X ,e.Y );
ig.DrawString (inputBox.textBox1 .Text ,theFont,new SolidBrush (foreColor),e.X ,e.Y );
}
}
//如果开始绘制,则开始记录鼠标位置
else if ((isDrawing = !isDrawing)==true)
{
startPoint = new Point (e.X ,e.Y );
oldPoint = new Point (e.X ,e.Y );
}
}

}

//鼠标移动
private void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
Graphics g;
g = this.CreateGraphics() ;

if (isDrawing)
{
switch (drawTool)
{
case drawTools.None:
break;
case drawTools.Pen :
//从上一个点到当前点绘制线段
g.DrawLine (new Pen (foreColor ,1),oldPoint,new Point(e.X,e.Y));
ig.DrawLine (new Pen (foreColor ,1),oldPoint,new Point(e.X,e.Y));
oldPoint.X = e.X ;
oldPoint.Y = e.Y ;
break;
case drawTools.Line :
//首先恢复此次操作之前的图像,然后再添加Line
this.Form1_Paint (this, new PaintEventArgs(this.CreateGraphics() ,this.ClientRectangle ));
g.DrawLine (new Pen (foreColor,1),startPoint,new Point (e.X ,e.Y ));
break;
case drawTools.Ellipse :
//首先恢复此次操作之前的图像,然后再添加Ellipse
this.Form1_Paint (this, new PaintEventArgs(this.CreateGraphics() ,this.ClientRectangle ));
g.DrawEllipse (new Pen (foreColor,1),startPoint.X ,startPoint.Y ,e.X -startPoint.X ,e.Y -startPoint.Y );
break;
case drawTools.Rectangle :
//首先恢复此次操作之前的图像,然后再添加Rectangle
this.Form1_Paint (this, new PaintEventArgs(this.CreateGraphics() ,this.ClientRectangle ));
g.DrawRectangle (new Pen(foreColor,1),startPoint.X ,startPoint.Y ,e.X -startPoint.X ,e.Y -startPoint.Y );
break;
case drawTools.String :
break;
case drawTools.Rubber :
//用背景色绘制宽线段
g.DrawLine (new Pen (backColor ,20),oldPoint,new Point(e.X,e.Y));
ig.DrawLine (new Pen (backColor ,20),oldPoint,new Point(e.X,e.Y));
oldPoint.X = e.X ;
oldPoint.Y = e.Y ;
break;
}
}
}

//鼠标松开,结束绘制
private void Form1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
isDrawing = false;
switch (drawTool)
{
case drawTools.Line :
ig.DrawLine (new Pen (foreColor,1),startPoint,new Point (e.X ,e.Y ));
break;
case drawTools.Ellipse :
ig.DrawEllipse (new Pen (foreColor,1),startPoint.X ,startPoint.Y ,e.X -startPoint.X ,e.Y -startPoint.Y );
break;
case drawTools.Rectangle :
ig.DrawRectangle (new Pen(foreColor,1),startPoint.X ,startPoint.Y ,e.X -startPoint.X ,e.Y -startPoint.Y );
break;
}
}

private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//将Image中保存的图像,绘制出来
Graphics g = this.CreateGraphics() ;
if (theImage!=null)
{
g.Clear(Color.White);
g.DrawImage (theImage,this.ClientRectangle );
}
}

private void Form1_SizeChanged(object sender, System.EventArgs e)
{
this.Form1_Paint (this, new PaintEventArgs(this.CreateGraphics() ,this.ClientRectangle ));
}

//利用ColorDialog选择前景色
private void menuItem7_Click(object sender, System.EventArgs e)
{
ColorDialog colorDg = new ColorDialog ();
if( colorDg.ShowDialog () == DialogResult.OK )
{
foreColor = colorDg.Color ;
}
}
}
}
张赐 2005-07-05
  • 打赏
  • 举报
回复
给你个代码,画什么图形都可以的,呵呵
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing .Imaging;

namespace MyDrawApp
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.MenuItem menuItem2;
private System.Windows.Forms.MenuItem menuItem3;
private System.Windows.Forms.MenuItem menuItem4;
private System.Windows.Forms.MenuItem menuItem5;
private System.Windows.Forms.MenuItem menuItem6;
private System.Windows.Forms.MenuItem menuItem7;
private System.Windows.Forms.ToolBar toolBar1;
private System.Windows.Forms.ToolBarButton toolBarButton1;
private System.Windows.Forms.ToolBarButton toolBarButton2;
private System.Windows.Forms.ToolBarButton toolBarButton3;
private System.Windows.Forms.ToolBarButton toolBarButton4;
private System.Windows.Forms.ToolBarButton toolBarButton5;
private System.Windows.Forms.OpenFileDialog openFileDg;
private System.Windows.Forms.SaveFileDialog saveFileDg;
private System.Windows.Forms.MenuItem menuItem8;
private System.Windows.Forms.ToolBarButton toolBarButton6;
private System.Windows.Forms.ToolBarButton toolBarButton7;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;


//布尔型变量,是否正在绘图
private bool isDrawing = false;
//绘图时记录鼠标的位置
private Point startPoint, oldPoint;
//枚举类型,各种绘图工具
private enum drawTools
{
Pen = 0,Line,Ellipse,Rectangle,String,Rubber,None
};
//当前使用的工具
private drawTools drawTool = drawTools.None ;
//当前编辑的文件名
private string editFileName;
//进行操作的位图
private Image theImage;
//绘制位图的Graphics实例
private Graphics ig;
//绘图使用的色彩
private Color foreColor = Color.Black ;
private Color backColor = Color.White ;



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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.menuItem2 = new System.Windows.Forms.MenuItem();
this.menuItem3 = new System.Windows.Forms.MenuItem();
this.menuItem4 = new System.Windows.Forms.MenuItem();
this.menuItem5 = new System.Windows.Forms.MenuItem();
this.menuItem6 = new System.Windows.Forms.MenuItem();
this.menuItem7 = new System.Windows.Forms.MenuItem();
this.menuItem8 = new System.Windows.Forms.MenuItem();
this.toolBar1 = new System.Windows.Forms.ToolBar();
this.toolBarButton1 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton2 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton3 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton4 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton6 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton7 = new System.Windows.Forms.ToolBarButton();
this.toolBarButton5 = new System.Windows.Forms.ToolBarButton();
this.openFileDg = new System.Windows.Forms.OpenFileDialog();
this.saveFileDg = new System.Windows.Forms.SaveFileDialog();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1,
this.menuItem7,
this.menuItem8});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem2,
this.menuItem3,
this.menuItem4,
this.menuItem5,
this.menuItem6});
this.menuItem1.Text = "文件(&F)";
//
// menuItem2
//
this.menuItem2.Index = 0;
this.menuItem2.Text = "打开(&O)";
this.menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
//
// menuItem3
//
this.menuItem3.Index = 1;
this.menuItem3.Text = "新建(&N)";
this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);
//
// menuItem4
//
this.menuItem4.Index = 2;
this.menuItem4.Text = "保存(&S)";
this.menuItem4.Click += new System.EventHandler(this.menuItem4_Click);
//
// menuItem5
//
this.menuItem5.Index = 3;
this.menuItem5.Text = "-";
//
// menuItem6
//
this.menuItem6.Index = 4;
this.menuItem6.Text = "退出(&X)";
//
// menuItem7
//
this.menuItem7.Index = 1;
this.menuItem7.Text = "颜色(&C)";
this.menuItem7.Click += new System.EventHandler(this.menuItem7_Click);
//
// menuItem8
//
this.menuItem8.Index = 2;
this.menuItem8.Text = "线条(&L)";
//
// toolBar1
//
this.toolBar1.Buttons.AddRange(new System.Windows.Forms.ToolBarButton[] {
this.toolBarButton1,
this.toolBarButton2,
this.toolBarButton3,
this.toolBarButton4,
this.toolBarButton6,
this.toolBarButton7,
this.toolBarButton5});
this.toolBar1.DropDownArrows = true;
this.toolBar1.Enabled = false;
this.toolBar1.Name = "toolBar1";
this.toolBar1.ShowToolTips = true;
this.toolBar1.Size = new System.Drawing.Size(344, 38);
this.toolBar1.TabIndex = 0;
this.toolBar1.ButtonClick += new System.Windows.Forms.ToolBarButtonClickEventHandler(this.toolBar1_ButtonClick);
//
// toolBarButton1
//
this.toolBarButton1.Text = "Pen";
//
// toolBarButton2
//
this.toolBarButton2.Text = "Line";
//
// toolBarButton3
//
this.toolBarButton3.Text = "Ellipse";
//
// toolBarButton4
//
this.toolBarButton4.Text = "Rectangle";
//
// toolBarButton6
//
this.toolBarButton6.Text = "String";
//
// toolBarButton7
//
this.toolBarButton7.Text = "Rubber";
//
// toolBarButton5
//
this.toolBarButton5.Text = "Mouse";
//
// saveFileDg
//
this.saveFileDg.FileName = "doc1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 233);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.toolBar1});
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "MyDrawApp";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseUp);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
this.ResumeLayout(false);

}
#endregion

JzeroBiao 2005-07-05
  • 打赏
  • 举报
回复
GDI+

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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