Solved

close button?

Posted on 2002-03-14
8
183 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 28

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

685 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