首页 > 解决方案 > 如何在关闭 xcool 窗口窗体时停止关闭整个应用程序

问题描述

我有一个像 CMS 这样的项目,我正在研究它,我已经添加了一个主题 xcool。

这里有 50 个窗口窗体,但现在的问题是,在关闭一个窗口窗体时,整个应用程序正在关闭。

当我删除这个主题时,它工作得很好。我没有找到任何主题的关闭方法等。

我试过的:

using System.IO;

namespace CampusManagement
{
    public partial class Student_Reg : XCoolForm.XCoolForm

    private XmlThemeLoader xtl = new XmlThemeLoader();


    this.TitleBar.TitleBarBackImage = CampusManagement.Properties.Resources.predator_256x256;
    this.MenuIcon = CampusManagement.Properties.Resources.alien_vs_predator_3_48x48.GetThumbnailImage(24, 24, null, IntPtr.Zero);
    xtl.ThemeForm = this;
    this.Border.BorderStyle = XCoolForm.X3DBorderPrimitive.XBorderStyle.Flat;
    this.TitleBar.TitleBarBackImage = CampusManagement.Properties.Resources.Mammooth_1;
    this.TitleBar.TitleBarCaption = "Campus Management System";
    xtl.ApplyTheme(Path.Combine(Environment.CurrentDirectory, @"..\..\Themes\BlueWinterTheme.xml"));

标签: .netwinforms

解决方案


有两种主要方法可以解决此问题。

  1. 如果您在事件处理程序XCoolForm.cs下查看 XCoolForm 的源代码。OnMouseDown它在两个地方检查单击的按钮是否是关闭按钮(第 312 行和第 353 行)。如果单击关闭按钮,它将退出应用程序。

    else if (xbtn.XButtonType == XTitleBarButton.XTitleBarButtonType.Close)
    {
        Application.Exit();
    }
    

    你想改为Application.Exit()改为Close()

    else if (xbtn.XButtonType == XTitleBarButton.XTitleBarButtonType.Close)
    {
        Close();
    }
    
  2. 另一种选择是覆盖 OnMouseDown 事件。但是您需要制作m_xTitleBarPointInRect保护,以便您可以访问它们。在第 63 行从私有XCoolForm.cs更改为受保护:m_xTitleBar

    protected XTitleBar m_xTitleBar = new XTitleBar();
    

    PointInRect在第 935 行将函数从私有更改为受保护:

    protected bool PointInRect(Point p, Rectangle rc)
    

    然后在您的表单中,您可以像这样覆盖鼠标按下事件:

    protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
    {
        foreach (XTitleBarButton xbtn in m_xTitleBar.TitleBarButtons)
        {
            if (PointInRect(
                e.Location,
                new Rectangle(
                    xbtn.XButtonLeft,
                    xbtn.XButtonTop,
                    xbtn.XButtonWidth,
                    xbtn.XButtonHeight
                )))
            {
                  // We just want to check if it was the close button that was clicked, if so then we close this form.
                  if (xbtn.XButtonType == XTitleBarButton.XTitleBarButtonType.Close)
                  {
                      Close();
                      return;
                  }
            }
        }
    
        // It wasn't the close button that was clicked, so run the base handler and let it take care of the button click.   
        base.OnMouseDown(e);
    } 
    

推荐阅读