MainForm设计器 代码
namespace CS学习之创建圆形卡尺工具
{
partial class MainForm
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
private void InitializeComponent()
{
this.canvas = new CS学习之创建圆形卡尺工具.Canvas();
this.panel1 = new System.Windows.Forms.Panel();
this.lab_CircleRadios = new System.Windows.Forms.Label();
this.trackb_CircleRadios = new System.Windows.Forms.TrackBar();
this.lab_CallipersLength = new System.Windows.Forms.Label();
this.trackb_CallipersLength = new System.Windows.Forms.TrackBar();
this.lab_CallipersWidth = new System.Windows.Forms.Label();
this.trackb_CallipersWidth = new System.Windows.Forms.TrackBar();
this.labNumber = new System.Windows.Forms.Label();
this.trackb_CallipersNumber = new System.Windows.Forms.TrackBar();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.trackb_CircleRadios)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersLength)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersWidth)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersNumber)).BeginInit();
this.SuspendLayout();
this.canvas.BackColor = System.Drawing.Color.Gray;
this.canvas.Dock = System.Windows.Forms.DockStyle.Fill;
this.canvas.Location = new System.Drawing.Point(3, 40);
this.canvas.Name = "canvas";
this.canvas.Size = new System.Drawing.Size(1140, 727);
this.canvas.TabIndex = 4;
this.panel1.Controls.Add(this.lab_CircleRadios);
this.panel1.Controls.Add(this.trackb_CircleRadios);
this.panel1.Controls.Add(this.lab_CallipersLength);
this.panel1.Controls.Add(this.trackb_CallipersLength);
this.panel1.Controls.Add(this.lab_CallipersWidth);
this.panel1.Controls.Add(this.trackb_CallipersWidth);
this.panel1.Controls.Add(this.labNumber);
this.panel1.Controls.Add(this.trackb_CallipersNumber);
this.panel1.Dock = System.Windows.Forms.DockStyle.Left;
this.panel1.Location = new System.Drawing.Point(3, 40);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(296, 727);
this.panel1.TabIndex = 6;
this.lab_CircleRadios.AutoSize = true;
this.lab_CircleRadios.Location = new System.Drawing.Point(35, 51);
this.lab_CircleRadios.Name = "lab_CircleRadios";
this.lab_CircleRadios.Size = new System.Drawing.Size(62, 18);
this.lab_CircleRadios.TabIndex = 7;
this.lab_CircleRadios.Text = "圆半径";
this.trackb_CircleRadios.Location = new System.Drawing.Point(27, 93);
this.trackb_CircleRadios.Maximum = 500;
this.trackb_CircleRadios.Minimum = 5;
this.trackb_CircleRadios.Name = "trackb_CircleRadios";
this.trackb_CircleRadios.Size = new System.Drawing.Size(225, 69);
this.trackb_CircleRadios.TabIndex = 6;
this.trackb_CircleRadios.Value = 36;
this.lab_CallipersLength.AutoSize = true;
this.lab_CallipersLength.Location = new System.Drawing.Point(35, 460);
this.lab_CallipersLength.Name = "lab_CallipersLength";
this.lab_CallipersLength.Size = new System.Drawing.Size(80, 18);
this.lab_CallipersLength.TabIndex = 5;
this.lab_CallipersLength.Text = "卡尺长度";
this.trackb_CallipersLength.Location = new System.Drawing.Point(27, 502);
this.trackb_CallipersLength.Maximum = 100;
this.trackb_CallipersLength.Minimum = 10;
this.trackb_CallipersLength.Name = "trackb_CallipersLength";
this.trackb_CallipersLength.Size = new System.Drawing.Size(225, 69);
this.trackb_CallipersLength.TabIndex = 4;
this.trackb_CallipersLength.Value = 36;
this.lab_CallipersWidth.AutoSize = true;
this.lab_CallipersWidth.Location = new System.Drawing.Point(35, 329);
this.lab_CallipersWidth.Name = "lab_CallipersWidth";
this.lab_CallipersWidth.Size = new System.Drawing.Size(80, 18);
this.lab_CallipersWidth.TabIndex = 3;
this.lab_CallipersWidth.Text = "卡尺宽度";
this.trackb_CallipersWidth.Location = new System.Drawing.Point(27, 371);
this.trackb_CallipersWidth.Maximum = 100;
this.trackb_CallipersWidth.Minimum = 10;
this.trackb_CallipersWidth.Name = "trackb_CallipersWidth";
this.trackb_CallipersWidth.Size = new System.Drawing.Size(225, 69);
this.trackb_CallipersWidth.TabIndex = 2;
this.trackb_CallipersWidth.Value = 36;
this.labNumber.AutoSize = true;
this.labNumber.Location = new System.Drawing.Point(35, 192);
this.labNumber.Name = "labNumber";
this.labNumber.Size = new System.Drawing.Size(80, 18);
this.labNumber.TabIndex = 1;
this.labNumber.Text = "卡尺数量";
this.trackb_CallipersNumber.Location = new System.Drawing.Point(27, 234);
this.trackb_CallipersNumber.Maximum = 100;
this.trackb_CallipersNumber.Minimum = 10;
this.trackb_CallipersNumber.Name = "trackb_CallipersNumber";
this.trackb_CallipersNumber.Size = new System.Drawing.Size(225, 69);
this.trackb_CallipersNumber.TabIndex = 0;
this.trackb_CallipersNumber.Value = 36;
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1146, 770);
this.Controls.Add(this.panel1);
this.Controls.Add(this.canvas);
this.Name = "MainForm";
this.Text = "MainForm";
this.Load += new System.EventHandler(this.MainForm_Load);
this.Controls.SetChildIndex(this.canvas, 0);
this.Controls.SetChildIndex(this.panel1, 0);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.trackb_CircleRadios)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersLength)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersWidth)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.trackb_CallipersNumber)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Canvas canvas;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label lab_CircleRadios;
private System.Windows.Forms.TrackBar trackb_CircleRadios;
private System.Windows.Forms.Label lab_CallipersLength;
private System.Windows.Forms.TrackBar trackb_CallipersLength;
private System.Windows.Forms.Label lab_CallipersWidth;
private System.Windows.Forms.TrackBar trackb_CallipersWidth;
private System.Windows.Forms.Label labNumber;
private System.Windows.Forms.TrackBar trackb_CallipersNumber;
}
}
(四)圆卡尺类代码
- 创建圆卡尺工具的基本属性,以及一些绘制方法。绘制圆、卡尺、准心。
using System;
using System.Drawing;
namespace CS学习之创建圆形卡尺工具
{
public class CircleCaliper
{
#region 属性
public PointF Center { get; set; }
public float Radius { get; set; }
public int CaliperCount { get; set; } = 36;
public float CaliperLength { get; set; } = 20;
public float CaliperWidth { get; set; } = 5;
public float InnerLengthRatio { get; set; } = 0.5f;
#endregion
#region 绘制方法
public void Draw(Graphics g)
{
using (Pen circlePen = new Pen(Color.Red, 2))
{
g.DrawEllipse(circlePen, Center.X - Radius, Center.Y - Radius,
Radius * 2, Radius * 2);
}
DrawCrosshair(g);
DrawCalipers(g);
}
private void DrawCrosshair(Graphics g)
{
float size = 3f;
using (Pen crossPen = new Pen(Color.Yellow, 1))
{
g.DrawLine(crossPen, Center.X - size, Center.Y, Center.X + size, Center.Y);
g.DrawLine(crossPen, Center.X, Center.Y - size, Center.X, Center.Y + size);
}
}
private void DrawCalipers(Graphics g)
{
using (Pen outerPen = new Pen(Color.Lime, 1))
using (Pen innerPen = new Pen(Color.Cyan, 1))
{
for (int i = 0; i < CaliperCount; i++)
{
float angle = (float)(2 * Math.PI * i / CaliperCount);
float cos = (float)Math.Cos(angle);
float sin = (float)Math.Sin(angle);
PointF edgePoint = new PointF(
Center.X + Radius * cos,
Center.Y + Radius * sin);
PointF outerEnd = new PointF(
edgePoint.X + CaliperLength * cos,
edgePoint.Y + CaliperLength * sin);
g.DrawLine(outerPen, edgePoint, outerEnd);
PointF innerEnd = new PointF(
edgePoint.X - CaliperLength * InnerLengthRatio * cos,
edgePoint.Y - CaliperLength * InnerLengthRatio * sin);
g.DrawLine(innerPen, edgePoint, innerEnd);
DrawCaliperRectangle(g, edgePoint, angle);
}
}
}
private void DrawCaliperRectangle(Graphics g, PointF center, float angle)
{
float perpAngle = angle + (float)(Math.PI / 2);
float dx = (float)(CaliperWidth / 2 * Math.Cos(perpAngle));
float dy = (float)(CaliperWidth / 2 * Math.Sin(perpAngle));
PointF[] outerRect = new PointF[4];
outerRect[0] = new PointF(center.X - dx, center.Y - dy);
outerRect[1] = new PointF(center.X + dx, center.Y + dy);
outerRect[2] = new PointF(center.X + dx + (float)(CaliperLength * Math.Cos(angle)),
center.Y + dy + (float)(CaliperLength * Math.Sin(angle)));
outerRect[3] = new PointF(center.X - dx + (float)(CaliperLength * Math.Cos(angle)),
center.Y - dy + (float)(CaliperLength * Math.Sin(angle)));
PointF[] innerRect = null;
innerRect = new PointF[4];
innerRect[0] = new PointF(center.X - dx, center.Y - dy);
innerRect[1] = new PointF(center.X + dx, center.Y + dy);
innerRect[2] = new PointF(center.X + dx - (float)(CaliperLength * InnerLengthRatio * Math.Cos(angle)),
center.Y + dy - (float)(CaliperLength * InnerLengthRatio * Math.Sin(angle)));
innerRect[3] = new PointF(center.X - dx - (float)(CaliperLength * InnerLengthRatio * Math.Cos(angle)),
center.Y - dy - (float)(CaliperLength * InnerLengthRatio * Math.Sin(angle)));
using (Brush outerBrush = new SolidBrush(Color.FromArgb(50, Color.Blue)))
{
g.FillPolygon(outerBrush, outerRect);
}
using (Pen outerPen = new Pen(Color.Blue, 1))
{
g.DrawPolygon(outerPen, outerRect);
}
if (innerRect != null)
{
using (Brush innerBrush = new SolidBrush(Color.FromArgb(50, Color.Blue)))
{
g.FillPolygon(innerBrush, innerRect);
}
using (Pen innerPen = new Pen(Color.Blue, 1))
{
g.DrawPolygon(innerPen, innerRect);
}
}
}
#endregion
}
}