You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

341 lines
11 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
namespace GeoSigmaViewer
{
public partial class Drawer : UserControl
{
public event EventHandler<EventArgs> SetScrollBar;
bool enableRedraw = false;
private bool isNewFile = true;
GeoSigma geo;
RectangleD mapRange = RectangleD.Empty;
public bool EnableRedraw { get => enableRedraw; set => enableRedraw=value; }
public RectangleD MapRange { get => mapRange; set => mapRange=value; }
public GeoSigma Geo { get => geo; set => geo=value; }
private DrawToolType activeTool; // active drawing tool
private DrawTool[] tools; // array of tools
/// <summary>
/// Active drawing tool.
/// </summary>
public DrawToolType ActiveTool
{
get
{
return activeTool;
}
set
{
if (tools!=null)
{
tools[(int)activeTool].End();
activeTool =value;
DrawItemType itemType = tools[(int)activeTool].ItemType;
if (Geo!=null)
{
if (itemType==DrawItemType.ITEM_VIEW_PAN)
{
Geo.SetViewPan();
}
else if (itemType==DrawItemType.ITEM_VIEW_WINDOW)
{
Geo.SetViewWindow();
}
else
{
Geo.SetItem((int)itemType);
tools[(int)activeTool].Start();
}
}
}
}
}
//public void enlargeLeft(double offX)
//{
// mapRange.X -=offX;
// mapRange.Width+=offX;
//}
//public void enlargeRight(double offX)
//{
// //mapRange.X=offX;
// mapRange.Width+=offX;
//}
//public void enlargeBottom(double offY)
//{
// //mapRange.X=offX;
// mapRange.Y+=offY;
// mapRange.Height+=offY;
//}
//public void enlargeTop(double offY)
//{
// mapRange.Height+=offY;
//}
public Drawer()
{
InitializeComponent();
//this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
//this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.HorizontalScroll.Visible=false;
this.VerticalScroll.Visible=false;
// create array of drawing tools
tools=new DrawTool[(int)DrawToolType.NumberOfDrawTools];
tools[(int)DrawToolType.Default]=new DrawToolDefault();
tools[(int)DrawToolType.Select]=new DrawToolSelect();
tools[(int)DrawToolType.Pointer]=new DrawToolPointer();
tools[(int)DrawToolType.ViewPan]=new DrawToolViewPan();
tools[(int)DrawToolType.ViewWindow]=new DrawToolViewWindow();
tools[(int)DrawToolType.Point] = new DrawToolPoint();
tools[(int)DrawToolType.Curve] = new DrawToolCurve();
tools[(int)DrawToolType.Proportion] = new DrawToolProportion();
tools[(int)DrawToolType.RectangularCSGrid] = new DrawToolRectangularCSGrid();
tools[(int)DrawToolType.Rectangle] = new DrawToolRectangle();
tools[(int)DrawToolType.Ellipse] = new DrawToolEllipse();
tools[(int)DrawToolType.Arc] = new DrawToolArc();
tools[(int)DrawToolType.Chord] = new DrawToolChord();
tools[(int)DrawToolType.Pie] = new DrawToolPie();
tools[(int)DrawToolType.Equilateral] = new DrawToolEquilateral();
tools[(int)DrawToolType.Text] = new DrawToolText();
tools[(int)DrawToolType.BreakDirect] = new DrawToolBreakDirect();
tools[(int)DrawToolType.Break] = new DrawToolBreak();
tools[(int)DrawToolType.Delete] = new DrawToolDelete();
tools[(int)DrawToolType.DeleteIn] = new DrawToolDeleteIn();
tools[(int)DrawToolType.DeleteOut] = new DrawToolDeleteOut();
}
public GeoSigma OpenFile(string drawFile)
{
if (!File.Exists(drawFile))
{
return null;
}
Console.WriteLine(drawFile);
if (Geo!=null)
{
Geo.Dispose();
}
Geo=null;
Geo=new GeoSigma();
foreach(DrawTool t in tools)
{
if (t != null)
{
t.SigmaDrawer = this;
}
}
Geo.OpenDocument(drawFile);
isNewFile=true;
Geo.SetHWND(this.Handle);
//this.Invalidate();
return Geo;
}
public void ZoomIn()
{
Geo.ViewEnlarge();
Geo.EnableRedraw(true);
ResetScrollbar();
this.ReDraw();
//SetScrollBar();
}
public void ZoomOut()
{
Geo.ViewReduce();
Geo.EnableRedraw(true);
ResetScrollbar();
this.ReDraw();
}
public void ViewAll()
{
Geo.ViewAll();
Geo.EnableRedraw(true);
//// 获取全图面积
//double dLeft = 0.0, dTop = 0.0, dRight = 0.0, dBottom = 0.0;
//Geo.GetMapRange(ref dLeft, ref dTop, ref dRight, ref dBottom);
//this.MapRange=RectangleD.FromLTRB(dLeft, dTop, dRight, dBottom);
//if (SetScrollBar!=null)
//{
// Rectangle rect = this.ClientRectangle;
// SetScrollBar(this, new ScrollArgs(rect, this.MapRange));
//}
ResetScrollbar();
this.ReDraw();
}
public void ReDrawWindow()
{
if (Geo==null) return;
Geo.EnableRedraw(true);
if (SetScrollBar!=null)
{
Rectangle rect = this.ClientRectangle;
SetScrollBar(this, new ScrollArgs(rect, this.MapRange));
}
}
public void ReDraw()
{
DrawMem();
this.Invalidate();
}
private void Drawer_Paint(object sender, PaintEventArgs e)
{
//Graphics graphics = e.Graphics;
Graphics graphics = this.CreateGraphics();
Console.WriteLine("Drawer_Paint");
//graphics.DrawLine(Pens.Red, new Point(0, 0), new Point(300, 300));
//return;
//Rectangle rect = this.ClientRectangle; // e.ClipRectangle-差一个像素;
Rectangle rect = e.ClipRectangle;
//graphics.Clear(Color.White);
if (Geo==null)
{
return;
}
IntPtr pMemDC = graphics.GetHdc();
if (isNewFile==true)
{
Geo.Initialize(pMemDC, rect.X, rect.Y, rect.Right, rect.Bottom);
DrawMem();
//Geo.DrawMem();
//Geo.DrawDC();
ResetScrollbar();
isNewFile=false;
}
else
{
//Geo.DrawDC();
//Geo.SetDC(pMemDC, rect.X, rect.Y, rect.Right, rect.Bottom);
//Geo.DrawMem();
//DrawMem();
//Geo.DrawDC();
}
Geo.SetDC(pMemDC, rect.X, rect.Y, rect.Right, rect.Bottom);
Geo.DrawDC();
}
public void DrawMem()
{
Rectangle rect = this.ClientRectangle;
Graphics graphics = this.CreateGraphics();
IntPtr pMemDC = graphics.GetHdc();
Geo.SetDC(pMemDC, rect.X, rect.Y, rect.Right, rect.Bottom);
Geo.DrawMem();
}
public void ResetScrollbar()
{
Rectangle rect = this.ClientRectangle;
double dLeft = 0.0, dTop = 0.0, dRight = 0.0, dBottom = 0.0;
Geo.GetMapRange(ref dLeft, ref dTop, ref dRight, ref dBottom);
this.MapRange=RectangleD.FromLTRB(dLeft, dTop, dRight, dBottom);
if (SetScrollBar!=null)
{
SetScrollBar(this, new ScrollArgs(rect, this.MapRange));
}
}
public void ChangeContextMenu(DrawToolType type)
{
switch(type)
{
case DrawToolType.Curve:
ContextMenuStrip = ctmCurve;
break;
default:
this.ContextMenuStrip = null;
break;
}
}
private void Drawer_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Debug:Drawer_MouseDown:x="+e.X+",y="+e.Y);
//Geo.MouseSelect(e.X, e.Y);
if(e.Button ==MouseButtons.Left)
{
if (tools==null)
{
return;
}
tools[(int)activeTool].OnLButtonDown(this, e);
//Geo.OnLButtonDown(e.X, e.Y);
}
}
private void Drawer_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button==MouseButtons.Left)
{
tools[(int)activeTool].OnLButtonUp(this, e);
}
}
private void Drawer_MouseMove(object sender, MouseEventArgs e)
{
//if (e.Button==MouseButtons.Left)
//{
if (tools==null)
{
return;
}
tools[(int)activeTool].OnMouseMove(this, e);
//}
}
private void Drawer_Load(object sender, EventArgs e)
{
Console.WriteLine("Drawer_Load");
//Graphics g = this.CreateGraphics();
//g.DrawLine(Pens.Red, new Point(0, 0), new Point(629, 200));
}
private void EndToolStripMenuItem_Click(object sender, EventArgs e)
{
((DrawToolCurve)tools[(int)activeTool]).Execute("end");
}
private void CancelToolStripMenuItem_Click(object sender, EventArgs e)
{
((DrawToolCurve)tools[(int)activeTool]).Execute("cancel");
}
private void AutoClosedToolStripMenuItem_Click(object sender, EventArgs e)
{
((DrawToolCurve)tools[(int)activeTool]).Execute("autoclose");
}
private void MergeToolStripMenuItem_Click(object sender, EventArgs e)
{
((DrawToolCurve)tools[(int)activeTool]).Execute("merge");
}
private void NextToolStripMenuItem_Click(object sender, EventArgs e)
{
((DrawToolCurve)tools[(int)activeTool]).Execute("next");
}
}
public class ScrollArgs : EventArgs
{
Rectangle screenRect;
RectangleD realRect;
public Rectangle ScreenRect { get => screenRect; set => screenRect=value; }
public RectangleD RealRect { get => realRect; set => realRect=value; }
public ScrollArgs(){
}
public ScrollArgs(Rectangle screenRect, RectangleD realRect)
:base()
{
this.ScreenRect=screenRect;
this.RealRect=realRect;
}
}
}