• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

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



0
Zorxx
Asked:
Zorxx
  • 4
  • 2
  • 2
1 Solution
 
TimCotteeCommented:
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
 
Ryan ChongCommented:
<listening..>
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
TimCotteeCommented:
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
 
ZorxxAuthor Commented:
Thanks TimCottee, you are absolutely right...
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now