Solved

How can I disable the maximize / restore on the window menu

Posted on 2001-09-17
8
322 Views
Last Modified: 2008-02-01
I can disable the close item with following code, but how can I disable the max/restore button on a mdiform???
I can't figure out what to change in this code.


' Bas module
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long) As Long
Public Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function RemoveMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400&
Public Const MF_REMOVE = &H1000&

Private Sub MDIForm_Load()

    Dim hSysMenu As Long, nCnt As Long
    ' Get handle to our form's system menu
    ' (Restore, Maximize, Move, close etc.)
    hSysMenu = GetSystemMenu(Me.hwnd, False)

    If hSysMenu Then
        ' Get System menu's menu count
        nCnt = GetMenuItemCount(hSysMenu)
        If nCnt Then
            ' Menu count is based on 0 (0, 1, 2, 3...)
            RemoveMenu hSysMenu, nCnt - 1, MF_BYPOSITION Or MF_REMOVE
            RemoveMenu hSysMenu, nCnt - 2, MF_BYPOSITION Or MF_REMOVE ' Remove the seperator
            DrawMenuBar Me.hwnd
            ' Force caption bar's refresh. Disabling X button
            Me.Caption = "Try to close me!"
        End If
    End If

End Sub



0
Comment
Question by:Zorxx
  • 4
  • 2
  • 2
8 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 6486940
Zorxx, all three for you:

MinEnabled(frmMDIForm) = False
MaxEnabled(frmMDIForm) = False
And Optionally
CloseEnabled(frmMDIForm) = False

To hide the two (or three) buttons. If you choose to hide all three, you must call the CloseEnabled
method last otherwise it will not work.

Option Explicit

'Window style constants
Private Const WS_MAXIMIZEBOX = &H10000
Private Const WS_MINIMIZEBOX = &H20000
Private Const GWL_STYLE = (-16)
Private Const MF_BYPOSITION = &H400&
Private Const MF_DISABLED = &H2&

'API Declarations
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 DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu 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 member variables
Private lWindowStyle As Long
Private lMenu As Long

Public Property Let MinEnabled(ByVal frm As Object, ByVal New_Value As Boolean)

 If New_Value = True Then
    lWindowStyle = GetWindowLong(frm.hwnd, GWL_STYLE)
    Call SetWindowLong(frm.hwnd, GWL_STYLE, lWindowStyle Or WS_MINIMIZEBOX)
    lMenu = GetSystemMenu(frm.hwnd, True)
    Call DrawMenuBar(frm.hwnd)
 Else
    lWindowStyle = GetWindowLong(frm.hwnd, GWL_STYLE)
    Call SetWindowLong(frm.hwnd, GWL_STYLE, lWindowStyle And Not WS_MINIMIZEBOX)
    lMenu = GetSystemMenu(frm.hwnd, True)
    Call DrawMenuBar(frm.hwnd)
 End If

End Property

Public Property Let MaxEnabled(ByVal frm As Object, ByVal New_Value As Boolean)
 
 If New_Value = True Then
    lWindowStyle = GetWindowLong(frm.hwnd, GWL_STYLE)
    Call SetWindowLong(frm.hwnd, GWL_STYLE, lWindowStyle Or WS_MAXIMIZEBOX)
    lMenu = GetSystemMenu(frm.hwnd, True)
    Call DrawMenuBar(frm.hwnd)
 Else
    lWindowStyle = GetWindowLong(frm.hwnd, GWL_STYLE)
    Call SetWindowLong(frm.hwnd, GWL_STYLE, lWindowStyle And Not WS_MAXIMIZEBOX)
    lMenu = GetSystemMenu(frm.hwnd, True)
    Call DrawMenuBar(frm.hwnd)
 End If
 
End Property

Public Property Let TermEnabled(ByVal frm As Object, ByVal New_Value As Boolean)
Dim lCount As Long
 
  If New_Value = True Then
    lMenu = GetSystemMenu(frm.hwnd, True)
    Call DrawMenuBar(frm.hwnd)
  Else
    lMenu = GetSystemMenu(frm.hwnd, 0)
    lCount = GetMenuItemCount(lMenu)
    Call RemoveMenu(lMenu, lCount - 1, MF_DISABLED Or MF_BYPOSITION)
    Call DrawMenuBar(frm.hwnd)
 End If

End Property
0
 

Author Comment

by:Zorxx
ID: 6492787
But how can i disable the restore button of an MDI child then????. Because when I disable the MAXbutton it will not show up. What I want to do is, when the MDI child form is shown there are three buttons the MIN the Restore and the close. How can I disable the Restore button here???
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6493398
<listening..>
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Zorxx
ID: 6493503
what is that ryancys,    <listening..>

Is this a special sign in the experts list.
0
 
LVL 51

Expert Comment

by:Ryan Chong
ID: 6493544
Sorry Zorxx,

No, Just put a signal here so that i will always get the updates on this question..

And of course learn something from here!
0
 

Author Comment

by:Zorxx
ID: 6493592
I will increase the points now.... to 65

Please help us out.
0
 
LVL 43

Accepted Solution

by:
TimCottee earned 65 total points
ID: 6519263
Zorxx, sorry for taking so long to get back. The code you have should work equally well for MDI Child forms as for the MDIForm itself. To disable the child form's restore button call the appropriate function:

MaxEnabled(MDIChildForm) = False

Though for some reason it still doesn't look as if it is disabled even though it is!
0
 

Author Comment

by:Zorxx
ID: 6522260
Thanks TimCottee, you are absolutely right...
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question