📄 form1.cs
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Drawing.Drawing2D;
namespace Example062__多种风格的端点
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.ListBox listBox2;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.listBox2 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.ItemHeight = 12;
this.listBox1.Items.AddRange(new object[] {
"箭头状锚头帽",
"菱形锚头帽",
"平线帽",
"没有帽",
"圆线帽",
"圆锚头帽",
"方线帽",
"方锚头帽",
"三角线帽"});
this.listBox1.Location = new System.Drawing.Point(8, 8);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(96, 112);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// listBox2
//
this.listBox2.ItemHeight = 12;
this.listBox2.Items.AddRange(new object[] {
"箭头状锚头帽",
"菱形锚头帽",
"平线帽",
"没有帽",
"圆线帽",
"圆锚头帽",
"方线帽",
"方锚头帽",
"三角线帽"});
this.listBox2.Location = new System.Drawing.Point(112, 8);
this.listBox2.Name = "listBox2";
this.listBox2.Size = new System.Drawing.Size(96, 112);
this.listBox2.TabIndex = 1;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(216, 157);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.listBox2,
this.listBox1});
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
Pen MyPen;
Graphics g;
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
//绘制直线
g.DrawLine(MyPen,10,150,200,150);
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
//设置起始点的帽样式
switch(this.listBox1.SelectedIndex)
{
case 0:
MyPen.StartCap=LineCap.ArrowAnchor;
break;
case 1:
MyPen.StartCap=LineCap.DiamondAnchor;
break;
case 2:
MyPen.StartCap=LineCap.Flat;
break;
case 3:
MyPen.StartCap=LineCap.NoAnchor;
break;
case 4:
MyPen.StartCap=LineCap.Round;
break;
case 5:
MyPen.StartCap=LineCap.RoundAnchor;
break;
case 6:
MyPen.StartCap=LineCap.Square;
break;
case 7:
MyPen.StartCap=LineCap.SquareAnchor;
break;
case 8:
MyPen.StartCap=LineCap.Triangle;
break;
}
//启发Paint事件并执行其中的代码
this.Invalidate();
}
private void Form1_Load(object sender, System.EventArgs e)
{
g=this.CreateGraphics();
//创建Pen对象并设置起始点和结束点的帽样式
MyPen=new Pen(Color.Blue,5);
MyPen.StartCap=LineCap.NoAnchor;
MyPen.EndCap=LineCap.NoAnchor;
}
private void listBox2_SelectedIndexChanged(object sender, System.EventArgs e)
{
//设置结束点的帽样式
switch(this.listBox2.SelectedIndex)
{
case 0:
MyPen.EndCap=LineCap.ArrowAnchor;
break;
case 1:
MyPen.EndCap=LineCap.DiamondAnchor;
break;
case 2:
MyPen.EndCap=LineCap.Flat;
break;
case 3:
MyPen.EndCap=LineCap.NoAnchor;
break;
case 4:
MyPen.EndCap=LineCap.Round;
break;
case 5:
MyPen.EndCap=LineCap.RoundAnchor;
break;
case 6:
MyPen.EndCap=LineCap.Square;
break;
case 7:
MyPen.EndCap=LineCap.SquareAnchor;
break;
case 8:
MyPen.EndCap=LineCap.Triangle;
break;
}
//启发Paint事件并执行其中的代码
this.Invalidate();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -