Disable Minimize & Maximize Title Bar Buttons

retiredrhb

New Member
Joined
Feb 15, 2003
Messages
4
The following code, posted by Raider, successfully disables the famous (X) close button at the top right corner of the Excel window. What changes to this code would I make to disable the Minimize or Maximize buttons at the top right corner of the Excel 2000 Window?

Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function FindWindowA Lib "user32" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Sub Workbook_Open()
Dim MyHandle
Dim hWnd As Long
MyCap$ = Application.Caption
hWnd = FindWindowA(vbNullString, MyCap$)
MyHandle = GetSystemMenu(hWnd, 0)
Call RemoveMenu(MyHandle, 6, &H400)
End Sub
This message was edited by retiredrhb on 2003-02-16 17:01
This message was edited by retiredrhb on 2003-02-16 17:03
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Howdy Retire, you're not out ice-fishing? :biggrin: This is a slightly modified piece of code provided by Andrew Baker and published in David Hager's EEE newsletter archived on John Walkenbach's site:<pre>
Option Explicit

Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Long, ByVal _
bRevert As Long) As Long
Declare Function DeleteMenu Lib "User32" (ByVal hMenu As Long, ByVal _
nPosition As Long, ByVal wFlags As Long) As Long
Declare Function FindWindowA Lib "User32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Const MF_BYPOSITION As Long = &H400
Const mlNUM_SYS_MENU_ITEMS As Long = 7

Sub DisableActiveDialogMenuControls()
Dim DialogCaption As String
Dim lHandle As Long, lcount As Long
On Error Resume Next
DialogCaption = Application.Caption
DialogCaption = DialogCaption & vbNullChar
lHandle = FindWindowA(vbNullString, DialogCaption)
If lHandle<> 0 Then
For lcount = 1 To mlNUM_SYS_MENU_ITEMS
DeleteMenu GetSystemMenu(lHandle, False), 0, MF_BYPOSITION
Next lcount
End If
End Sub

Sub EnableActiveDialogMenuControls()
Dim lHandle As Long
Dim DialogCaption As String
On Error Resume Next
DialogCaption = Application.Caption
DialogCaption = DialogCaption & vbNullChar
lHandle = FindWindowA(vbNullString, DialogCaption)
GetSystemMenu lHandle, True
End Sub</pre>

The buttons appear to be functional, but are not.

Hope this helps.


Edit: This gets the min, max & close, if you just want the first two, make the following tweak:<pre>
Const mlNUM_SYS_MENU_ITEMS As Long = 6</pre>

Edit2: Here's a non-loop variety (just min & max):<pre>
Option Explicit

Declare Function GetActiveWindow Lib "User32" () As Integer
Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Integer, _
ByVal bRevert As Integer) As Integer
Declare Function DeleteMenu Lib "User32" (ByVal hMenu As Integer, _
ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer

Sub killmenu()
DeleteMenu GetSystemMenu(GetActiveWindow, 0), 0, 1024
DeleteMenu GetSystemMenu(GetActiveWindow, 0), 2, 1024
End Sub

Sub Restoremenu()
GetSystemMenu GetActiveWindow, True
GetSystemMenu GetActiveWindow, True
End Sub</pre>

_________________
Cheers,<font size=+2><font color="red"> Nate<font color="blue"> Oliver</font></font></font>
This message was edited by nateo on 2003-02-18 10:48
 
Upvote 0
Thanks Nate. I used the top module with a loop of 1 to 7 to disable Minimize, Maximize and Close. Works great! I used to ice fish but not anymore, easier to go to the grocery store. Are you doing this message board for a living or just a hobby? Richard
 
Upvote 0
Hi Richard, incidentally, welcome to the board. :)

It sure is, it's bollox cold out there. :biggrin:

Thanks Nate. Are you doing this message board for a living or just a hobby? Richard

You're welcome. It's more of a hobby, but it feels like a living. :wink:
 
Upvote 0
Works on WinME but not on WinXP

Nate,
As I mentioned to you on a prior post, the following statements you supplied me worked GREAT on Windows ME with Office 2000 Premium. :D However, when I tested it on a system with Windows XP and the same Office 2000 Premium the results were not as good. The (X) Close disabled OK but not the Minimize and Maximize on the top right on the Excel Title Bar. I tried changing the loop of "1 to 7" to 1 to even higher numbers but no go. Maybe something in the "Declare" has to be changed?

Option Explicit

Declare Function GetSystemMenu Lib "User32" (ByVal hwnd As Long, ByVal _
bRevert As Long) As Long
Declare Function DeleteMenu Lib "User32" (ByVal hMenu As Long, ByVal _
nPosition As Long, ByVal wFlags As Long) As Long
Declare Function FindWindowA Lib "User32" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long

Const MF_BYPOSITION As Long = &H400
Const mlNUM_SYS_MENU_ITEMS As Long = 7


Sub DisableCloseMenuControls()
Dim DialogCaption As String
Dim lHandle As Long, lcount As Long
' Disbles Minimize, Maximize & Close Menu and Buttons
On Error Resume Next
DialogCaption = Application.Caption
DialogCaption = DialogCaption & vbNullChar
lHandle = FindWindowA(vbNullString, DialogCaption)
If lHandle <> 0 Then
For lcount = 1 To mlNUM_SYS_MENU_ITEMS
DeleteMenu GetSystemMenu(lHandle, False), 0, MF_BYPOSITION
Next lcount
End If
End Sub


Brrrr, its cold outside. Thanks, Richard
 
Upvote 0
The code listed by NateO above, which I have relisted below, works great for my Excel Application, but does not disable my Workbook Min/Max/Close buttons (the second row) - at least on my machine (Excel 2000). What modifications would you make to the code so that it would disable/enable the workbook as well? (Please include the Enable code)

Appreciate your help.

' Disable/Enable Min/Max/Close
Option Explicit

Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long

Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal _
nPosition As Long, ByVal wFlags As Long) As Long

Declare Function FindWindowA Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Declare Function GetActiveWindow Lib "user32" () As Integer

Const MF_BYPOSITION As Long = &H400
Const mlNUM_SYS_MENU_ITEMS As Long = 7 ' all 3 buttons (6 for just MM)

Public Sub DisableActiveDialogMenuControls()
Dim DialogCaption As String
Dim lHandle As Long, lcount As Long

On Error Resume Next
DialogCaption = Application.Caption
DialogCaption = DialogCaption & vbNullChar

lHandle = FindWindowA(vbNullString, DialogCaption)

If lHandle <> 0 Then
For lcount = 1 To mlNUM_SYS_MENU_ITEMS
DeleteMenu GetSystemMenu(lHandle, False), 0, MF_BYPOSITION
Next lcount
End If
End Sub


' reenable the Min/Max/Close buttons for Excel application
Private Sub EnableActiveDialogMenuControls()
Dim lHandle As Long
Dim DialogCaption As String

On Error Resume Next
DialogCaption = Application.Caption
DialogCaption = DialogCaption & vbNullChar

lHandle = FindWindowA(vbNullString, DialogCaption)

GetSystemMenu lHandle, True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,874
Members
449,056
Latest member
ruhulaminappu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top