我想根据条件禁用窗体上的X按钮。所以类似于:
If Boolean Then
ControlBox = False
Else
ControlBox = True
End If
当我尝试使用此功能时,我收到一条错误消息,说visual basic不支持此功能。这将在表单加载时完成,布尔值不会更改。
我已经搜索了这个论坛,但找不到满足我需求的答案。
提前谢谢。
看起来您正试图在运行时设置ControlBox属性。正如你所看到的,你不能这样做。然而,只要有一点API的魔力,你就可以完成这项任务:
Option Explicit
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const WS_SYSMENU = &H80000
Private Const GWL_STYLE = (-16)
Private Const SWP_FRAMECHANGED = &H20
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Private Const SWP_NOSIZE = &H1
Private Property Let ControlBoxVisible(ByVal Value As Boolean)
Dim style As Long
style = GetWindowLong(Me.hWnd, GWL_STYLE)
style = IIf(Value, style Or WS_SYSMENU, style And Not WS_SYSMENU)
SetWindowLong Me.hWnd, GWL_STYLE, style
SetWindowPos Me.hWnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOZORDER Or SWP_NOSIZE
End Property
然后你会这样使用它:
Private Sub Form_Load()
ControlBoxVisible = False
End Sub
使用API调用。查看启用/禁用表单关闭按钮
有一个压缩的项目文件,其中包含源代码。