c# cad 二次开发 类库对话框的示例
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _19对话框示例
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.DrawEentity();
}
//画图形的函数
private void DrawEentity()
{
int width = picBxDrawMap.Width;
int height = picBxDrawMap.Height;
Bitmap bitmap = new Bitmap(width, height);//创建一个位图对象
Graphics graph = Graphics.FromImage(bitmap); //通过位图创建GDI+对象
Rectangle rect = new Rectangle((int)(width * 0.15), (int)(height * 0.15), (int)(width * 0.8), (int)(height * 0.8));
//绘制图形
Rectangle rectExtends = this.DrawLineAndArc(bitmap, graph,rect);
//画注释
this.DrawDimension(bitmap, graph, rectExtends);
//graph.DrawRectangle(new Pen(Brushes.Red), rect);
picBxDrawMap.Image = bitmap;
}
private Rectangle DrawLineAndArc(Bitmap bitmap, Graphics graph, Rectangle rect)
{
double ratio = rect.Width * 1.0 / rect.Height;
double width = (double)numUDlength.Value;//矩形宽度
double height = (double)numUDheight.Value;//矩形的高度
Pen pen = new Pen(Brushes.Black);
Point topLineLeftPoint;
Point topLineRightPoint;
Point rightLineTopPoint;
Point rightLineBottomPoint;
Point bottomLineLeftPoint;
Point bottomLineRightPoint;
Point leftLineTopPoint;
Point leftLineBottomPoint;
if (width/height >= ratio)
{
//以长度占满绘图区域,按比例绘制高度,圆弧
// width => rect.Width height = height * rect.Width/wdith
height = height* rect.Width/width;
int divHeight = (int)(rect.Height - height)/2;
topLineLeftPoint = new Point(rect.X, rect.Y + divHeight);
topLineRightPoint = new Point(rect.X+rect.Width, rect.Y + divHeight);
rightLineTopPoint = topLineRightPoint;
rightLineBottomPoint = new Point(rect.X + rect.Width, rect.Y + rect.Height- divHeight);
bottomLineLeftPoint = new Point(rect.X, rect.Y + rect.Height - divHeight);
bottomLineRightPoint = rightLineBottomPoint;
leftLineTopPoint = topLineLeftPoint;
leftLineBottomPoint = bottomLineLeftPoint;
}
else
{
//以高度占满绘图区域,按比例绘制长度,圆弧
width = width * rect.Height / height;
int divWidth = (int)(rect.Width - width) / 2;
topLineLeftPoint = new Point(rect.X + divWidth, rect.Y);
topLineRightPoint = new Point(rect.X + rect.Width - divWidth, rect.Y);
rightLineTopPoint = topLineRightPoint;
rightLineBottomPoint = new Point(rect.X + rect.Width - divWidth, rect.Y + rect.Height);
bottomLineLeftPoint = new Point(rect.X + divWidth, rect.Y + rect.Height);
bottomLineRightPoint = rightLineBottomPoint;
leftLineTopPoint = topLineLeftPoint;
leftLineBottomPoint = bottomLineLeftPoint;
}
//绘制上边线
graph.DrawLine(pen, topLineLeftPoint, topLineRightPoint);
//绘制下边线
graph.DrawLine(pen, bottomLineLeftPoint, bottomLineRightPoint);
//绘制左边线
graph.DrawLine(pen, leftLineTopPoint, leftLineBottomPoint);
//绘制右边线
graph.DrawLine(pen, rightLineTopPoint, rightLineBottomPoint);
Rectangle rectExtends = new Rectangle(topLineLeftPoint.X,topLineLeftPoint.Y,topLineRightPoint.X-topLineLeftPoint.X,leftLineBottomPoint.Y-leftLineTopPoint.Y);
return rectExtends;
}
/// <summary>
/// 绘制注释
/// </summary>
/// <param name="bitmap"></param>
/// <param name="graph"></param>
/// <param name="rect"></param>
private void DrawDimension(Bitmap bitmap,Graphics graph,Rectangle rect)
{
int width = picBxDrawMap.Width;
int height = picBxDrawMap.Height;
Pen pen = new Pen(Brushes.LawnGreen);
string strW = numUDlength.Value.ToString();//水平方向的注释文字
string strH = numUDheight.Value.ToString();//垂直方向的注释文字
//半径的注释文字
#region //水平长度的注释线
//Point pointHL1 = new Point(rect.X,(int)(height * 0.05));
//Point pointHL2 = new Point(rect.X,(int)(height * 0.14));
Point pointHL1 = new Point(rect.X, (int)(height * 0.05));
Point pointHL2 = new Point(rect.X,rect.Y-4);
Point pointHR1 = new Point(rect.X+rect.Width, (int)(height * 0.05));
Point pointHR2 = new Point(rect.X + rect.Width, rect.Y - 4);
Point PointHH1 = new Point(rect.X, (int)(height * 0.08));
Point PointHH11 = new Point(rect.X + rect.Width / 2 - 12 * (strW.Length / 2)-6, (int)(height * 0.08));
Point PointHH2 = new Point(rect.X + rect.Width / 2 + 12 * (strW.Length / 2)+8, (int)(height * 0.08));
Point PointHH21 = new Point(rect.X + rect.Width, (int)(height * 0.08));
graph.DrawString(strW, new Font("宋体", 12), Brushes.LawnGreen, rect.X + rect.Width / 2-12*(strW.Length/2), (int)(height * 0.08)-8);
graph.DrawLine(pen, pointHL1, pointHL2);//水平标注的左侧综线
graph.DrawLine(pen, pointHR1, pointHR2);//水平标注的左右侧综线
graph.DrawLine(pen, PointHH1, PointHH11);//第一段水平线
graph.DrawLine(pen, PointHH2, PointHH21);//第二段水平线
//左侧箭头
graph.DrawLine(pen, PointHH1, new Point(PointHH1.X + 12, PointHH1.Y - 3));
graph.DrawLine(pen, PointHH1, new Point(PointHH1.X + 12, PointHH1.Y + 3));
//右侧箭头
graph.DrawLine(pen, PointHH21, new Point(PointHH21.X - 12, PointHH1.Y - 3));
graph.DrawLine(pen, PointHH21, new Point(PointHH21.X - 12, PointHH1.Y + 3));
#endregion
#region //垂直高度的注释
Point pointVT1 = new Point((int)(width * 0.05), rect.Y);
Point pointVT2 = new Point(rect.X-4, rect.Y);
graph.DrawLine(pen,pointVT1, pointVT2); //垂直注释上面横线
Point pointVB1 = new Point((int)(width * 0.05), rect.Y + rect.Height);
Point pointVB2 = new Point(rect.X - 4, rect.Y + rect.Height);
graph.DrawLine(pen, pointVB1, pointVB2); //垂直注释下面横线
graph.DrawString(strH, new Font("宋体", 12), Brushes.LawnGreen,(int)(width * 0.08)-strH.Length*10/2, rect.Y+ rect.Height / 2 - 8);//垂直方向的注释文字
Point pointVV1 = new Point((int)(width * 0.08), rect.Y);
Point pointVV11 = new Point((int)(width * 0.08), rect.Y+rect.Height/2-8);
Point pointVV2 = new Point((int)(width * 0.08), rect.Y + rect.Height / 2 + 8);
Point pointVV21 = new Point((int)(width * 0.08), rect.Y + rect.Height);
graph.DrawLine(pen, pointVV1, pointVV11); //垂直注释第一段线
graph.DrawLine(pen, pointVV2, pointVV21); //垂直注释第二段线
//上面箭头
graph.DrawLine(pen, pointVV1, new Point(pointVV1.X - 3, pointVV1.Y + 12));
graph.DrawLine(pen, pointVV1, new Point(pointVV1.X + 3, pointVV1.Y + 12));
//下面箭头
graph.DrawLine(pen, pointVV21, new Point(pointVV21.X - 3, pointVV21.Y - 12));
graph.DrawLine(pen, pointVV21, new Point(pointVV21.X + 3, pointVV21.Y - 12));
#endregion
//半径注释
//绘制中心线
#region
double lengthDiv = (rect.Height + 10) / 39;
Point firstPoint = new Point(rect.X + rect.Width / 2, rect.Y - 5);
Point endPoint = new Point((int)(rect.X + rect.Width / 2), (int)(firstPoint.Y + 2 * lengthDiv));
for (int i = 0; i < 20; i++)
{
graph.DrawLine(new Pen(Brushes.PaleVioletRed), firstPoint, endPoint);
firstPoint.Y = endPoint.Y + (int)lengthDiv;
endPoint = new Point((int)(rect.X + rect.Width / 2), (int)(firstPoint.Y + 2 * lengthDiv));
}
lengthDiv = (rect.Width + 10) / 39;
firstPoint = new Point((int)(rect.X-5),(int)(rect.Y + rect.Height/2));
endPoint = new Point((int)(firstPoint.X + lengthDiv), (int)(rect.Y + rect.Height / 2));
for (int i = 0; i < 22; i++)
{
graph.DrawLine(new Pen(Brushes.PaleVioletRed), firstPoint, endPoint);
firstPoint.X = endPoint.X + (int)lengthDiv;
endPoint = new Point((int)(firstPoint.X + lengthDiv), (int)(rect.Y + rect.Height / 2));
}
#endregion
}
private void numUDlength_ValueChanged(object sender, EventArgs e)
{
this.DrawEentity();
}
private void numUDheight_ValueChanged(object sender, EventArgs e)
{
this.DrawEentity();
}
}
}