C#开发的矢量图形绘制(转)
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
usingSystem.IO; //streamer io
usingSystem.Runtime.Serialization; // io
using System.Runtime.Serialization.Formatters.Binary; // io
using System.Drawing.Printing;
namespace EmrKB
{
publicpartial class vectShapes : UserControl
{
private string Status;
public string Option;
private string redimStatus = "";
private string msg = "";
[CategoryAttribute("Debug"),DescriptionAttribute("ShowDebugInfo")]
public bool ShowDebug { get; set; }
private int startX;
private int startY;
private Shapes s;
private float _Zoom = 1;
private bool _A4 = true;
private int _dx = 0;
private int _dy = 0;
private int startDX = 0;
private int startDY = 0;
private int truestartX = 0;
private int truestartY = 0;
//画笔Pen工具 START
private ArrayList VisPenPointList;
private ArrayList PenPointList;
private int PenPrecX;
private int PenPrecY;
//画笔Pen工具 END
private Bitmap offScreenBmp;
private Bitmap offScreenBackBmp;
// Grid
public int _gridSize = 0;
public bool fit2grid = true;
//Graphic
private System.Drawing.Drawing2D.CompositingQuality_CompositingQuality =System.Drawing.Drawing2D.CompositingQuality.Default;
private System.Drawing.Text.TextRenderingHint _TextRenderingHint =System.Drawing.Text.TextRenderingHint.AntiAlias;
private System.Drawing.Drawing2D.SmoothingMode _SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
private System.Drawing.Drawing2D.InterpolationMode_InterpolationMode =System.Drawing.Drawing2D.InterpolationMode.Default;
// Drawing Rect
private bool MouseSx;
private int tempX;
private int tempY;
// 预览 & 打印
private Anteprima AnteprimaFrm;
// 编辑框
private richForm2 editorFrm;
public RichTextBox r;
public Color CreationPenColor;
public float CreationPenWidth;
public Color CreationFillColor;
public bool CreationFilled;
//public PropertyGrid propGrid;
//EVENT
public event OptionChanged optionChanged;
public event ObjectSelected objectSelected;
//Image1.tif
Cursor AddPointCur = getCursor("newPoint3.cur",Cursors.Cross);
Cursor DelPointCur = getCursor("delPoint3.cur",Cursors.Default);
// Gets he *.cur file in a.
public static Cursor getCursor(string a, Cursor defCur)
{
try
{
return new Cursor(a);
}
catch
{
return defCur;
}
}
public vectShapes()
{
InitializeComponent();
myInit();
//from Ilango.M
this.SetStyle(ControlStyles.UserPaint |ControlStyles.AllPaintingInWmPaint |ControlStyles.OptimizedDoubleBuffer, true); // added line
}
//Graphic
[CategoryAttribute("Graphics"),DescriptionAttribute("Interp.Mode")]
public System.Drawing.Drawing2D.InterpolationModeInterpolationMode
{
get
{
return _InterpolationMode;
}
set
{
_InterpolationMode = value;
}
}
[CategoryAttribute("Graphics"),DescriptionAttribute("Smooth.Mode")]
public System.Drawing.Drawing2D.SmoothingMode SmoothingMode
{
get
{
return _SmoothingMode;
}
set
{
_SmoothingMode = value;
}
}
[CategoryAttribute("Graphics"),DescriptionAttribute("Txt.Rend.Hint")]
public System.Drawing.Text.TextRenderingHintTextRenderingHint
{
get
{
return _TextRenderingHint;
}
set
{
_TextRenderingHint = value;
}
}
[CategoryAttribute("Graphics"),DescriptionAttribute("Comp.Quality")]
public System.Drawing.Drawing2D.CompositingQualityCompositingQuality
{
get
{
return _CompositingQuality;
}
set
{
_CompositingQuality = value;
}
}
[CategoryAttribute(" "), DescriptionAttribute("Canvas")]
public string ObjectType
{
get
{
return "Canvas";
}
}
[CategoryAttribute(" "),DescriptionAttribute("Grid Size")]
public int gridSize
{
get
{
return _gridSize;
}
set
{
if (value >= 0)
{
_gridSize = value;
}
if (_gridSize > 0)
{
this.dx = _gridSize * (int)(this.dx / _gridSize);
this.dy = _gridSize * (int)(this.dy / _gridSize);
}
this.redraw(true);
}
}
[CategoryAttribute(" "),DescriptionAttribute("Canvas Zoom")]
public float Zoom
{
get
{
return _Zoom;
}
set
{
if (value > 0)
{
_Zoom = value;
this.redraw(true);
}
else
{
_Zoom = 1;
this.redraw(true);
}
}
}
[CategoryAttribute(" "),DescriptionAttribute("Show A4")]
public bool A4
{
get
{
return _A4;
}
set
{
_A4 = value;
}
}
[CategoryAttribute(" "),DescriptionAttribute("Canvas OriginX")]
public int dx
{
get
{
return _dx;
}
set
{
_dx = value;
}
}
[CategoryAttribute(" "),DescriptionAttribute("Canvas OriginY")]
public int dy
{
get
{
return _dy;
}
set
{
_dy = value;
}
}
public void zoomIn()
{
this.Zoom = (int)(this.Zoom + 1);
}
public void zoomOut()
{
if (this.Zoom > 1)
{
this.Zoom = (int)(this.Zoom - 1);
}
}
public void mergePolygons()
{
s.mergePolygons();
redraw(true);
}
public void delPoints()
{
this.s.delPoint();
redraw(true);
}
public void linkNodes()
{
this.s.linkNodes();
redraw(true);
}
public void delNodes()
{
this.s.delNodes();
redraw(true);
}
public void extPoints()
{
this.s.extPoints();
redraw(true);
}
public void XMirror()
{
this.s.XMirror();
redraw(true);
}
public void YMirror()
{
this.s.YMirror();
redraw(true);
}
public void Mirror()
{
this.s.Mirror();
redraw(true);
}
private void myInit()
{
//Status = "";
changeStatus("");
Option = "select";
Graphics g = this.CreateGraphics();
//retrive printer resolution
PrintDocument pd = new PrintDocument();
//MessageBox.Show(pd.PrinterSettings.DefaultPageSettings.PrinterResolution.X.ToString());
//MessageBox.Show(pd.PrinterSettings.DefaultPageSettings.PrinterResolution.Y.ToString());
Graphics g_pr =pd.PrinterSettings.CreateMeasurementGraphics();
SizeF sizef;
float x_pr, y_pr = 0;
sizef = g_pr.MeasureString("YourStringHere", Font);
x_pr = sizef.Width;
y_pr = sizef.Height;
//y_pr = Font.Height;
float x_vi, y_vi = 0;
sizef = g.MeasureString("YourStringHere", Font);
x_vi = sizef.Width;
y_vi = sizef.Height;
//s = newShapes((pd.PrinterSettings.DefaultPageSettings.PrinterResolution.X/ g.DpiX),pd.PrinterSettings.DefaultPageSettings.PrinterResolution.Y);
s = new Shapes(g.DpiX * (x_pr / x_vi), g.DpiY * (y_pr /y_vi));
// undoB = new UndoBuffer(5);
g.Dispose();
editorFrm = new richForm2();
this.r = editorFrm.richTextBox1;
CreationPenColor = Color.Black;
CreationPenWidth = 1f;
CreationFillColor = Color.Black;
CreationFilled = false;
this.optionChanged += new OptionChanged(FakeOptionChange);
this.objectSelected += newObjectSelected(FakeObjectSelected);
offScreenBackBmp = new Bitmap(this.Width, this.Height);
offScreenBmp = new Bitmap(this.Width, this.Height);
}
private void FakeOptionChange(object sender, OptionEventArgse)
{ }
private void FakeObjectSelected(object sender, PropertyEventArgse)
{ }
private void changeStatus(string s)
{
this.Status = s;
}
private void changeOption(string s)
{
this.Option = s;
// Notify Option change to "listening object" (i.e: toolBbox)
OptionEventArgs e = new OptionEventArgs(this.Option);
optionChanged(this, e);// raise event
}
public void anteprimaStampa(float zoom)
{
InitializePrintPreviewControl(zoom);
}
#region Print & Preview
public void Stampa()
{
this.s.deSelect();
AnteprimaFrm = new Anteprima();
AnteprimaFrm.PrintPreviewControl1.Name ="PrintPreviewControl1";
AnteprimaFrm.PrintPreviewControl1.Document =AnteprimaFrm.docToPrint;
AnteprimaFrm.PrintPreviewControl1.Zoom = 1;
AnteprimaFrm.PrintPreviewControl1.Document.DocumentName ="Anteprima";
AnteprimaFrm.PrintPreviewControl1.UseAntiAlias = true;
AnteprimaFrm.docToPrint.PrintPage +=
new System.Drawing.Printing.PrintPageEventHandler(
docToPrint_PrintPage);
// Per stampare
AnteprimaFrm.docToPrint.Print();
AnteprimaFrm.Dispose();
}
private void InitializePrintPreviewControl(float zoom)
{
this.s.deSelect();
AnteprimaFrm = new Anteprima();
// Construct the PrintPreviewControl.
//AnteprimaFrm.PrintPreviewControl1 = newPrintPreviewControl();
// Set location, name, and dock style forPrintPreviewControl1.
//AnteprimaFrm.PrintPreviewControl1.Location = new Point(88,80);
AnteprimaFrm.PrintPreviewControl1.Name = "Preview";
//AnteprimaFrm.PrintPreviewControl1.Dock = DockStyle.Fill;
// da testare??
//AnteprimaFrm.PrintPreviewControl1.BackColor =this.BackColor;
//AnteprimaFrm.PrintPreviewControl1.BackgroundImage =this.BackgroundImage;
// Set the Document property to the PrintDocument
// for which the PrintPage event has been handled.
AnteprimaFrm.PrintPreviewControl1.Document =AnteprimaFrm.docToPrint;
// Set the zoom to 25 percent.
AnteprimaFrm.PrintPreviewControl1.Zoom = zoom;
// Set the document name. This will show be displayed when
// the document is loading into the control.
AnteprimaFrm.PrintPreviewControl1.Document.DocumentName ="Preview";
// Set the UseAntiAlias property to true so fonts aresmoothed
// by the operating system.
AnteprimaFrm.PrintPreviewControl1.UseAntiAlias = true;
// Add the control to the form.
//AnteprimaFrm.Controls.Add(AnteprimaFrm.PrintPreviewControl1);
// Associate the event-handling method with the
// document's PrintPage event.
AnteprimaFrm.docToPrint.PrintPage +=
new System.Drawing.Printing.PrintPageEventHandler(
docToPrint_PrintPage);
// Per stampare
//AnteprimaFrm.docToPrint.Print();
AnteprimaFrm.ShowDialog();
}
// 预览图显示在界面上
// by handling the documents PrintPage event
private void docToPrint_PrintPage(
object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
// Insert code to render the page here.
// This code will be called when the control is drawn.
// The following code will render a simple
// message on the document in the control.
//string text = "In docToPrint_PrintPage method.";
//System.Drawing.Font printFont =
// newFont("Arial", 35, FontStyle.Regular);
// e.Graphics.DrawString(text, printFont,
// Brushes.Black, 10, 10);
Graphics g = e.Graphics;
//Do Double Buffering
//Bitmap offScreenBmp;
// Graphics offScreenDC;
//offScreenBmp = new Bitmap(this.Width, this.Height);
//
//offScreenDC = Graphics.FromImage(offScreenBmp);
//offScreenDC.Clear(this.BackColor);
//background image
//if ((this.loadImage) & (this.visibleImage))
// offScreenDC.DrawImage(this.loadImg, 0, 0);
//
//offScreenDC.SmoothingMode=SmoothingMode.AntiAlias;
// test
//MessageBox.Show("dipx : " + g.DpiX + " ; dipy : " +g.DpiY);
//s.Draw(offScreenDC);
if (this.BackgroundImage != null)
g.DrawImage(this.BackgroundImage, 0, 0);
//TEST !!!!!!!!!!!!!!!!!!!!!!!!!!!1
//this.DrawMesure(g);
s.Draw(g, 0, 0, 1);
//if (this.MouseSx)
//{
// System.Drawing.Pen myPen = newSystem.Drawing.Pen(System.Drawing.Color.Red);
// offScreenDC.DrawRectangle(myPen, new Rectangle(this.startX,this.startY, tmpX - this.startX, tmpY - this.startY));
// myPen.Dispose();
//}
//g.DrawImageUnscaled(offScreenBmp, 0, 0);
g.Dispose();
}