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

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



ZorxxAsked:
Who is Participating?
 
TimCotteeConnect With a Mentor Head of Software ServicesCommented:
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
 
TimCotteeHead of Software ServicesCommented:
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
 
ZorxxAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Ryan ChongCommented:
<listening..>
0
 
ZorxxAuthor Commented:
what is that ryancys,    <listening..>

Is this a special sign in the experts list.
0
 
Ryan ChongCommented:
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
 
ZorxxAuthor Commented:
I will increase the points now.... to 65

Please help us out.
0
 
ZorxxAuthor Commented:
Thanks TimCottee, you are absolutely right...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.