|
-
May 13th, 2002, 05:27 AM
#1
Thread Starter
Junior Member
Disabling close [X] button in VB.Net
See subject! Tried the VB6 implementation that I found on the site but get the error "Hwnd is not a member of 'My_Prog_name.MainForm'" when trying to compile for debugging. Any ideas anyone??!?
I'm assuming there's probably a very easy way to do this in VB.net instead of grabbing the forms handle.
-
May 13th, 2002, 08:40 AM
#2
If I remember correctly, the VB6 property Hwnd is called Handle in VB.NET.
-
May 13th, 2002, 08:58 AM
#3
Thread Starter
Junior Member
Thanks Frans, but I did try that and got a "Value of Type 'System.InPtr' (referring to 'Me.Handle' of course) cannot be converted to Long. This makes sense when you look at the code:
'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
Look at the declarations section. We are calling "user32.dll" to handle the window. I'm sure the .Net framework probably has a more efficient way of doing this.
This is where I'm stuck. I think I'm nearly there but need re-assurance that I'm going the right way about this.
-
May 13th, 2002, 09:10 AM
#4
API usage is different between VB6 and .NET you cant just take Vb6 api code and paste into .NET. paremeter types have to be change Long to Integer, Integer to Short, cant use Any, etc. Plus you have to use COM interop. But instead of doing all that, ....
Set the property Form.ControlBox to false.
-
May 13th, 2002, 09:18 AM
#5
Thread Starter
Junior Member
Getting there. I do understand that you can't just paste VB6 code into VB.net and "it'll work". I used the VB6 code as a means of getting an idea of how it used to be done. As it turns out I don't think any of the code will be of any use to me.
Thanks for the advice Cander but what I need is a little more sophisticated than just dis-abling the controlbox!!
The program still needs to minimize and maximize from the toolbar but it can't close as there will probably be open files if the main process isn't stopped in the correct manner. This is the main reason that I need to handle the close box directly. The other reason is so that I can deploy it to the system tray in later releases when the close button is clicked.
Can anybody provide me with a decent answer??!? There must be some-one with the knowledge!!
-
May 13th, 2002, 10:58 AM
#6
OK, I'm sure there must be a .NET way to do it, but here is your code converted as it was.
In a module:
VB Code:
Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As IntPtr, ByVal bRevert As Integer) As IntPtr
Declare Function GetMenuItemCount Lib "user32" _
(ByVal hMenu As IntPtr) As Integer
Declare Function DrawMenuBar Lib "user32" _
(ByVal hwnd As IntPtr) As Integer
Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As IntPtr, ByVal nPosition As Integer, _
ByVal wFlags As Integer) As Integer
Public Const MF_BYPOSITION As Integer = &H400
Public Const MF_REMOVE As Integer = &H1000
In your form (I used form2):
VB Code:
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim hSysMenu As IntPtr
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.Handle, 0)
If Not hSysMenu.Equals(0) 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.Handle)
'Force caption bar's refresh. Disabling X button
Me.Text = "Try to close me!"
End If
End If
End Sub
-
May 13th, 2002, 12:37 PM
#7
Junior Member
Re: Disabling close [X] button in VB.Net
Originally posted by DWetherilt
The program still needs to minimize and maximize from the toolbar but it can't close as there will probably be open files if the main process isn't stopped in the correct manner. This is the main reason that I need to handle the close box directly.
I'm still a little new at this , so I apologize in advance if I am misunderstanding your situation, but I know that if the user closes a form using the X button, you can catch the event, and even cancel it, if you want. So another way to solve your problem may be to catch that event and write code that makes sure that the process is stopped correctly before closing the program.
I saw some sample code somewhere (I can try to find it again, if you would like) that showed how to catch a close event and determine where it came from (closing the form by hitting the close button, closing the parent form, using the form menu, etc.). The point of the sample code that I saw was to handle the scenario when the user clicked the close [X] button. If anyone wants to see the code, let me know I'll try to find it and post it here.
LDD
----------------
"Character is what you do in the dark." Dwight L. Moody
-
May 13th, 2002, 01:18 PM
#8
Frenzied Member
you might find this helpful
http://www.vbforums.com/showthread.p...hreadid=159297
that link is to a thread where i talk to myself about adding a menu item to a forms system menu not it's menu bar it's system menu
after rereading that damn i sound like a jerk
Magiaus
If I helped give me some points.
-
May 14th, 2002, 03:35 AM
#9
Thread Starter
Junior Member
Frans is the man!!
Frans. Thanks for doing that conversion - it seems blatently obvious now!!! It worked a treat!! Hope I can help you one day.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|