vb.net 从标题栏 .NET 视觉上删除/禁用关闭按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1743433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:31:15  来源:igfitidea点击:

Visually remove/disable close button from title bar .NET

vb.netmdititlebar

提问by Jrud

I have been asked to remove or disable the close button from our VB .NET 2005 MDI application. There are no native properties on a form that allow you to grey out the close button so the user cannot close it, and I do not remember seeing anything in the form class that will allow me to do this.

我被要求从我们的 VB .NET 2005 MDI 应用程序中删除或禁用关闭按钮。表单上没有本机属性允许您将关闭按钮灰显,因此用户无法关闭它,而且我不记得在表单类中看到任何允许我这样做的内容。

Is there perhaps an API call or some magical property to set or function to call in .NET 2005 or later to do this?

在 .NET 2005 或更高版本中是否可能有 API 调用或一些神奇的属性来设置或调用来执行此操作?

~~~~~~~~~~~~~~~~~~~~~~~~~~~~

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

More information:

更多信息:

I need to maintain the minimize/maximize functionality

我需要维护最小化/最大化功能

I need to maintain the original title bar because the form's drawing methods are already very complex.

我需要保持原来的标题栏,因为表单的绘制方法已经很复杂了。

回答by Philip Wallace

Based on the latest information you added to your question, skip to the end of my answer.

根据您添加到问题中的最新信息,请跳至我的回答末尾。



This is what you need to set to false: Form.ControlBox Property

这是您需要设置为 false 的内容: Form.ControlBox 属性

BUT, you will lose the minimize and maximize buttons as well as the application menu (top left).

但是,您将丢失最小化和最大化按钮以及应用程序菜单(左上角)。

As an alternative, override OnClose and set Cancel to true (C# example):

作为替代方案,覆盖 OnClose 并将取消设置为 true(C# 示例):

protected override void OnFormClosing(FormClosingEventArgs e)
{
    if (e.CloseReason != CloseReason.WindowsShutDown && e.CloseReason != CloseReason.ApplicationExitCall)
    {
        e.Cancel = true;
    }

    base.OnFormClosing(e);
}


If neither of these solutions are acceptable, and you must disable just the close button, you can go the pinvoke/createparams route:

如果这些解决方案都不可接受,并且您必须仅禁用关闭按钮,则可以使用 pinvoke/createparams 路线:

How to disable close button from window form using .NET application

如何使用 .NET 应用程序从窗口窗体禁用关闭按钮

This is the VB version of jdm's code:

这是jdm代码的VB版本:

Private Const CP_NOCLOSE_BUTTON As Integer = &H200
Protected Overloads Overrides ReadOnly Property CreateParams() As    CreateParams
   Get 
      Dim myCp As CreateParams = MyBase.CreateParams 
      myCp.ClassStyle = myCp.ClassStyle Or CP_NOCLOSE_BUTTON 
      Return myCp 
   End Get 
End Property 

回答by jdm

You can disable the close button and the close menu item in the system menu by changing the "class style" of the window. Add the following code to your form:

您可以通过更改窗口的“类样式”来禁用系统菜单中的关闭按钮和关闭菜单项。将以下代码添加到您的表单中:

const int CS_NOCLOSE = 0x200;

protected override CreateParams CreateParams {
    get {
        CreateParams cp = base.CreateParams;
        cp.ClassStyle |= CS_NOCLOSE;
        return cp;
    }
}

This will not just stop the window from getting closed, but it will actually grey out the button. It is C# but I think it should be easy to translate it to VB.

这不仅会阻止窗口关闭,而且实际上会使按钮变灰。它是 C#,但我认为将其转换为 VB 应该很容易。

回答by user2849882

Here is a simple way to remove the close button:
1. Select the Form
2. Now go to Properties.
3. Find ControlBoxand change the value to False.

这是删除关闭按钮的简单方法:
1. 选择表单
2. 现在转到Properties
3. 找到ControlBox并将值更改为False

This will remove all the Control Buttons (e.g. Minimize, Maximize, Exit)and also the icon also that is in the to left corner before the title.

这将删除所有控制按钮(例如最小化、最大化、退出)以及位于标题前左角的图标。

回答by Dave Swersky

You should be able to override the OnClose event of the form. This is common when an application minimizes to the System Tray when "closed".

您应该能够覆盖表单的 OnClose 事件。当应用程序在“关闭”时最小化到系统托盘时,这很常见。

回答by jmweb

When you press the X box on the form. The Form1_Closingis done first, then the Form1_Closedis done.

当您按下窗体上的 X 框时。在Form1_Closing首先进行,然后Form1_Closed就完成了。

The e.Cancel = Truein the Form1_Closing- prevents Form1_Closedfrom being called therefore, leaving your form still active.

e.Cancel = TrueForm1_Closing-防止Form1_Closed来自因此被称为,让你的形式仍然有效。

回答by user3781113

Prevent to close the form, but hide it:

防止关闭表单,但将其隐藏:

Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
    Me.WindowState = FormWindowState.Minimized 
    Me.Visible=false
    e.Cancel = True
End Sub

回答by jonaspp

You can set the ControlBoxproperty to False, but the whole title bar will be gone but the title itself...

您可以将该ControlBox属性设置为False,但整个标题栏将消失,但标题本身...

回答by azemoten

What jmweb said hereis OK as well. The X close button won't go if you cancel the event on form closing. But doing so, you need to release the processes the form needs and then closing the form.

jmweb在这里说的也可以。如果您在关闭表单时取消事件,则 X 关闭按钮不会消失。但是这样做,您需要释放表单所需的流程,然后关闭表单。

Me.Dispose()
Me.Close()

This worked for me using Menu Strip.

这对我使用菜单条有效。

回答by small_town_girl

Select (or click) the form itself Click on events in the property window (the little lightning bolt icon). Look for Form.Closingand double click it. Then type: e.cancel=true

选择(或单击)表单本身 单击属性窗口中的事件(小闪电图标)。寻找Form.Closing并双击它。然后输入:e.cancel=true

回答by Rahul Gurujala

Making a Form without a Titlebar in Visual Basic.

在 Visual Basic 中制作没有标题栏的表单。

Go to Form Propertiesand set both ControlBoxand ShowIconto false.

Form Properties和一套既ControlBoxShowIcon假。

Then, clear allthe text from the form's textproperty.

然后,清除表单属性中的所有文本text