禁用关闭按钮而不隐藏控制框

Disable the Close button without hiding the Controlbox

我有一个应用程序,我不希望用户能够关闭表单。好的,很简单。只需为表单设置 ControlBox = false 即可。

不过,我想将应用程序图标保留在表格的左上角。我知道未成年人,但细节对我来说很重要。

设置 Controlbox = false 还会使应用程序的图标从表单的左上角消失。有解决办法吗??

Here 是我使用的代码。

我的 VB.Net 版本。

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

我需要一个有条件地禁用关闭的选项(就像标准 MessageBoxYesNo 问题所做的那样)所以接受的答案对我不起作用或者我可能没有看到我怎样才能让它工作。我最终得到了这个

Private Const MF_BYPOSITION As Int32 = &H400
Private Const MF_REMOVE As Int32 = &H1000
Private Declare Auto Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal bRevert As Int32) As IntPtr
Private Declare Auto Function GetMenuItemCount Lib "user32.dll" (ByVal hMenu As IntPtr) As Int32
Private Declare Function DrawMenuBar Lib "user32.dll" (ByVal hwnd As IntPtr) As Boolean
Private Declare Auto Function RemoveMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Int32, ByVal wFlags As Int32) As Int32

Public Sub DisableCloseButton(ByVal hwnd As IntPtr)
    Dim hMenu As IntPtr, n As Int32
    hMenu = GetSystemMenu(hwnd, 0)
    If Not hMenu.Equals(IntPtr.Zero) Then
        n = GetMenuItemCount(hMenu)
        If n > 0 Then
            RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)
            RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)
            DrawMenuBar(hwnd)
        End If
    End If
End Sub

通过

调用

DisableCloseButton(MyForm.Handle)

由于我将它用于自定义消息框,因此我没有测试如何重新启用。