Solved

close button?

Posted on 2002-03-14
8
181 Views
Last Modified: 2012-05-04
in a form can we disable or make the close button disable?
0
Comment
Question by:pranay01
8 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 6866893
Look at the form's controlbox property.

Also, the form's borderstyle property can affect the presence of a close box, since control boxes don't exist on dialog forms.
0
 
LVL 27

Expert Comment

by:Ark
ID: 6866989
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Private Const MF_BYPOSITION = &H400&

Private Sub RemoveMenus(frm As Form, _
    remove_restore As Boolean, _
    remove_move As Boolean, _
    remove_size As Boolean, _
    remove_minimize As Boolean, _
    remove_maximize As Boolean, _
    remove_separator As Boolean, _
    remove_close As Boolean)
Dim hMenu As Long
   
    ' Get the form's system menu handle.
    hMenu = GetSystemMenu(hwnd, False)
   
    If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
    If remove_separator Then DeleteMenu hMenu, 5, MF_BYPOSITION
    If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
    If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
    If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
    If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
    If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub Form_Load()
    ' Remove the Close system menu item and the
    ' menu separator.
    RemoveMenus Me, False, False, _
        False, False, False, True, True
End Sub

Cheers
0
 
LVL 4

Accepted Solution

by:
trkcorp earned 50 total points
ID: 6867043
aikimark is correct but,
What type of form is it?  On a "normal" window the control box property = false can remove it but it also removes the icon (system menu) and the Min/Max buttons.  
MDI Forms don't have these properties but on a MDI form GetWindowLong & SetWindowLong APIs can accomplish the same thing. But, I do not believe you can get rid of the close and maintain the min/max buttons nor can you just disable the close button.  (Someone prove me wrong, please!)
If you want MDI form code to do what I described, tell me and I will post it for you.
0
ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 4

Expert Comment

by:trkcorp
ID: 6867061
Ark, you proved me wrong and I thank you!
0
 
LVL 18

Expert Comment

by:Crash2100
ID: 6867098
a simple thing you could do is put the following line of code in the form's unload event.  And this will make it so whenever you click the close button it won't do anything.

Private Sub Form_Unload(Cancel As Integer)
    Cancel = -1
End Sub
0
 

Author Comment

by:pranay01
ID: 6867240
thank u trkcorp!
plz post the codes for an MDI form for me !!
0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6867325
Ark's Code would work for you too, and maybe you can mix & match but here you go:

'This will set the form to fixed, remove the min/max buttons, tell the user not to try and reset the
size and prevent him from doing so.

Option Explicit
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, ByVal nIndex As Long) As Long

Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000  
Const WS_MAXIMIZEBOX = &H10000
Const WS_SYSMENU = &H80000

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long

CurStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
'you can remove any of these to affect the form as you wish.
CurStyle = CurStyle And Not (WS_MINIMIZEBOX) 'max box
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX) 'min box
CurStyle = CurStyle And Not (WS_THICKFRAME) 'set fixed size
CurStyle = CurStyle And Not (WS_SYSMENU) 'like control box = false on "normal" form

NewStyle = SetWindowLong(Me.hwnd, GWL_STYLE, CurStyle)
End Sub

'add this if you want, it kind of depends on whether you
'leave the system menu intact (see Ark's stuff) whether you
'would want this or whether you set a fixed border.
Private Sub MDIForm_Resize()
Static bMsgd As Boolean
Me.WindowState = vbNormal
If Not bMsgd Then
MsgBox "You are not allowed to resize this window!"
bMsgd = True
Else
bMsgd = False
End If
End Sub




0
 
LVL 4

Expert Comment

by:trkcorp
ID: 6867535
I like the way this works with Ark's code used too.  Try this:

Option Explicit
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 GetWindowLong Lib "user32" Alias "GetWindowLongA" _
  (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, _
  ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long

Const MF_BYPOSITION = &H400&

Const GWL_STYLE = (-16)
Const WS_THICKFRAME = &H40000
Const WS_MINIMIZEBOX = &H20000
Const WS_MAXIMIZEBOX = &H10000
Const WS_SYSMENU = &H80000

Private Sub RemoveMenus(frm As Form, _
   remove_restore As Boolean, _
   remove_move As Boolean, _
   remove_size As Boolean, _
   remove_minimize As Boolean, _
   remove_maximize As Boolean, _
   remove_separator As Boolean, _
   remove_close As Boolean)
Dim hMenu As Long
   
' Get the form's system menu handle.
hMenu = GetSystemMenu(hwnd, False)
'Do NOT rearrange this code as it operates by relative position!
If remove_close Then DeleteMenu hMenu, 6, MF_BYPOSITION
If remove_separator Then DeleteMenu hMenu, 5, MF_BYPOSITION
If remove_maximize Then DeleteMenu hMenu, 4, MF_BYPOSITION
If remove_minimize Then DeleteMenu hMenu, 3, MF_BYPOSITION
If remove_size Then DeleteMenu hMenu, 2, MF_BYPOSITION
If remove_move Then DeleteMenu hMenu, 1, MF_BYPOSITION
If remove_restore Then DeleteMenu hMenu, 0, MF_BYPOSITION
End Sub

Private Sub MDIForm_Load()
Dim CurStyle As Long
Dim NewStyle As Long

CurStyle = GetWindowLong(Me.hwnd, GWL_STYLE)
CurStyle = CurStyle And Not (WS_MINIMIZEBOX)
CurStyle = CurStyle And Not (WS_MAXIMIZEBOX)
'CurStyle = CurStyle And Not (WS_THICKFRAME)
'CurStyle = CurStyle And Not (WS_SYSMENU)

NewStyle = SetWindowLong(Me.hwnd, GWL_STYLE, CurStyle)

RemoveMenus Me, True, False, _
    False, True, True, True, True

Me.Show

End Sub
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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 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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

770 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