View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Leith Ross[_2_] Leith Ross[_2_] is offline
external usenet poster
 
Posts: 128
Default Removing a control

On Oct 8, 4:50 pm, JW wrote:
On Oct 8, 6:45 pm, Mekinnik
wrote:

How do I remove the close control from the upper right corner of my user form?


While it is possible to remove the control completely via API calls
and such, it is much easier to just capture the event when the user
clicks on the close button and handle it.
Private Sub UserForm_QueryClose _
(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
MsgBox "Hey, don't do that!!!!"
Cancel = True
End If
End Sub


Hello Mekinnik,

Here is the API version to disable the "Close Box". Just add the
statement "DisableX" to your UserForm's Activate event. Copy the macro
code add paste it into a Standard VBA module.

'Start of Macro Code
'Written: October 08, 2007
'Author: Leith Ross
'Summary: Disables the Close Box on the UserForm. Add the statement
' DisableX to the UserForm_Activate ent procedure.

' Define API Constants
Public Const MF_BYPOSITION = &H400&
Public Const MF_DISABLED = &H2&

' Define the API calls needed
Private Declare Function GetSystemMenu _
Lib "user32.dll" _
(ByVal hWnd As Long, _
ByVal bRevert As Long) As Long

Private Declare Function GetMenuItemCount _
Lib "user32.dll" _
(ByVal hMenu As Long) As Long

Private Declare Function RemoveMenu _
Lib "user32.dll" _
(ByVal hMenu As Long, _
ByVal nPosition As Long, _
ByVal wFlags As Long) As Long

Private Declare Function DrawMenuBar _
Lib "user32.dll" _
(ByVal hWnd As Long) As Long

' Returns the Window Handle of the Window accepting input
Private Declare Function GetForegroundWindow _
Lib "user32.dll" () As Long

Public Sub DisableX()

Dim hWnd As Long
Dim hMenu As Long, nCount As Long
Dim Ret As Long

' Get the Window Handle of the active UserForm
hWnd = GetForegroundWindow()

' Get handle to system menu
hMenu = GetSystemMenu(hWnd, 0)

' Get number of items in menu
nCount = GetMenuItemCount(hMenu)

' Remove last item from system menu (last item is 'Close')
Ret = RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or
MF_BYPOSITION)

' Redraw menu
Ret = DrawMenuBar(hWnd)

End Sub
'End of Macro Code

Sincerely,
Leith Ross