管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-09-03 596f3ad8d0a3b8d4641df16ba5b17f6a2a5090a7
TEWin/FrmWin.cs
@@ -9,6 +9,7 @@
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using TerraExplorerX;
using TETree;
@@ -33,9 +34,13 @@
        public SGWorld71 SG;
        System.Timers.Timer timer;
        ITerrainLabel71 angleLabel;
        ITerrainPolyline71 angleLine;
        string startName = "OnStart";
        string angleGroupName = "角量算";
@@ -43,17 +48,15 @@
        string mainUrl = Application.StartupPath;
        System.Timers.Timer timer;
        public FrmWin()
        {
            InitializeComponent();
            //this.KeyPreview = true;
            this.Load += FrmWeb_Load;
            //this.KeyDown += FrmWin_KeyDown;
            this.FormClosed += FrmWin_FormClosed;
            this.Load += FrmWeb_Load;
            this.FormClosing += FrmWin_FormClosing;
            timer = new System.Timers.Timer(1500);
            timer = new System.Timers.Timer(1000);
            timer.Elapsed += timer_Elapsed;
        }
@@ -115,15 +118,30 @@
        void SG_OnLoadFinished(bool bSuccess)
        {
            this.SG.OnLoadFinished -= SG_OnLoadFinished;
            try
            {
                this.SG.OnLoadFinished -= SG_OnLoadFinished;
                string id = this.SG.ProjectTree.FindItem(startName);
                if (string.IsNullOrEmpty(id)) return;
                var obj = this.SG.ProjectTree.GetObject(id);
                if (obj.ObjectType == ObjectTypeCode.OT_LOCATION)
                {
                    this.SG.Navigate.FlyTo(id);
                }
            }
            catch { }
        }
        void FrmWin_FormClosed(object sender, FormClosedEventArgs e)
        void FrmWin_FormClosing(object sender, FormClosingEventArgs e)
        {
            ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
            LogOut.Info("关闭程序.");
        }
        #endregion
        #region invoke
        protected override void DefWndProc(ref Message m)
        {
            switch (m.Msg)
@@ -155,7 +173,7 @@
                case 0xc1: // 打开数据方案/打开显示场景
                    SG.Command.Execute(1001, null); break;
                case 0xc2: // 保存数据方案/保存显示场景
                    SG.Command.Execute(1003, null); break;
                    Save(); break;
                case 0xc3: // 矢量数据(默认为*.shp,支持Excel)
                    SG.Command.Execute(1013, 5); break;
                case 0xc4: // 影像数据(默认为*.tif)
@@ -208,8 +226,17 @@
                    LoadShp(); break;
                case 0xd9: // 读取shp
                    ReadShp(); break;
                case 0xda: // 按照属性显示
                    DisplayByAttribute(0, "按照属性显示"); break;
                case 0xdb: // 按照属性设置颜色
                    DisplayByAttribute(1, "按照属性设置颜色"); break;
                case 0xdc: // 打开数据方案
                    OpenPrj(); break;
                case 0xdd: // 保存项目
                    SavePrj(); break;
            }
        }
        #endregion
        #region user32.dll
        public const int SWP_NOSIZE = 0x0001;
