這段時間忙於研究WinForm的標尺與網格,主管要我做的跟PowerDesigner一樣,經過一段時間的研究、實踐、修改,最終達標了,代碼如下。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.PRinting;
using System.Drawing.Drawing2D;
//計算屏幕像素大小和mm大小引用
using System.Runtime.InteropServices;
namespace UntilityControl
{
public partial class RuleAndGrid : UserControl
{
public Control useControl;
private TextBox lineX=new TextBox();
private TextBox lineY=new TextBox();
private TextBox rectangleX=new TextBox();
private TextBox rectangleY=new TextBox();
Pen penG = new Pen(Color.Black);
Pen penL = new Pen(Color.Red);
Font font = new Font("宋體", 8);
public RuleAndGrid()
{
InitializeComponent();
}
public Panel panel1 = new Panel();
//每毫米占的像素數
float pxwidth;
private void RulerAndGrid_Paint(object sender, PaintEventArgs e)
{
Graphics PenGraphics = e.Graphics;
InitializeGraph(PenGraphics);
}
private void RulerAndGrid_Load(object sender, EventArgs e)
{
panel1.Left = 21;
panel1.Top = 21;
panel1.Width = RulerWidth - 42;
panel1.Height = RulerHeight - 42;
panel1.BackColor = Color.White;
panel1.MouseMove += new MouseEventHandler(panel1_MouseMove);
panel1.MouseUp+=new MouseEventHandler(panel1_MouseUp);
panel1.Paint+=new PaintEventHandler(panel1_Paint);
lineX.Width = 1;
lineX.Height = 14;
lineX.Top = 6;
lineX.BackColor = Color.Red;
lineX.Multiline = true;
lineX.Visible = false;
this.Controls.Add(lineX);
lineY.Width = 14;
lineY.Height = 1;
lineY.Left = 6;
lineY.BackColor = Color.Red;
lineY.Multiline = true;
lineY.Visible = false;
this.Controls.Add(lineY);
rectangleX.Height = 6;
rectangleX.Top =14;
rectangleX.BackColor = Color.Black;
rectangleX.Multiline = true;
rectangleX.Visible = false;
this.Controls.Add(rectangleX);
rectangleY.Width = 6;
rectangleY.Left = 14;
rectangleY.BackColor = Color.Black;
rectangleY.Multiline = true;
rectangleY.Visible = false;
this.Controls.Add(rectangleY);
this.Controls.Add(panel1);
Calcsale();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawGrid();
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
SetProperty sp = new SetProperty();
sp.rad = this;
sp.ShowDialog();
}
else
{
if (useControl != null)
{
useControl.MouseClick +=new MouseEventHandler(useControl_MouseClick);
useControl.Top = e.Y;
useControl.Left = e.X;
panel1.Controls.Add(useControl);
panel1.Cursor = Cursors.Default;