ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Disable close button of Modeless Userform (https://www.excelbanter.com/excel-programming/276943-disable-close-button-modeless-userform.html)

RB Smissaert

Disable close button of Modeless Userform
 
Using Excel 2002 and 2000.
How do I disable the close button of a Modeless Userform?
I can do it with a modal userform like this:

Public Const MF_BYPOSITION = &H400
Public Const MF_REMOVE = &H1000
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, ByVal bRevert As
Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As
Long, ByVal wFlags As Long) _
As Long

Private Sub UserForm_Activate()

DisableCloseButton GetActiveWindow()

End Sub


Sub DisableCloseButton(hWnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION)
Call DrawMenuBar(hWnd)
End If

End Sub


But that doesn't work with a Modeless Userform.
Thanks for any advice.



RBS


RB Smissaert

Disable close button of Modeless Userform
 
Solved this one. Not sure what did the trick, probably putting DoEvents in
just the right place.

RBS

"RB Smissaert" wrote in message
...
Using Excel 2002 and 2000.
How do I disable the close button of a Modeless Userform?
I can do it with a modal userform like this:

Public Const MF_BYPOSITION = &H400
Public Const MF_REMOVE = &H1000
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, ByVal bRevert

As
Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition

As
Long, ByVal wFlags As Long) _
As Long

Private Sub UserForm_Activate()

DisableCloseButton GetActiveWindow()

End Sub


Sub DisableCloseButton(hWnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION)
Call DrawMenuBar(hWnd)
End If

End Sub


But that doesn't work with a Modeless Userform.
Thanks for any advice.



RBS



RB Smissaert

Disable close button of Modeless Userform
 
Rob,

I could do as you suggested, but in this particular case the form is just
holding a progressbar. Apart from this it doesn't do anything else. The form
will be unloaded when the procedure is finished.
Clicking the close button just doesn't serve any purpose and I might as well
disable it. It works fine now anyhow.

RBS


"Rob Bovey" wrote in message
...
Hi RB,

Instead of going through all that trouble, why not use the QueryClose
event to diable the *action* of clicking the X-close button, rather than
disabling the button itself, i.e.

Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode < vbFormCode Then Cancel = True
End Sub

Or instead (my preference), route any clicks to the X-close button through
whatever cancel procedure you've set up for your form, since this tends to
make for a more intuitive UI.

--
Rob Bovey, MCSE, MCSD, Excel MVP
Application Professionals
http://www.appspro.com/

* Please post all replies to this newsgroup *
* I delete all unsolicited e-mail responses *


"RB Smissaert" wrote in message
...
Using Excel 2002 and 2000.
How do I disable the close button of a Modeless Userform?
I can do it with a modal userform like this:

Public Const MF_BYPOSITION = &H400
Public Const MF_REMOVE = &H1000
Public Declare Function GetActiveWindow Lib "user32" () As Long
Public Declare Function DrawMenuBar Lib "user32" _
(ByVal hWnd As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long
Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hWnd As Long, ByVal bRevert

As
Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition

As
Long, ByVal wFlags As Long) _
As Long

Private Sub UserForm_Activate()

DisableCloseButton GetActiveWindow()

End Sub


Sub DisableCloseButton(hWnd As Long)

Dim hMenu As Long
Dim menuItemCount As Long

hMenu = GetSystemMenu(hWnd, 0)

If hMenu Then
menuItemCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, menuItemCount - 1, _
MF_REMOVE Or MF_BYPOSITION)
Call DrawMenuBar(hWnd)
End If

End Sub


But that doesn't work with a Modeless Userform.
Thanks for any advice.



RBS






All times are GMT +1. The time now is 06:48 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com