Visual Basic/VB >> Form "Close Button" - top right "X" Continue

by Jim Y » Thu, 09 Oct 2003 06:58:47 GMT

Is it possible to *remove* or otherwise hide the "X" from the blue border?
Jim Y

Jim Y < XXXX@XXXXX.COM > wrote in message
news:sLLgb.168451$ XXXX@XXXXX.COM ...
> Is it possible to disable that method of closing a form and still have a blue border at the top? Example?
>
> Jim Y
>
>




Visual Basic/VB >> Form "Close Button" - top right "X" Continue

by dnagel » Thu, 09 Oct 2003 07:09:20 GMT


set the forms "Control Box" property to false...

D.










Visual Basic/VB >> Form "Close Button" - top right "X" Continue

by MikeD » Thu, 09 Oct 2003 08:28:10 GMT






blue border at the top? Example?


As stated, you can set the ControlBox property to False. As also stated,
this also removes the icon from the title bar. If that's not acceptable,
you can disable the Close button, but you can't otherwise remove it....at
least, not to my knowledge. Here's how to disable it.

-----BEGIN CODE
Option Explicit

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long,
ByVal bRevert As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd 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_BYCOMMAND As Long = &H0
Private Const SC_CLOSE As Long = &HF060

Private Sub Form_Click()

Dim hSysMenu As Long
Static bDisabled As Boolean

If bDisabled Then
'Restores the system menu to its default state.
Call GetSystemMenu(Me.hwnd, 1)
Else
'Get a handle for the system menu
hSysMenu = GetSystemMenu(Me.hwnd, 0)
Call RemoveMenu(hSysMenu, SC_CLOSE, MF_BYCOMMAND)
End If

bDisabled = Not bDisabled

'Redraw the menu to reflect the change
Call DrawMenuBar(Me.hwnd)

End Sub
-----END CODE

Just click the form to toggle the Close button's state.

Mike




Form "Close Button" - top right "X" Continue

by MikeD » Thu, 09 Oct 2003 08:41:28 GMT






border?


a

I should probably mention that I have no idea if that code "works" under
Windows XP. I can easily see that it might not. One of these days, I might
actually install WinXP so I can test these things for myself. <g>


Mike





Form "Close Button" - top right "X" Continue

by Randy Birch » Thu, 09 Oct 2003 08:54:01 GMT

Windows is windows ... it works on XP.

An improvement would be to also remove the separator that is left at the
bottom of the sysmenu:

Option Explicit

Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long,
ByVal bRevert As Long) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd 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 Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As Long)
As Long

Private Const MF_BYCOMMAND As Long = &H0
Private Const SC_CLOSE As Long = &HF060
Private Const MF_BYPOSITION As Long = &H400


Private Sub Form_Click()

Dim hSysMenu As Long
Static bDisabled As Boolean

If bDisabled Then
'Restores the system menu to its default state.
Call GetSystemMenu(Me.hwnd, 1)
Else
'Get a handle for the system menu
hSysMenu = GetSystemMenu(Me.hwnd, 0)
Call RemoveMenu(hSysMenu, SC_CLOSE, MF_BYCOMMAND)
Call RemoveMenu(hSysMenu, GetMenuItemCount(hSysMenu) - 1,
MF_BYPOSITION)

End If

bDisabled = Not bDisabled

'Redraw the menu to reflect the change
Call DrawMenuBar(Me.hwnd)

End Sub


--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.




:


: >


: > > Is it possible to *remove* or otherwise hide the "X" from the blue
: border?
: > > Jim Y
: > >


: > > > Is it possible to disable that method of closing a form and still
have
: a
: > blue border at the top? Example?
: > > >
: >
: >
: > As stated, you can set the ControlBox property to False. As also
stated,
: > this also removes the icon from the title bar. If that's not
acceptable,
: > you can disable the Close button, but you can't otherwise remove
it....at
: > least, not to my knowledge. Here's how to disable it.
:
: I should probably mention that I have no idea if that code "works" under
: Windows XP. I can easily see that it might not. One of these days, I
might
: actually install WinXP so I can test these things for myself. <g>
:
:
: Mike
:
:
:




Form "Close Button" - top right "X" Continue

by MikeD » Thu, 09 Oct 2003 10:35:34 GMT






Having no practicle experience with WinXP, I wasn't sure if manifests might
affect this somehow. On occasion (albeit relatively rare), sometimes things
ARE different from one version of Windows to the next.


True. I forgot about that remaining in the system menu, although from a
functional standpoint it's irrelevant.

Mike




Form "Close Button" - top right "X" Continue

by Jim Y » Thu, 09 Oct 2003 10:53:59 GMT

I know this works, but thought I would ask about getting rid of the "X' if possible. I want the icon to remain at the
left and will go with the following from Randy:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Cancel = (UnloadMode = vbFormControlMenu)

End Sub

Correct me if I am wrong, but a friend of mine uses that "X" to terminate his programs. If I remember, that method is a
"No-No". My other option is to place "Unload form" statements in the Query_Unload event. Any suggestions on the better
way to go?

Thank you,
Jim Y










Form "Close Button" - top right "X" Continue

by MikeD » Thu, 09 Oct 2003 11:23:48 GMT





his programs. If I remember, that method is a
Query_Unload event. Any suggestions on the better

Why would that be a no-no? The Close button in the title bar is just a way
to close that particular form...nothing more. It doesn't necessarily mean
the entire app should close. However, if by that form closing the app
*should* close, then that's exactly what should happen. I close apps all
the time by clicking the title bar's Close button, and I'd imagine that's
how most people close apps. It's a lot easier than selecting Exit (or Close)
from the File menu. Try getting a copy of Win3.x and play with that for a
while. It'll take you about 10 minutes to appreciate the Close button in
the title bar. <g>

