Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabeling the "x"(Close window) button 1

Status
Not open for further replies.

uprocker2

Programmer
Dec 26, 2004
34
0
0
BE
Is it possible to disable the close window(
close.gif
) button
whitout removing it only make it unuseble in a visual basic app.

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
Try this:

Private Declare Function GetSystemMenu Lib "User32" (ByVal hWnd As Long, ByVal bRevert 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 Const MF_BYPOSITION = &H400


Private Sub Form_Load()
RemoveMenu GetSystemMenu(Me.hWnd, 0), 6, MF_BYPOSITION
End Sub

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
Kinda hokey but you can
Code:
Option Explicit

Dim AuthorizedExit As Boolean

Private Sub Command1_Click()
    AuthorizedExit = True
    Unload Me
End Sub

Private Sub Form_Load()
    AuthorizedExit = False
End Sub

Private Sub Form_Unload(Cancel As Integer)
    If Not AuthorizedExit Then Cancel = True
End Sub
Where the command button "Command1" unloads the form but the "X" button does nothing.
 
Golom, the problem I see with that is the X button seems to work, you can press it but nothing happens, it gives the impression that something is wrong. I believe it is better to either disable it or remove it all together.

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
>>>>>DrJabaJoe thx it works but can u explane the code please so i can learn what the code realy does?

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
btw is there aslo a possibility makin the window only minimazible

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
Code:
Disable the X (close button) on the title bar

This tip shows you how to disable the X on the title bar of you forms. It does this by removing the close item in the form's system menu. This works fine, until you want to re-enable the X.

Declarations

Paste this into the declaration section:

Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long

Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As Long) As Long

Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As Long) As Long

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&
Form Code

Then paste this into the form:

Private Sub Form_Load()
Dim hSysMenu As Long
Dim nCnt As Long

'First, show the form
Me.Show

'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
 
DrJavaJoe

You're right. My normal take on this is to set "ControlBox" to False and make the whole thing go away since, on principle, I don't want controls visible on the form if they don't do nothing . His question however said that he didn't want to remove it.
 
Code:
Private Declare Function [COLOR=blue]GetSystemMenu Lib "User32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long[/color]
Private Declare Function [COLOR=green]RemoveMenu Lib "User32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long[/color]
Private Const MF_BYPOSITION = &H400
    
Private Sub Form_Load()
    [COLOR=green]RemoveMenu [COLOR=blue]GetSystemMenu(Me.hWnd, 0)[/color], 6, MF_BYPOSITION[/color]
End Sub

Same As...
Code:
    hMenu = [COLOR=blue]GetSystemMenu(Me.hWnd, 0)[/color]
    [COLOR=green]RemoveMenu hMenu, 6, MF_BYPOSITION[/color]

Me.hWnd is the Window Handle of the Calling Form (Me)

hMenu is the hWnd of the MenuObject

GetSystemMenu, gets the System Menu Object from the Calling Form (Me)
GetSystemMenu(hWnd, bRevert)
MSDN said:
If the bRevert parameter is FALSE, the return value is a handle to a copy of the window menu

RemoveMenu, deletes a menu item or detaches a submenu from the specified menu.
RemoveMenu(hMenu, nPosition, wFlags)
MSDN said:
Parameters

hMenu
Handle to the menu to be changed.
uPosition
Specifies the menu item to be deleted, as determined by the uFlags parameter.
uFlags
Specifies how the uPosition parameter is interpreted. This parameter must be one of the following values.
MF_BYCOMMAND
Indicates that uPosition gives the identifier of the menu item. If neither the MF_BYCOMMAND nor MF_BYPOSITION flag is specified, the MF_BYCOMMAND flag is the default flag.
MF_BYPOSITION
Indicates that uPosition gives the zero-based relative position of the menu item.

Here are the API docs:
GetSystemMenu

RemoveMenu


Have Fun, Be Young... Code BASIC
-Josh

cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
GetSystemMenu returns the handle to the form's system menu or the menu you get when you right click the forms icon in the upper left hand corner. The RemoveMenu function actually removes the menu specified in this case 6 or Close from that menu, thus removing the menu item that the
close button is associated to. You may also want to remove the seperator that was above the close menu item by calling the RemoveMenu function again like this:

RemoveMenu GetSystemMenu(Me.hWnd, 0), 5, MF_BYPOSITION

as far as removing the maximize button just set the forms MaxButton property to false.

Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'.
 
alehawk, as a matter of courtesy, when you copy and paste from other sites could you please credit them?
 
So if I put one of these controls in a activex it wil work just fine>???!??!!?!?

sign07.GIF
sign07.GIF
sign07.GIF
Uprocker2
sign06.gif
sign06.gif
sign06.gif
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top