窗体中的关闭按钮

窗体中的关闭按钮

本文介绍了禁用 Windows 窗体中的关闭按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在互联网上找到了此代码,但不确定如何使用它.此外,我需要在工作完成后重新启用它.请帮忙.

I found this code over the internet but am unsure about how to use it. Also I need to enable it back after work is complete. Help please.

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

推荐答案

您可以将其粘贴到表单的代码中以使用它.然而,这将永久禁用关闭按钮.动态执行需要非常不同的代码,您必须修改系统菜单.将此代码粘贴到您的表单中并在您的逻辑中使用 CloseEnabled 属性:

You would paste this into your form's code to use it. This however permanently disables the close button. Doing it dynamically requires very different code, you have to modify the system menu. Paste this code into your form and use the CloseEnabled property in your logic:

Public Property CloseEnabled() As Boolean
    Get
        Return mCloseEnabled
    End Get
    Set(ByVal value As Boolean)
        If value <> mCloseEnabled Then
            mCloseEnabled = value
            setSystemMenu()
        End If
    End Set
End Property
Private mCloseEnabled As Boolean = True

Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
    MyBase.OnHandleCreated(e)
    setSystemMenu()
End Sub

Private Sub setSystemMenu()
    Dim menu As IntPtr = GetSystemMenu(Me.Handle, False)
    Dim enable As Integer
    If Not mCloseEnabled Then enable = 1
    EnableMenuItem(menu, SC_CLOSE, enable)
End Sub

'' P/Invoke declarations
Private const SC_CLOSE As Integer = &hf060
Private Declare Function GetSystemMenu Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
Private Declare Function EnableMenuItem Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal IDEnableItem As Integer, ByVal wEnable As Integer) As Integer

这篇关于禁用 Windows 窗体中的关闭按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:22