Xuất bản ngày 12/01/2022
Views
Xóa nút [ X ] Trên Userform bằng VBA
Gần đây, trong khi làm việc trên một dự án, tôi đã nghĩ đến việc ẩn nút đóng [X] ở trên cùng của Biểu mẫu người dùng VBA. Tôi đã quản lý để xác định ba lựa chọn để đạt được điều này. Nếu bạn đang nghĩ về điều tương tự, thì bài đăng này sẽ cung cấp cho bạn câu trả lời mà bạn đang tìm kiếm.
MODEL
Worksheet
'Include this code at the top of the module
Private Const GWL_STYLE = -16
Private Const WS_CAPTION = &HC00000
Private Const WS_SYSMENU = &H80000
#If VBA7 Then
Private Declare PtrSafe Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long) As Long
Private Declare PtrSafe Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, _
ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare PtrSafe Function FindWindowA _
Lib "user32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare PtrSafe Function DrawMenuBar _
Lib "user32" (ByVal hWnd As Long) As Long
#Else
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 FindWindowA _
Lib "user32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function DrawMenuBar _
Lib "user32" (ByVal hWnd As Long) As Long
#End If
'Include this code in the same module as the API calls above
Public Sub SystemButtonSettings(frm As Object, SHOW As Boolean)
Dim windowStyle As Long
Dim windowHandle As Long
windowHandle = FindWindowA(vbNullString, frm.Caption)
windowStyle = GetWindowLong(windowHandle, GWL_STYLE)
If SHOW = False Then
SetWindowLong windowHandle, GWL_STYLE, (windowStyle And Not WS_SYSMENU)
Else
SetWindowLong windowHandle, GWL_STYLE, (windowStyle + WS_SYSMENU)
End If
DrawMenuBar (windowHandle)
End Sub
Private Sub CommandButton1_Click() Call SystemButtonSettings(Me, False) End Sub

