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.

1404 lines
48 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// <copyright file="FormMain.cs" company="PlaceholderCompany">
// Copyright (c) PlaceholderCompany. All rights reserved.
// </copyright>
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using GeoSigma.SigmaDrawerUtil;
using GeoSigma.UCDraw;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Linq;
using UCDraw;
using WeifenLuo.WinFormsUI.Docking;
namespace PcgDraw
{
/// <summary>
/// 绘图模块主窗口
/// </summary>
public partial class FormMain : Form
{
private bool saveLayout = true;
private FrmLayers frmLayers;
private FrmProperty frmProperty;
private FrmOutput frmOutput;
private ToolStripRenderer toolStripProfessionalRenderer = new ToolStripProfessionalRenderer();
private string dockDefaultPath = string.Empty;
private string dockPanelPath = string.Empty;
private VisualStudioToolStripExtender.VsVersion currThemeVersion;
private ThemeBase currTheme;
private int nViewCount = 0;
private DrawerConfig config = null;
// private GeoSigma geo = null;
private FileHandler fileHandler;
/// <summary>
/// 正在进行布局操作
/// </summary>
private bool isOperationLayout = false;
private List<MainView> mainViews = new List<MainView>();
private MainView viewerCur = null;
private string startFile = string.Empty;
private List<string> lstArgs = new List<string>();
public double ZColorWidth { get; set; } = 25;
/// <summary>
/// Initializes a new instance of the <see cref="FormMain"/> class.
/// </summary>
public FormMain()
{
InitializeComponent();
this.dockPanel.DocumentStyle = DocumentStyle.DockingMdi;
this.dockPanelPath = Path.Combine(Application.StartupPath, "DockPanel.config");
vsToolStripExtender1.DefaultRenderer = toolStripProfessionalRenderer;
loadDockPanel();
config = DrawerConfig.Instance;
fileHandler = new FileHandler();
fileHandler.LoadFileEvent += OpenFile;
}
public void GetLengthAngle(string info)
{
this.tsslLengthAngle.Text = info;
}
/// <summary>
/// Initializes a new instance of the <see cref="FormMain"/> class.
/// 带启动文件的构造函数
/// </summary>
/// <param name="startFile">启动文件</param>
public FormMain(string[] args)
: this()
{
if (args.Length > 0)
{
this.startFile = args[0];
}
if (args.Length > 1)
{
lstArgs.Clear();
for (int i = 1; i < args.Length; i++)
{
lstArgs.Add(args[i]);
}
}
}
private void FormMain_Load(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.startFile))
{
if (this.lstArgs.Count > 0)
{
string strZC = this.lstArgs.Find(it => { return it.ToUpper().StartsWith("/ZC"); });
if (!string.IsNullOrEmpty(strZC))
{
int nSizeIndex = strZC.IndexOf(':');
if (nSizeIndex > 0)
{
ZColorWidth = Convert.ToInt32(strZC.Substring(nSizeIndex + 1));
}
OpenFile(this.startFile, true);
this.lstArgs.Clear();
}
}
else
{
OpenFile(this.startFile);
}
}
this.tsmiFileSelectSave.Enabled = false;
Application.Idle += Application_Idle;
int originSet = config.FindConfigValue("Display", "ZoomMode", 0);
if (originSet == 0)
{
tsmiLeftTop.Checked = true;
tsmiCenter.Checked = false;
}
else
{
tsmiLeftTop.Checked = false;
tsmiCenter.Checked = true;
}
string themeSet = config.FindConfigValue("Theme", "Version", "0");
ToolStripMenuItem themeVersion = new ToolStripMenuItem();
themeVersion = menuItemSchemaVS2015Light;
menuItemSchemaVS2015Light.Checked = themeSet.IndexOf(menuItemSchemaVS2015Light.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2015Light.Checked)
{
themeVersion = menuItemSchemaVS2015Light;
}
//menuItemSchemaVS2005.Checked = themeSet.IndexOf(menuItemSchemaVS2005.ToString().Replace(" ", "")) > -1;
//if (menuItemSchemaVS2005.Checked)
//{
// themeVersion = menuItemSchemaVS2005;
//}
menuItemSchemaVS2003.Checked = themeSet.IndexOf(menuItemSchemaVS2003.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2003.Checked)
{
themeVersion = menuItemSchemaVS2003;
}
menuItemSchemaVS2012Light.Checked = themeSet.IndexOf(menuItemSchemaVS2012Light.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2012Light.Checked)
{
themeVersion = menuItemSchemaVS2012Light;
}
menuItemSchemaVS2012Blue.Checked = themeSet.IndexOf(menuItemSchemaVS2012Blue.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2012Blue.Checked)
{
themeVersion = menuItemSchemaVS2012Blue;
}
menuItemSchemaVS2012Dark.Checked = themeSet.IndexOf(menuItemSchemaVS2012Dark.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2012Dark.Checked)
{
themeVersion = menuItemSchemaVS2012Dark;
}
menuItemSchemaVS2013Light.Checked = themeSet.IndexOf(menuItemSchemaVS2013Light.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2013Light.Checked)
{
themeVersion = menuItemSchemaVS2013Light;
}
menuItemSchemaVS2013Blue.Checked = themeSet.IndexOf(menuItemSchemaVS2013Blue.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2013Blue.Checked)
{
themeVersion = menuItemSchemaVS2013Blue;
}
menuItemSchemaVS2013Dark.Checked = themeSet.IndexOf(menuItemSchemaVS2013Dark.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2013Dark.Checked)
{
themeVersion = menuItemSchemaVS2013Dark;
}
menuItemSchemaVS2015Blue.Checked = themeSet.IndexOf(menuItemSchemaVS2015Blue.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2015Blue.Checked)
{
themeVersion = menuItemSchemaVS2015Blue;
}
menuItemSchemaVS2015Dark.Checked = themeSet.IndexOf(menuItemSchemaVS2015Dark.ToString().Replace(" ", "")) > -1;
if (menuItemSchemaVS2015Dark.Checked)
{
themeVersion = menuItemSchemaVS2015Dark;
}
setSchema(themeVersion, null);
SetRecentMenu(null);
}
/// <summary>
/// 设置最近打开的文件菜单
/// </summary>
private void SetRecentMenu(string strNewFile)
{
if (!string.IsNullOrEmpty(strNewFile))
{
fileHandler.AddRecentFile(strNewFile);
}
fileHandler.UpdateMenu(btnOpenFile);
btnOpenFile.DropDownItems.Add(tssRecentFile);
btnOpenFile.DropDownItems.Add(btnReloadFile);
}
private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (saveLayout)
{
dockPanel.SaveAsXml(dockPanelPath);
}
else if (File.Exists(dockPanelPath))
{
File.Delete(dockPanelPath);
}
Application.Idle -= Application_Idle;
}
private void Application_Idle(object sender, EventArgs e)
{
SetUIStatus();
// 设置是否被修改的状态
// setDirtyFlag(viewer);
}
private void SetUIStatus()
{
bool bEnableDataIO = false;
if (this.viewerCur == null)
{
bEnableDataIO = false;
}
else
{
bEnableDataIO = true;
}
this.tsmiImport.Enabled = bEnableDataIO;
this.tsmiExport.Enabled = bEnableDataIO;
tsmiShow.Enabled = bEnableDataIO;
this.tsmiFileSelectSave.Enabled = bEnableDataIO;
tsmiPointSave.Enabled = bEnableDataIO;
tsmiCurveSave.Enabled = bEnableDataIO;
tsmiSolidSave.Enabled = bEnableDataIO;
tsmiText.Enabled = bEnableDataIO;
tsmiCleanCurve.Enabled = bEnableDataIO;
tsmiXYZFSave.Enabled = bEnableDataIO;
tsmiSaveSymbol.Enabled = bEnableDataIO;
tsbtnMerge.Enabled = bEnableDataIO;
if (this.viewerCur != null && this.viewerCur.IsElementSelected)
{
this.tsmiFileSelectSave.Enabled = true;
}
else
{
this.tsmiFileSelectSave.Enabled = false;
}
}
/// <summary>
/// 显示坐标状态
/// </summary>
/// <param name="mouseXY">鼠标坐标状态</param>
private void showMouseXY(string mouseXY)
{
tsslStatusInfo.Text = mouseXY;
}
private void showTipMessage(string tipMessage)
{
tsslTipInfo.Text = tipMessage;
}
#region 界面设置
private void SetCurrentLayer(object send, PropertyChangedEventArgs e)
{
if (frmLayers != null)
{
frmLayers.Text = e.PropertyName;
}
}
private void createView(MainView viewer, bool showDoc)
{
this.SuspendLayout();
createPanels();
frmLayers.MainControl = viewer.LayerTreeControl;
frmProperty.MainControl = viewer.PropertyControl;
frmOutput.MainControl = viewer.StaticsControl;
frmLayers.Text = viewer.CurrentLayer;
viewer.LayerSelected -= SetCurrentLayer;
viewer.LayerSelected += SetCurrentLayer;
viewer.MouseMoveXYEvent -= showMouseXY;
viewer.MouseMoveXYEvent += showMouseXY;
viewer.TipMessageHandler += showTipMessage;
// 设置工具条
for (int i = this.toolStripContainer1.TopToolStripPanel.Controls.Count; i > 0; i--)
{
// 删除主工具条以外的其它工具条
if (!object.ReferenceEquals(this.toolStripContainer1.TopToolStripPanel.Controls[i - 1], tspMain))
{
this.toolStripContainer1.TopToolStripPanel.Controls.RemoveAt(i - 1);
}
}
ToolStrip tspEle = viewer.ToolstripElements;
tspEle.Dock = DockStyle.None;
this.toolStripContainer1.TopToolStripPanel.Controls.Add(tspEle);
// tspEle.Location =
// tspEle.Size
ToolStrip tspView = viewer.ToolstripView;
tspView.Dock = DockStyle.None;
this.toolStripContainer1.TopToolStripPanel.Controls.Add(tspView);
tspMain.Left = 0;
tspMain.Dock = DockStyle.Top;
tspView.Dock = DockStyle.None;
tspEle.Dock = DockStyle.None;
tspEle.Location = new System.Drawing.Point(0, tspMain.Height + 2);
tspView.Location = new System.Drawing.Point(0, tspEle.Height + tspMain.Height + 4);
vsToolStripExtender1.SetStyle(tspView, currThemeVersion, currTheme);
vsToolStripExtender1.SetStyle(tspEle, currThemeVersion, currTheme);
// 设置文档
if (showDoc)
{
FrmDoc frmDoc = CreateDocByView(viewer);
frmDoc.Show(dockPanel, DockState.Document);
}
this.ResumeLayout(true);
}
private FrmDoc CreateDocByView(MainView mainView)
{
if (mainView == null)
{
return null;
}
FrmDoc frmDoc = new FrmDoc
{
HideOnClose = false,
CloseButtonVisible = true,
};
frmDoc.FormClosing += FrmDoc_FormClosing;
frmDoc.Activated += FrmDoc_Activated;
// frmDoc.Show(dockPanel, DockState.Document);
frmDoc.ID = mainView.ID;
frmDoc.MainControl = mainView.ViewControl;
// viewer.ViewControl.BorderStyle = BorderStyle.None;
if (string.IsNullOrEmpty(mainView.FileFullName))
{
frmDoc.FileFullName = null;
}
else
{
// FileInfo fi = new FileInfo(mainView.FileFullName);
// frmDoc.Text = fi.Name;
frmDoc.FileFullName = mainView.FileFullName;
}
frmDoc.Text = mainView.Text;
return frmDoc;
}
private void FrmDoc_Activated(object sender, EventArgs e)
{
this.Text = $"地质图形--{((FrmDoc)sender).Text}";
}
/// <summary>
/// 激活图件
/// </summary>
/// <param name="text">名称</param>
private bool ActiveDoc(string fullFile)
{
IDockContent frmDoc = dockPanel.Documents.FirstOrDefault(it =>
{
FrmDoc doc = it as FrmDoc;
if (doc.FileFullName.Equals(fullFile, StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
return false;
});
if (frmDoc != null)
{
((FrmDoc)frmDoc).Activate();
return true;
}
return false;
}
private void FrmDoc_FormClosing(object sender, FormClosingEventArgs e)
{
// 布局操作
if (isOperationLayout)
{
return;
}
// 关闭操作
FrmDoc frmDoc = sender as FrmDoc;
MainView view = findViewByDocID(frmDoc.ID);
if (view != null)
{
if (saveAndClose(frmDoc))
{
view.ClearLayerTree();
mainViews.Remove(view);
if (object.ReferenceEquals(view, viewerCur))
{
viewerCur = null;
}
view = null;
}
else
{
e.Cancel = true; // 修复Bug:图形修改后点关闭提示是否保存,点击取消也关闭图形。
}
}
}
private bool saveAndClose(FrmDoc frmDoc)
{
bool bModified = false;
if (viewerCur != null && viewerCur.IsDocumentModified())
{
bModified = true;
}
// If the page is dirty then we need to ask if it should be saved
if (frmDoc.Text.EndsWith("*") || bModified)
{
switch (MessageBox.Show(
"是否将修改保存到'" + frmDoc.Text.TrimEnd('*') + "' ?",
"关闭文档",
MessageBoxButtons.YesNoCancel))
{
case DialogResult.Cancel:
// Returning false indicates the operation was cancelled
return false;
case DialogResult.Yes:
btnSaveFile_Click(null, null);
return true;
case DialogResult.No:
return true;
}
}
return true;
}
private void dockPanel_ActiveDocumentChanged(object sender, EventArgs e)
{
FrmDoc frmDoc = dockPanel.ActiveDocument as FrmDoc;
if (frmDoc == null)
{
//this.tsmiImport.Enabled = false;
//this.tsmiExport.Enabled = false;
return;
}
MainView viewFind = findViewByDocID(frmDoc.ID);
if (viewerCur == null || viewerCur.ID != viewFind.ID)
{
viewerCur = viewFind;
createView(viewerCur, false);
}
}
private void createPanels()
{
if (frmLayers == null)
{
frmLayers = new FrmLayers();
frmLayers.Text = "图层";
}
if (frmProperty == null)
{
frmProperty = new FrmProperty();
frmProperty.Text = "属性";
}
if (frmOutput == null)
{
frmOutput = new FrmOutput();
frmOutput.Text = "统计";
}
}
private void closeAllContents()
{
// we don't want to create another instance of tool window, set DockPanel to null
if (frmLayers != null)
{
frmLayers.DockPanel = null;
}
if (frmProperty != null)
{
frmProperty.DockPanel = null;
}
if (frmOutput != null)
{
frmOutput.DockPanel = null;
}
// Close all other document windows
closeAllDocuments();
// IMPORTANT: dispose all float windows.
foreach (var window in dockPanel.FloatWindows.ToList())
{
window.Dispose();
}
}
private void closeAllDocuments()
{
if (dockPanel.DocumentStyle == DocumentStyle.SystemMdi)
{
foreach (Form form in MdiChildren)
{
form.Close();
}
}
else
{
foreach (IDockContent document in dockPanel.DocumentsToArray())
{
FrmDoc frmDoc = document as FrmDoc;
frmDoc.Controls.RemoveAt(0);
// IMPORANT: dispose all panes.
document.DockHandler.DockPanel = null;
document.DockHandler.Close();
}
}
}
private void loadDefaultPanels(FrmDoc frmDoc)
{
createPanels();
dockPanel.SuspendLayout(true);
// 加载所有文档
foreach (MainView view in mainViews)
{
FrmDoc frm = CreateDocByView(view);
if (frm != null)
{
frm.Show(dockPanel, DockState.Document);
}
}
// 输出信息面板居下
frmOutput.Show(dockPanel, DockState.DockBottom);
// 图层面板居左
frmLayers.Show(dockPanel, DockState.DockLeft);
dockPanel.DockLeftPortion = 186;
// 属性面板居右
frmProperty.Show(dockPanel, DockState.DockRight);
dockPanel.DockRightPortion = 200;
dockPanel.Controls.SetChildIndex(dockPanel.DockWindows[DockState.Document], 0);
dockPanel.Controls.SetChildIndex(dockPanel.DockWindows[DockState.DockLeft], 4);
dockPanel.Controls.SetChildIndex(dockPanel.DockWindows[DockState.DockRight], 3);
dockPanel.Controls.SetChildIndex(dockPanel.DockWindows[DockState.DockTop], 2);
dockPanel.Controls.SetChildIndex(dockPanel.DockWindows[DockState.DockBottom], 1);
dockPanel.ResumeLayout(true, true);
isOperationLayout = false;
}
private void loadDockPanel()
{
createPanels();
try
{
// 根据配置文件动态加载浮动窗体
this.dockPanel.LoadFromXml(this.dockPanelPath, persistString =>
{
// 图层窗体
if (persistString == typeof(FrmLayers).ToString())
{
return frmLayers;
}
// 属性窗体
else if (persistString == typeof(FrmProperty).ToString())
{
return frmProperty;
}
// 输出窗体
else if (persistString == typeof(FrmOutput).ToString())
{
return frmOutput;
}
// 文档窗体
else
{
string[] parsedStrings = persistString.Split(new char[] { ',' });
if (parsedStrings.Length != 3)
{
return null;
}
if (parsedStrings[0] != typeof(FrmDoc).ToString())
{
return null;
}
if (parsedStrings[1] == string.Empty)
{
return null;
}
int nDocID = Convert.ToInt32(parsedStrings[1]);
MainView view = findViewByDocID(nDocID);
FrmDoc frmDoc = CreateDocByView(view);
if (frmDoc != null)
{
//frmDoc.Text = parsedStrings[2];
//frmDoc.HideOnClose = false;
//frmDoc.CloseButtonVisible = true;
//frmDoc.MainControl = view.ViewControl;
//frmDoc.FormClosing += FrmDoc_FormClosing;
return frmDoc;
}
return null;
}
});
}
catch (Exception)
{
// 配置文件不存在或配置文件有问题时 按系统默认规则加载子窗体
loadDefaultPanels(null);
}
}
private MainView findViewByDocID(int docID)
{
foreach (MainView view in this.mainViews)
{
if (view.ID == docID)
{
return view;
}
}
return null;
}
/// <summary>
/// 设置界面主题
/// </summary>
/// <param name="sender">主题菜单项</param>
/// <param name="e">参数</param>
private void setSchema(object sender, EventArgs e)
{
isOperationLayout = true;
dockPanel.SaveAsXml(dockPanelPath);
closeAllContents();
//if (sender == this.menuItemSchemaVS2005)
//{
// currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2005;
// currTheme = vS2005Theme1;
//}
if (sender == this.menuItemSchemaVS2003)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2003;
currTheme = vS2003Theme1;
}
else if (sender == this.menuItemSchemaVS2012Light)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2012;
currTheme = vS2012LightTheme1;
}
else if (sender == this.menuItemSchemaVS2012Blue)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2012;
currTheme = vS2012BlueTheme1;
}
else if (sender == this.menuItemSchemaVS2012Dark)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2012;
currTheme = vS2012DarkTheme1;
}
else if (sender == this.menuItemSchemaVS2013Blue)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2013;
currTheme = vS2013BlueTheme1;
}
else if (sender == this.menuItemSchemaVS2013Light)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2013;
currTheme = vS2013LightTheme1;
}
else if (sender == this.menuItemSchemaVS2013Dark)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2013;
currTheme = vS2013DarkTheme1;
}
else if (sender == this.menuItemSchemaVS2015Blue)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2015;
currTheme = vS2015BlueTheme1;
}
else if (sender == this.menuItemSchemaVS2015Dark)
{
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2015;
currTheme = vS2015DarkTheme1;
}
else
{
// (sender == this.menuItemSchemaVS2015Light)
currThemeVersion = VisualStudioToolStripExtender.VsVersion.Vs2015;
currTheme = vS2015LightTheme1;
}
this.dockPanel.Theme = currTheme;
this.enableVSRenderer(currThemeVersion, currTheme);
config.WriteString("Theme", "Version", currTheme.ToString());
//menuItemSchemaVS2005.Checked = (sender == menuItemSchemaVS2005);
menuItemSchemaVS2003.Checked = (sender == menuItemSchemaVS2003);
menuItemSchemaVS2012Light.Checked = (sender == menuItemSchemaVS2012Light);
menuItemSchemaVS2012Blue.Checked = (sender == menuItemSchemaVS2012Blue);
menuItemSchemaVS2012Dark.Checked = (sender == menuItemSchemaVS2012Dark);
menuItemSchemaVS2013Light.Checked = (sender == menuItemSchemaVS2013Light);
menuItemSchemaVS2013Blue.Checked = (sender == menuItemSchemaVS2013Blue);
menuItemSchemaVS2013Dark.Checked = (sender == menuItemSchemaVS2013Dark);
menuItemSchemaVS2015Light.Checked = (sender == menuItemSchemaVS2015Light);
menuItemSchemaVS2015Blue.Checked = (sender == menuItemSchemaVS2015Blue);
menuItemSchemaVS2015Dark.Checked = (sender == menuItemSchemaVS2015Dark);
if (dockPanel.Theme.ColorPalette != null)
{
statusStrip1.BackColor = dockPanel.Theme.ColorPalette.MainWindowStatusBarDefault.Background;
toolStripContainer1.TopToolStripPanel.BackColor = dockPanel.Theme.ColorPalette.CommandBarToolbarDefault.Background;
}
loadDockPanel();
isOperationLayout = false;
}
private void enableVSRenderer(VisualStudioToolStripExtender.VsVersion version, ThemeBase theme)
{
IEnumerator ie = this.toolStripContainer1.TopToolStripPanel.Controls.GetEnumerator();
while (ie.MoveNext())
{
if (ie.Current is ToolStrip)
{
ToolStrip tsp = ie.Current as ToolStrip;
vsToolStripExtender1.SetStyle(tsp, version, theme);
tsp.GripMargin = new Padding(0);
tsp.Padding = new Padding(-2);
tsp.Margin = new Padding(-2);
}
}
vsToolStripExtender1.SetStyle(statusStrip1, version, theme);
}
/// <summary>
/// 加载默认布局
/// </summary>
/// <param name="sender">默认布局按钮</param>
/// <param name="e">参数</param>
private void tsbDefaultLayout_Click(object sender, EventArgs e)
{
isOperationLayout = true;
closeAllContents();
loadDefaultPanels(null);
}
/// <summary>
/// 锁定布局
/// </summary>
/// <param name="sender">锁定布局按钮</param>
/// <param name="e">参数</param>
private void tsbLockLayout_Click(object sender, EventArgs e)
{
dockPanel.AllowEndUserDocking = !dockPanel.AllowEndUserDocking;
}
/// <summary>
/// 显示图层窗口
/// </summary>
/// <param name="sender">图层菜单项</param>
/// <param name="e">参数</param>
private void tsbLayer_Click(object sender, EventArgs e)
{
if (frmLayers == null)
{
frmLayers = new FrmLayers();
}
frmLayers.Show(dockPanel);
}
/// <summary>
/// 显示属性窗口
/// </summary>
/// <param name="sender">属性菜单项</param>
/// <param name="e">参数</param>
private void tsbProperty_Click(object sender, EventArgs e)
{
if (frmProperty == null)
{
frmProperty = new FrmProperty();
}
frmProperty.Show(dockPanel);
}
/// <summary>
/// 显示输出窗口
/// </summary>
/// <param name="sender">输出窗口菜单项</param>
/// <param name="e">参数</param>
private void tsbOutput_Click(object sender, EventArgs e)
{
ShowAndActiveOutput();
}
/// <summary>
/// Shows and active output.
/// </summary>
private void ShowAndActiveOutput()
{
if (frmOutput == null)
{
frmOutput = new FrmOutput();
}
frmOutput.Show(dockPanel);
// dockPanel.ActiveAutoHideContentChanged += DockPanel_ActiveAutoHideContentChanged;
if (DockHelper.IsDockStateAutoHide(frmOutput.DockState))
{
dockPanel.ActiveAutoHideContent = frmOutput;
if (frmOutput.DockState == DockState.DockBottomAutoHide)
{
frmOutput.DockState = DockState.DockBottom;
}
else if (frmOutput.DockState == DockState.DockLeftAutoHide)
{
frmOutput.DockState = DockState.DockLeft;
}
else if (frmOutput.DockState == DockState.DockRightAutoHide)
{
frmOutput.DockState = DockState.DockRight;
}
else if (frmOutput.DockState == DockState.DockTopAutoHide)
{
frmOutput.DockState = DockState.DockTop;
}
}
frmOutput.Focus();
}
private void toolStripContainer1_TopToolStripPanel_SizeChanged(object sender, EventArgs e)
{
toolStripContainer1.Height = toolStripContainer1.TopToolStripPanel.Height;
}
#endregion
/// <summary>
/// 打开文件
/// </summary>
/// <param name="sender">按钮</param>
/// <param name="e">参数</param>
private void btnOpenFile_Click(object sender, EventArgs e)
{
string strFile = DocHelper.OpenDialog();
if (strFile == null)
{
return;
}
OpenFile(strFile);
}
/// <summary>
/// 为图形文件全路径名添加dfd后缀.
/// </summary>
private string processImageFiles(string fileName)
{
int index = fileName.LastIndexOf('.');
if (index <= 0)
{
return fileName;
}
string postFix = fileName.Substring(index);
postFix = postFix.ToUpper();
if (postFix == ".JPG")
{
return fileName + ".kev";
}
return fileName;
}
/// <summary>
/// 重新加载文件
/// </summary>
/// <param name="sender">按钮</param>
/// <param name="e">事件参数</param>
private void btnReloadFile_Click(object sender, EventArgs e)
{
viewerCur.ReloadFile();
viewerCur.ToolRedraw();
this.viewerCur.GetLengthAngleEvent += GetLengthAngle;
}
private void OpenFile(string strFile, bool setZColor = false)
{
if (this.ActiveDoc(strFile))
{
return;
}
MainView viewer = new MainView();
viewer.ToolMainVisible = false;
viewer.DrawerGotFocus += (object sender, EventArgs ea) =>
{
Trace.WriteLine("openFile:" + ea.ToString());
};
viewer.ShowStatisticResult += Viewer_ShowStatisticResult;
viewer.ZColorWidth = this.ZColorWidth;
if (viewer.OpenFile(strFile, setZColor) == false)
{
return;
}
SetRecentMenu(strFile);
strFile = processImageFiles(strFile);
mainViews.Add(viewer);
viewerCur = viewer;
//viewer.FileFullName = strFile;
createView(viewer, true);
viewer.FileModifyEvent += Viewer_FileModifyEvent;
this.viewerCur.GetLengthAngleEvent += GetLengthAngle;
}
//[JsonConverter(typeof(CPListConverter<CP>))]
List<CP> lst;
/// <summary>
/// 新建文件
/// </summary>
/// <param name="sender">新建文件按钮</param>
/// <param name="e">事件参数</param>
private void btnNewFile_Click(object sender, EventArgs e)
{
NewFile();
return;
//JObject obj1 = JObject.FromObject(new
//{ id = 1, name = "井号", X = 20, });
//JArray jArray = new JArray();
//jArray.Add(obj1);
//jArray.Add(obj1);
//string strData = jArray.ToString();
//object obj = JsonConvert.DeserializeObject(strData);
//List<CP> cps = new List<CP>();
//B1 b1 = new B1();
//B2 b2 = new B2();
//CP cP = new CP();
//cps.Add(b1);
//cps.Add(b2);
//cps.Add(cP);
//strData = JsonConvert.SerializeObject(cps);
//lst = JsonConvert.DeserializeObject<List<CP>>(strData, new CPListConverter<CP>());
//List<CP> lstOut = lst as List<CP>;
}
//[Json]
class CP
{
public string Test1 { get; set; } = "1";
public string Test2 { get; set; } = "2";
}
//[JsonObject(ItemConverterType =typeof(CP))]
class B1 : CP
{
public string TestB { get; set; } = "B1";
}
class B2 : CP
{
public string TestB2 { get; set; } = "B2";
}
//class CPListConverter<T> : JsonConverter
//{
// public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
// {
// var jObject = JObject.Load(reader);
// List<T> values = new List<T>();
// foreach (var item in jObject.Properties())
// {
// Type type = Type.GetType(item.Name);
// var value = item.Value.ToObject(type);
// values.Add((T)value);
// }
// return values;
// }
// public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
// {
// var values = (List<T>)value; JObject jObject = new JObject();
// foreach (var item in values)
// {
// jObject.Add(item.GetType().FullName, JToken.FromObject(item));
// }
// var p = jObject.Properties();
// foreach (var item in p)
// {
// //Debug.Log(item.Name);
// //Debug.Log(item.Name);
// }
// serializer.Serialize(writer, jObject);
// }
// public override bool CanConvert(Type objectType)
// {
// return true;
// }
//}
private void NewFile()
{
MainView viewer = new MainView();
viewer.DrawerGotFocus += (object senderDrawer, EventArgs ea) =>
{
Trace.WriteLine(ea.ToString());
};
string strPageText = "图形 " + (++nViewCount);
if (viewer.NewFile(strPageText) == false)
{
return;
}
mainViews.Add(viewer);
viewerCur = viewer;
createView(viewer, true);
viewer.FileModifyEvent += Viewer_FileModifyEvent;
this.viewerCur.GetLengthAngleEvent += GetLengthAngle;
}
private void Viewer_FileModifyEvent(object sender, bool isModified)
{
MainView view = sender as MainView;
FrmDoc docFind = dockPanel.Documents.FirstOrDefault(it =>
((FrmDoc)it).ID == view.ID) as FrmDoc;
docFind.Text = view.Text;
}
/// <summary>
/// 保存文件
/// </summary>
/// <param name="sender">保存文件按钮</param>
/// <param name="e">事件参数</param>
private void btnSaveFile_Click(object sender, EventArgs e)
{
if (viewerCur == null)
{
return;
}
if (viewerCur.SaveFile())
{
setTitle(viewerCur.FileFullName);
this.SetRecentMenu(viewerCur.FileFullName);
}
}
/// <summary>
/// 另存为
/// </summary>
/// <param name="sender">另存为按钮</param>
/// <param name="e">参数</param>
private void btnSaveAs_Click(object sender, EventArgs e)
{
if (viewerCur == null)
{
return;
}
if (viewerCur.SaveAs())
{
setTitle(viewerCur.FileFullName);
}
}
private void setTitle(string title)
{
FileInfo fi = new FileInfo(title);
((FrmDoc)dockPanel.ActiveDocument).Text = fi.Name;
}
/// <summary>
/// 显示统计面板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Viewer_ShowStatisticResult(object sender, EventArgs e)
{
ShowAndActiveOutput();
}
/// <summary>
/// 打印事件
/// </summary>
/// <param name="sender">打印按钮</param>
/// <param name="e">事件参数</param>
private void tsbPrint_Click(object sender, EventArgs e)
{
viewerCur.PrintDraw();
}
/// <summary>
/// 符号管理
/// </summary>
/// <param name="sender">符号管理事件</param>
/// <param name="e">事件参数</param>
private void btnSymbolManage_Click(object sender, EventArgs e)
{
this.viewerCur.ShowSymbolManager(null);
}
/// <summary>
/// 快捷键处理
/// </summary>
/// <param name="sender">当前窗体</param>
/// <param name="e">键盘事件</param>
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.O)
{
this.btnOpenFile_Click(null, null);
}
else if (e.KeyCode == Keys.S && e.Control)
{
this.btnSaveFile_Click(null, null);
}
else if (e.KeyCode == Keys.N && e.Control)
{
this.btnNewFile_Click(null, null);
}
else if (e.KeyCode == Keys.Escape)
{
if (viewerCur != null)
{
viewerCur.Escape();
}
}
}
/// <summary>
/// 拖放进入
/// </summary>
/// <param name="sender">窗体</param>
/// <param name="e">事件参数</param>
private void FormMain_DragEnter(object sender, DragEventArgs e)
{
// 表示接收到的数据是文件类型
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
// 设置鼠标效果
e.Effect = DragDropEffects.All;
}
}
/// <summary>
/// 拖放事件
/// </summary>
/// <param name="sender">窗体</param>
/// <param name="e">事件参数</param>
private void FormMain_DragDrop(object sender, DragEventArgs e)
{
// 取出拖放数据,返回类型为 Object需要强制转换成 string[] 类型
var ary = e.Data.GetData(DataFormats.FileDrop) as string[];
for (int i = 0; i < ary.Length; i++)
{
// 数组中的每个元素都是一个文件或目录的完整路径
var path = ary[i];
OpenFile(path);
}
}
private void tsmiLeftTop_Click(object sender, EventArgs e)
{
tsmiLeftTop.Checked = sender == tsmiLeftTop;
tsmiCenter.Checked = sender == tsmiCenter;
config.WriteString("Display", "ZoomMode", "0");
}
private void tsmiCenter_Click(object sender, EventArgs e)
{
tsmiCenter.Checked = sender == tsmiCenter;
tsmiLeftTop.Checked = sender == tsmiLeftTop;
config.WriteString("Display", "ZoomMode", "1");
}
private enum KeyModifiers
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
// id hot key identifier
[DllImport("user32.dll", SetLastError = true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private void RegisterHotKeys()
{
RegisterHotKey(Handle, 1001, KeyModifiers.Control, Keys.O);
RegisterHotKey(Handle, 1002, KeyModifiers.Control, Keys.S);
RegisterHotKey(Handle, 1003, KeyModifiers.Control, Keys.N);
RegisterHotKey(Handle, 1004, KeyModifiers.None, Keys.Escape);
}
private void UnRegisterHotKeys()
{
UnregisterHotKey(Handle, 1001);
UnregisterHotKey(Handle, 1002);
UnregisterHotKey(Handle, 1003);
UnregisterHotKey(Handle, 1004);
}
private void processHotKey(int hotKeyId)
{
if (hotKeyId == 1001)
{
string[] fileNames = DocHelper.SelectMutiFiles();
foreach (var item in fileNames)
{
if (string.IsNullOrEmpty(item))
{
OpenFile(item);
}
}
}
else if (hotKeyId == 1002)
{
//SaveFile();
if (viewerCur == null)
{
return;
}
if (viewerCur.SaveFile())
{
setTitle(viewerCur.FileFullName);
}
}
else if (hotKeyId == 1003)
{
NewFile();
}
else if (hotKeyId == 1004)
{
ProcessEscape();
}
}
protected override void WndProc(ref Message m)//监视Windows消息
{
const int WM_HOTKEY = 0x0312; //热键消息
if (m.Msg == WM_HOTKEY)
{
processHotKey(m.WParam.ToInt32());
}
base.WndProc(ref m);
}
private void FormMain_Activated(object sender, EventArgs e)
{
RegisterHotKeys();
}
private void FormMain_Deactivate(object sender, EventArgs e)
{
UnRegisterHotKeys();
}
private void ProcessEscape()
{
if (viewerCur == null)
return;
viewerCur.Escape();
}
private void tsbtnMerge_Click(object sender, EventArgs e)
{
var openFile = new OpenFileDialog();
openFile.Title = "合并文件";
openFile.Filter = "GeoSigma Files (*.gm*)|*.*";
openFile.FilterIndex = 0;
openFile.Multiselect = false;
if (openFile.ShowDialog() == DialogResult.OK)
{
viewerCur.FileMerge(openFile.FileName);
viewerCur.ToolRedraw();
}
}
private string GetFileName(string extName)
{
var sfd = new SaveFileDialog();
sfd.Filter = extName;
if (sfd.ShowDialog() != DialogResult.OK)
return "";
return sfd.FileName.ToString();
}
private void tsmiShow_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
this.viewerCur.FileSaveView(filePath);
}
private void tsmiFileSelectSave_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
this.viewerCur.FileSaveSelect(filePath);
}
private void tsmiPointSave_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (string.IsNullOrEmpty(filePath))
{
return;
}
this.viewerCur.FileSavePoint(filePath);
}
private void tsmiCurveSave_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (string.IsNullOrEmpty(filePath))
{
return;
}
this.viewerCur.FileSaveCurve(filePath);
}
private void tsmiSolidSave_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (!string.IsNullOrEmpty(filePath))
{
this.viewerCur.FileSaveSolid(filePath);
}
}
private void tsmiText_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (string.IsNullOrEmpty(filePath))
{
return;
}
this.viewerCur.FileSaveText(filePath);
}
private void tsmiCleanCurve_Click(object sender, EventArgs e)
{
string extName = "KE Files (*.kev)|*.kev|All file(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (string.IsNullOrEmpty(filePath))
{
return;
}
this.viewerCur.FileSaveCleanCurve(filePath);
}
private void tsmiXYZFSave_Click(object sender, EventArgs e)
{
string extName = "XYZ File (*.xyz)|*.xyz|All Files(*.*)|*.*||";
string filePath = this.GetFileName(extName);
if (string.IsNullOrEmpty(filePath))
{
return;
}
this.viewerCur.FileSaveToXyz(filePath);
}
private void tsmiSaveSymbol_Click(object sender, EventArgs e)
{
this.viewerCur.FileSaveToMark();
}
private void tsmiImport_Click(object sender, EventArgs e)
{
this.viewerCur.ImportDataFile();
}
private void tsmiExport_Click(object sender, EventArgs e)
{
this.viewerCur.ExportDataFile();
}
private void tsmiAbout_Click(object sender, EventArgs e)
{
FrmAbout frmAbout = new FrmAbout();
frmAbout.ShowDialog();
}
}
}