lixiaojun
2024-07-19 2415c4eedaa96532ffc7eb1c7e5d018106115bf4
Desktop/HStation.Desktop.Xhs.Core/MainForm.cs
@@ -1,19 +1,456 @@
using DevExpress.XtraBars.Docking;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Navigation;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors;
using HStation.WinFrmUI;
using System.IO;
using Yw.WinFrmUI;
using Yw.WinFrmUI.Page;
using static DevExpress.XtraPrinting.BarCode.DataBarExpandedPatternProcessor;
namespace HStation.Desktop
{
    public partial class MainForm : Form
    public partial class MainForm : RibbonForm
    {
        public MainForm()
        {
            InitializeComponent();
        }
        #region TileNavPane 相关处理
        //最大化最小化窗体
        private void nbNormal_ElementClick(object sender, NavElementEventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
        //最小化窗体
        private void nbMin_ElementClick(object sender, NavElementEventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }
        //关闭窗体
        private void nabClose_ElementClick(object sender, NavElementEventArgs e)
        {
            this.Close();
        }
        //移动窗体
        private void tileNavPane_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks >= 2)
            {
                if (this.WindowState == FormWindowState.Maximized)
                {
                    this.WindowState = FormWindowState.Normal;
                }
                else
                {
                    this.WindowState = FormWindowState.Maximized;
                }
            }
            else
            {
                var hitInfo = this.tileNavPane.CalcHitInfo(e.Location);
                if (hitInfo.InButton)
                    return;
                this.DragMove();
            }
        }
        //功能点击事件
        private void tileNavPane_ElementClick(object sender, NavElementEventArgs e)
        {
            var item = e.Element as NavButton;
            if (item.Tag != null)
            {
                if (item.Tag is int)
                {
                    // var modular = (eModular)(int)item.Tag;
                    // SelectModular(modular, null);
                }
            }
        }
        #endregion
        #region 窗体事件
        //界面记载事件
        private void MainForm_Load(object sender, EventArgs e)
        {
        }
        //界面显示事件
        private void MainForm_Shown(object sender, EventArgs e)
        {
            var homeGuid = new PageGuid()
            {
                Modular = "Xhs",
                MoudingType = eMoudingType.Tab,
                Function = "home"
            };
            var homePage = new HomeXhsProjectPage();
            this.CreatePage(homePage, homeGuid);
        }
        //界面正在退出时确认
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                if (XtraMessageBox.Show("是否关闭程序?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) != DialogResult.Yes)
                {
                    e.Cancel = true;
                    return;
                }
            }
        }
        //界面退出
        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
        }
        //窗体尺寸改变
        private void MainForm_Resize(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
            }
            else
            {
            }
        }
        #endregion
        #region TabbedView 相关事件处理程序
        //选中
        private void tabbedView1_DocumentSelected(object sender, DocumentEventArgs e)
        {
            if (e.Document == null)
                return;
            var surfaceGuid = e.Document.Tag as PageGuid;
            if (surfaceGuid == null)
                return;
            SelectModular(surfaceGuid.Modular, surfaceGuid);
        }
        //添加
        private void tabbedView1_DocumentAdded(object sender, DocumentEventArgs e)
        {
        }
        //激活
        private void tabbedView1_DocumentActivated(object sender, DocumentEventArgs e)
        {
            var page = e.Document.Control as DocumentPage;
            if (page == null)
                return;
        }
        //关闭 此时控件已经为空
        private void tabbedView1_DocumentClosed(object sender, DocumentEventArgs e)
        {
        }
        #endregion
        #region Page
        //是否存在Page
        private bool IsExistPage(PageGuid pguid, bool isActivePage)
        {
            if (pguid == null)
            {
                return false;
            }
            if (this.dockManager1.Panels != null && this.dockManager1.Panels.Count > 0)
            {
                foreach (DockPanel panel in this.dockManager1.Panels)
                {
                    if (panel.Tag != null)
                    {
                        if (panel.Tag is PageGuid)
                        {
                            if ((panel.Tag as PageGuid).ToString() == pguid.ToString())
                            {
                                if (isActivePage)
                                {
                                    panel.Show();
                                }
                                return true;
                            }
                        }
                    }
                }
            }
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                foreach (BaseDocument doc in this.tabbedView1.Documents)
                {
                    if (doc.Tag != null)
                    {
                        if (doc.Tag is PageGuid)
                        {
                            if ((doc.Tag as PageGuid).ToString() == pguid.ToString())
                            {
                                if (isActivePage)
                                {
                                    this.tabbedView1.Controller.Activate(doc);
                                }
                                return true;
                            }
                        }
                    }
                }
            }
            return false;
        }
        //更新数据
        private void RefreshPageData(PageGuid pguid)
        {
            if (pguid == null)
            {
                return;
            }
            if (this.dockManager1.Panels != null && this.dockManager1.Panels.Count > 0)
            {
                var panel = this.dockManager1.Panels.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                if (panel != null)
                {
                    if (panel.ControlContainer.Controls.Count > 0)
                    {
                        var page = panel.ControlContainer.Controls[0] as DocumentPage;
                        if (page != null)
                        {
                            page.RefreshData();
                        }
                    }
                    return;
                }
            }
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                if (doc != null)
                {
                    var page = doc.Control as DocumentPage;
                    if (page != null)
                    {
                        page.RefreshData();
                    }
                    return;
                }
            }
        }
        //更新PageTitle
        private void UpdatePageTitle(PageGuid pguid, PageTitle title)
        {
            if (pguid == null || title == null)
                return;
            if (this.dockManager1.Panels != null && this.dockManager1.Panels.Count > 0)
            {
                var panel = this.dockManager1.Panels.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                if (panel != null)
                {
                    panel.Text = title.Caption;
                    panel.ImageOptions.Image = title.HeaderImage;
                    panel.ImageOptions.SvgImage = title.HeaderSvgImage;
                    panel.ImageOptions.SvgImageSize = title.SvgImageSize;
                    if (panel.ControlContainer.Controls.Count > 0)
                    {
                        var page = panel.ControlContainer.Controls[0] as DocumentPage;
                        if (page != null)
                        {
                            page.PageTitle = title;
                        }
                    }
                    return;
                }
            }
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                if (doc != null)
                {
                    doc.Caption = title.Caption;
                    doc.ImageOptions.Image = title.HeaderImage;
                    doc.ImageOptions.SvgImage = title.HeaderSvgImage;
                    doc.ImageOptions.SvgImageSize = title.SvgImageSize;
                    var page = doc.Control as DocumentPage;
                    if (page != null)
                    {
                        page.PageTitle = title;
                    }
                    return;
                }
            }
        }
        //创建Page
        private bool CreatePage(DocumentPage page, PageGuid pguid)
        {
            if (page == null || pguid == null)
            {
                return false;
            }
            page.PageGuid = pguid;
            page.Dock = DockStyle.Fill;
            page.IsExistPageEvent += IsExistPage;
            page.CreatePageEvent += CreatePage;
            page.RefreshPageDataEvent += RefreshPageData;
            page.UpdatePageTitleEvent += UpdatePageTitle;
            page.ClosePageEvent += ClosePage;
            page.RegistEvents();
            switch (pguid.MoudingType)
            {
                case eMoudingType.Dock:
                    {
                        this.dockManager1.BeginUpdate();
                        DockPanel docPnl = null;
                        if (pguid.DockType == eDockType.Left)
                        {
                            docPnl = this.dockManager1.AddPanel(DockingStyle.Left);
                        }
                        else if (pguid.DockType == eDockType.Right)
                        {
                            docPnl = this.dockManager1.AddPanel(DockingStyle.Right);
                        }
                        docPnl.Footer = Directory.GetCurrentDirectory();
                        if (page.PageTitle != null)
                        {
                            docPnl.Text = page.PageTitle.Caption;
                            docPnl.ImageOptions.Image = page.PageTitle.HeaderImage;
                            docPnl.ImageOptions.SvgImage = page.PageTitle.HeaderSvgImage;
                            docPnl.ImageOptions.SvgImageSize = page.PageTitle.SvgImageSize;
                        }
                        docPnl.ControlContainer.Controls.Add(page);
                        docPnl.Tag = pguid;
                        this.dockManager1.EndUpdate();
                        docPnl.Show();
                    }
                    break;
                case eMoudingType.Tab:
                    {
                        this.tabbedView1.BeginUpdate();
                        var doc = this.tabbedView1.AddDocument(page);
                        doc.Footer = Directory.GetCurrentDirectory();
                        if (page.PageTitle != null)
                        {
                            doc.Caption = page.PageTitle.Caption;
                            doc.ImageOptions.Image = page.PageTitle.HeaderImage;
                            doc.ImageOptions.SvgImage = page.PageTitle.HeaderSvgImage;
                            doc.ImageOptions.SvgImageSize = page.PageTitle.SvgImageSize;
                        }
                        doc.Tag = pguid;
                        this.tabbedView1.EndUpdate();
                        this.tabbedView1.Controller.Activate(doc);
                    }
                    break;
                default: break;
            }
            page.InitialDataSource();
            return true;
        }
        //关闭Page
        private void ClosePage(PageGuid pguid)
        {
            switch (pguid.MoudingType)
            {
                case eMoudingType.Dock:
                    {
                        if (this.dockManager1.Panels != null && this.dockManager1.Panels.Count > 0)
                        {
                            var panel = this.dockManager1.Panels.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                            if (panel != null)
                            {
                                if (panel.Visibility != DockVisibility.Hidden)
                                    panel.Close();//会触发正在关闭和关闭事件
                                                  //this.dockManager1.RemovePanel(panel);//不会触发正在关闭和关闭事件
                            }
                        }
                    }
                    break;
                case eMoudingType.Tab:
                    {
                        if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
                        {
                            var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is PageGuid && (x.Tag as PageGuid).ToString() == pguid.ToString());
                            if (doc != null)
                            {
                                this.tabbedView1.Controller.Close(doc);//会触发正在关闭和关闭事件
                            }
                        }
                    }
                    break;
                default: break;
            }
        }
        #endregion
        #region Modular
        //选择模块
        private void SelectModular(string modular, PageGuid pageGuid = null)
        {
            //foreach (NavButton item in this.tileNavPane.Buttons)
            //{
            //    if (item.Tag == null)
            //        continue;
            //    if (item.Tag is int)
            //    {
            //        if (((int)item.Tag) == (int)modular)
            //        {
            //            item.Appearance.BackColor = Color.FromArgb(255, 128, 0);
            //        }
            //        else
            //        {
            //            item.Appearance.BackColor = Color.Transparent;
            //        }
            //    }
            //}
            //this.docPnlFuncList.Text = modular.ToString();
            //this.funcTreeListCtrl1.SetBindingData(modular, surfaceGuid);
            //if (surfaceGuid == null)
            //{
            //    var func = this.funcTreeListCtrl1.SetDefault();
            //    if (func != null)
            //        CreateFuncPage(func);
            //}
        }
        #endregion