.net - 如何在关闭 xcool 窗口窗体时停止关闭整个应用程序

标签 .net winforms

我有一个像 CMS 这样的项目,我正在开发它,并且我添加了一个主题 xcool。

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

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

我尝试过的:

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"));

最佳答案

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

  1. 如果您查看 XCoolForm 的源代码,请查看 XCoolForm.csOnMouseDown 事件处理程序下的内容。有两个地方检查单击的按钮是否是关闭按钮(第 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 以便您可以访问它们。在 XCoolForm.cs 中,将第 63 行的 m_xTitleBar 从私有(private)更改为 protected :

    protected XTitleBar m_xTitleBar = new XTitleBar();
    

    并在第 935 行将 PointInRect 函数从私有(private)更改为 protected :

    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);
    } 
    

关于.net - 如何在关闭 xcool 窗口窗体时停止关闭整个应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52178868/

相关文章:

c# - 不能在 Style Setter 上设置 TargetName 属性,那么它是如何设置的呢?

.net - Flurl.Http 自定义错误

c# - 使用带反射的 T4 .tt 模板

c# - 当传入串口的数据是异步的时,如何通过 Web 服务存储数据?

c# - 精确缩放图像

.net - 创建与 ASPNetUsers 表和自定义的关系

c# - c# 中的 ShowDialog() 和 ShowDialog(IWin32Window) 有什么区别?

c# - 如何使用 public get set 从 Form 2 启用 Form 1 中的按钮?

c# - 在面板上绘画允许自动滚动

c# - DataGridView 滚动行为