Solved

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

Posted on 2001-09-17
8
315 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 50

Expert Comment

by:Ryan Chong
ID: 6493398
<listening..>
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

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

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Validating VB6 Function 19 59
VBA filters 2 58
MS SQL store procedure to calculate and return result 6 56
Need Nag Screen for Reboot if system up time is over 14 days 12 44
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

815 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now