- Tham gia
- 20/9/06
- Bài viết
- 793
- Được thích
- 1,287
- Nghề nghiệp
- ĐCTV - ĐCCT
Nút X chính là nút đóng chương trình (góc phải trên cùng trong UserForm)
1. Vô hiệu hoá nút X, nút X vẫn hiện rõ
2. Vô hiệu hoá nút X, nút X mờ đi
3. Ẩn nút X
Trong VB để ẩn các nút phóng to (maximize), thu nhỏ (minimize), đóng (Close) của đối tượng Form ta đặt thuộc tính ControlBox=False. Nhưng trong VBA thì khác:
(Sưu tầm trên Internet)
1. Vô hiệu hoá nút X, nút X vẫn hiện rõ
Mã:
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then Cancel = True
End Sub
Mã:
Option Explicit
'Copyright © 2005 by RobDog888 (VB/Office Guru™). All Rights reserved.
'
'Distribution: You can freely use this code in your own
' applications provided that this copyright
' is left unchanged, but you may not reproduce
' or publish this code on any web site, online
' service, or distribute as source on any
' media without express permission.
'
'Add a Command Button so you can close the userform
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Const MF_BYPOSITION = &H400&
'
Private Sub UserForm_Initialize()
Dim lHwnd As Long
lHwnd = FindWindow("ThunderDFrame", "UserForm1")
Do While lHwnd = 0
lHwnd = FindWindow("ThunderDFrame", "UserForm1")
DoEvents
Loop
RemoveMenu GetSystemMenu(lHwnd, 0), 6, MF_BYPOSITION
End Sub
Trong VB để ẩn các nút phóng to (maximize), thu nhỏ (minimize), đóng (Close) của đối tượng Form ta đặt thuộc tính ControlBox=False. Nhưng trong VBA thì khác:
Mã:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long
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 Const WS_SYSMENU As Long = &H80000
Private Const GWL_STYLE As Long = -16&
Private Sub UserForm_Initialize()
HideCloseBox Me
End Sub
Private Sub HideCloseBox(ByVal UserForm As UserForm)
Dim hWnd As Long
Dim WindowStyle As Long
If Application.Version < 9 Then
hWnd = FindWindow("ThunderXFrame", UserForm.Caption)
Else
hWnd = FindWindow("ThunderDFrame", UserForm.Caption)
End If
If hWnd <> 0 Then
WindowStyle = GetWindowLong(hWnd, GWL_STYLE)
SetWindowLong hWnd, GWL_STYLE, WindowStyle And Not WS_SYSMENU
End If
End Sub