0

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

但是,我想将应用程序图标保留在表单的左上角。我知道是次要的,但细节对我来说很重要。

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

4

2 回答 2

2

是我使用的代码。

我的 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
于 2015-02-04T13:51:02.223 回答
1

我需要一个可以有条件地禁用关闭的选项(就像标准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)

As I was using it for a custom message box, I didn't test how to re-enable.

于 2016-07-11T16:01:00.583 回答