Mike





Form "Close Button" - top right "X" Continue

by Larry Serflaten » Thu, 09 Oct 2003 11:50:02 GMT

"Jim Y" < XXXX@XXXXX.COM > wrote

I agree with Mike, which also agrees with normal Windows responses.
Open Notepad, or the calculator, or other such one window programs
and you'll see that is what is supposed to happen.


Your form is already in the process of unloading at that point. Even if
you have multiple forms in your app, the place to unload them is in the
Unload event. Use QueryUnload to prompt the user if there is unsaved
data that would be lost if they closed at that time. If they cancel, you
can use that event's Cancel parameter to stop the unloading process.

LFS





Form "Close Button" - top right "X" Continue

by Randy Birch » Thu, 09 Oct 2003 12:08:10 GMT

The X is nothing more than a means to initiate the shutdown of a particular
form. It is no different than using the sysmenu Close command, or invoking
an Unload Me from a menu or command button.

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.




: I know this works, but thought I would ask about getting rid of the "X' if
possible. I want the icon to remain at the
: left and will go with the following from Randy:
:
: Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
:
: Cancel = (UnloadMode = vbFormControlMenu)
:
: End Sub
:
: Correct me if I am wrong, but a friend of mine uses that "X" to terminate
his programs. If I remember, that method is a
: "No-No". My other option is to place "Unload form" statements in the
Query_Unload event. Any suggestions on the better
: way to go?
:
: Thank you,
: Jim Y
:
:


: > Is it possible to *remove* or otherwise hide the "X" from the blue
border?
: > Jim Y
: >


: > > Is it possible to disable that method of closing a form and still have
a blue border at the top? Example?
: > >
: > > Jim Y
: > >
: > >
: >
: >
:
:




Form "Close Button" - top right "X" Continue

by Lofty » Thu, 09 Oct 2003 17:16:58 GMT

As I mentioned above, set the form's ControlBox property to False!

--
Lofty
http://www.brainache.demon.co.uk






blue border at the top? Example?




Form "Close Button" - top right "X" Continue

by Lofty » Thu, 09 Oct 2003 17:34:00 GMT

Oops! I really should check for further responses to questions before I
pipe up with bone suggestions!!

Sorry!

--
Lofty
http://www.brainache.demon.co.uk






border?


a




Form "Close Button" - top right "X" Continue

by Jim Y » Thu, 09 Oct 2003 23:07:28 GMT

> The Close button in the title bar is just a way

That is what I remember - it closes the form and does NOT terminate the program. That is the reason for my initial
post.

In a large program with, say 30 forms, I must include the Query_Unload routine on each form to unload 30 forms when the
"X" is activated. I guess the way to go is to add code to each form something like:

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)

Call Terminate

End Sub

where Terminate lists all of the forms to unload. In my case, I have Terminate in a module and just have to add the sub
to each form.

Today, in one of my texts, I found a topic titled, "Applications that Look Like They Are Over - but Aren't". That topic
describes using Form_QueryUnload routine which lists the forms to unload.

Thank you for your comments,
Jim Y









Similar Threads

1. Form "Close Button" - top right "X" - Visual Basic/VB

2. Closing app in IDE from close (X box) at top right hand corner

Hi All,

I have had this problem a number of times.  I have an app that is working 
just fine, however the only issue is that I cannot close the app by clicking 
the standard windows X (To right hand corner) if running within the IDE.  I 
have tried to trap the forms unload, deactivate events but no, nothing.  I 
compile the app and all works fine.  You may say thats all that is needed, 
however, I am intreaged as to why I can't close within the IDE.  I have to 
use a menu or command button!

Help!!!!!!!

Regards

matt A

3. Dialog Box is open, close and continue - forms with outlook qu

4. Dialog Box is open, close and continue - forms with outlook questi

5. contols top(left & right) edges rounded (imitating a form)

6. Form: disable Close button and remove Close menu and then reactivate

In Vs2005 i need to disable the Close button and remove Close Menu and then 
reactivate them.

I have found how to disable Close button and remove close menu, but i 
haven't found how to reactivate them.

Does someone knows how to reactivate them?

This is how i disable close button and remove close menu:

<DllImport("user32.dll")> Private Shared Function GetSystemMenu(ByVal hWnd 
As IntPtr, ByVal wMsg As Boolean) As IntPtr
End Function

<DllImport("user32.dll")> Private Shared Function RemoveMenu(ByVal hWnd As 
IntPtr, ByVal uPosition As Int32, ByVal uFlags As Int32) As Boolean
End Function

<DllImport("user32.dll")> Private Shared Function SendMessage(ByVal hWnd As 
IntPtr, ByVal wMsg As Int32, ByVal wParam As Int32, ByVal lParam As Int32) 
As Int32
End Function

Private Const SC_CLOSE As Int32 = &HF060
Private Const MF_BYCOMMAND As Int32 = 0
Private Const WM_NCACTIVATE As Int32 = &H86

Private Sub DeactivateCloseButtonAndMenu()
   Dim hMenu As IntPtr
   hMenu = GetSystemMenu(Me.Handle, False)
   RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND)
   SendMessage(Me.Handle, WM_NCACTIVATE, 0, 0)
   SendMessage(Me.Handle, WM_NCACTIVATE, 1, 0)
End Sub

Thanks. 


7. Form closing after button click

8. Close a form by pressing esc, how to do it without having a button

Hello there,

I was wondering if anyone knew how to close a form when the user presses the 
esc key. I dont have any buttons on the form so I cannot use the 
cancelButton property of the form.

I have tried using the forms keypressed event but it doesnt seem to be 
firing. Does anyone have any suggestins?

Thank you
John Sheppard