@@ -261,18 +288,79 @@
                }
            }
        }
        #endregion
        public static void Main()
        #region OSGBLab
        const int GCLP_HICON = -14;
        const int ICON_SMALL = 0;
        const int ICON_BIG = 1;
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowsName);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr FindWindowEx(IntPtr hWndParent, IntPtr hWndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll", SetLastError = true)]
        static extern bool SetWindowText(IntPtr hWnd, String lpString);
        [DllImport("user32.dll", SetLastError = true)]
        public static extern IntPtr SetClassLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        public void DealOsgbLab()
        {
            // 获取指定窗体的句柄
            //IntPtr hWnd = // 获取窗体句柄的方法,例如FindWindow或者通过进程名获取
            // 获取窗体的坐标
            //RECT rect;
            //GetWindowRect(hWnd, out rect);
            //Console.WriteLine("窗体坐标: 左上角({0}, {1}), 右下角({2}, {3})", rect.Left, rect.Top, rect.Right, rect.Bottom);
            /*Process[] processes = Process.GetProcessesByName("OSGBLab");
            if (processes.Length > 0)
            {
                IntPtr hWnd = processes[0].MainWindowHandle;
                SetWindowText(hWnd, "OSGB数据处理工具");
            }*/
            IntPtr hWnd = FindWindow(null, "OSGBLab(倾斜伴侣) (Windows) 免费版");
            if (hWnd == IntPtr.Zero) return;
            DealOsgbLab(hWnd);
        }
        public void DealOsgbLab(Process p)
        {
            do
            {
                if (p.MainWindowHandle != IntPtr.Zero)
                {
                    DealOsgbLab(p.MainWindowHandle);
                    break;
                }
                Thread.Sleep(50);
            } while (true);
        }
        [DllImport("user32.dll", EntryPoint = "SetClassLongPtr", SetLastError = true)]
        static extern IntPtr SetClassLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
        [DllImport("user32.dll", EntryPoint = "SetClassLong", SetLastError = true)]
        static extern int SetClassLong32(IntPtr hWnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll")]
        static extern IntPtr LoadIcon(IntPtr hInstance, IntPtr iconName);
        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        public void DealOsgbLab(IntPtr hWnd)
        {
            //ShowWindow(hWnd, 0); // 5
            SetWindowText(hWnd, ConfigurationManager.AppSettings["osgbLabTitle"]);
            Icon icon = new Icon(Path.Combine(mainUrl, @"Resources\favicon.ico"));
            SetClassLongPtr(hWnd, GCLP_HICON, icon.Handle);
            //SetClassLongPtr(hWnd, GCLP_HICON + ICON_BIG, icon.Handle);
        }
        #endregion
        #region ShowHtml+RunExe
        public void ShowHtmlByid(int id, int arg)
        {
            switch (id)
@@ -320,7 +408,7 @@
            SG.Window.ShowPopup(popup);
        }
        public void RunExe(string path, string args, bool noWindow = true)
        public Process RunExe(string path, string args, bool noWindow = true)
        {
            Process p = new Process();
            p.StartInfo.FileName = path;
@@ -328,6 +416,8 @@
            p.StartInfo.CreateNoWindow = noWindow;
            p.StartInfo.UseShellExecute = false;
            p.Start();
            return p;
        }
        public static string ExecCmd(List<string> list)
@@ -375,6 +465,7 @@
            return str;
        }
        #endregion
        #region 角量算
        private void AngleMeasurement()
@@ -562,6 +653,7 @@
        }
        #endregion
        #region 调用Html
        private string GetAbsolutePath(string key)
        {
            string str = ConfigurationManager.AppSettings[key];
@@ -582,8 +674,15 @@
        private void InvokeOsgbLab()
        {
            ExecCmd(new List<string> { "taskkill /f /t /im OSGBLab.exe" });
            RunExe(GetAbsolutePath("osgblabPath"), null, false);
            Process p = RunExe(GetAbsolutePath("osgblabPath"), null, false);
            DealOsgbLab(p);
        }
        private void DisplayByAttribute(int type, String name)
        {
            this.ShowHtml(name, mainUrl + @"\Resources\DisplayByAttribute\DisplayByAttribute.html?Type=" + type + "&lang=2052", 20, 20, 365, 355);
        }
        #endregion
        #region 加载shp + 读取shp
        private void LoadShp()
@@ -686,5 +785,84 @@
            return shpTxtPath;
        }
        #endregion
        #region 方案管理
        private void Save()
        {
            //SG.Command.Execute(1003, null);
            SaveLocation();
            this.SG.Project.Save();
        }
        private void OpenPrj()
        {
            string flyPath = ReadPrjPath();
            if (String.IsNullOrEmpty(flyPath)) flyPath = Path.Combine(mainUrl, "see.fly");
            if (!File.Exists(flyPath))
            {
                flyPath = ConfigurationManager.AppSettings["fly"] ?? ConfigurationManager.AppSettings["testFly"];
                if (!flyPath.Contains(":\\")) flyPath = Path.Combine(mainUrl, flyPath);
            }
            this.SG.Open(flyPath);
        }
        private void SavePrj()
        {
            try
            {
                string flyPath = ReadPrjPath();
                if (String.IsNullOrEmpty(flyPath)) flyPath = Path.Combine(mainUrl, "see.fly");
                this.Save();
                //this.SG.Project.SaveAs(flyPath);
                if (this.fly.Equals(flyPath)) return;
                if (File.Exists(flyPath)) File.Delete(flyPath);
                string cmd = string.Format("mklink \"{0}\" \"{1}\"", flyPath, this.fly);
                ExecCmd(new List<string> { cmd });
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private void SaveLocation()
        {
            try
            {
                string id = this.SG.ProjectTree.FindItem(startName);
                if (!string.IsNullOrEmpty(id)) this.SG.ProjectTree.DeleteItem(id);
                var pos = this.SG.Navigate.GetPosition();
                var loc = this.SG.Creator.CreateLocationHere(this.SG.ProjectTree.RootID, startName);
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private String ReadPrjPath()
        {
            String txt = Path.Combine(mainUrl, "fly.txt");
            if (!File.Exists(txt)) return null;
            try
            {
                using (StreamReader sr = new StreamReader(txt))
                {
                    return sr.ReadLine();
                }
            }
            catch (Exception ex)
            {
                LogOut.Error(ex.Message + "\r\n" + ex.StackTrace);
                return null;
            }
        }
        #endregion
    }
}