|
-
Feb 24th, 2007, 08:37 PM
#1
Re: Disable Close Button and Prevent Form Being Moved
 Originally Posted by shakti5385
The drawback with that is that you must inherit your form from that class. My class can be added to any form you like, which makes it a little simpler to use. I'd guess that that author has used the same method of disabling the Move menu item though.
-
Nov 7th, 2007, 07:23 PM
#2
Addicted Member
Re: Disable Close Button and Prevent Form Being Moved
Hey,
I thought I would post this and see if it helps...a simple way to disable the close button in vb 2005
Code Code:
Private Const SC_CLOSE As Integer = &HF060
Private Const MF_ENABLED As Integer = &H0
Private Const MF_GRAYED As Integer = &H1
Private Const MF_DISABLED As Integer = &H2
Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As IntPtr, ByVal item As Integer, ByVal enab As Integer) As Boolean
Public Sub EnableExit(ByVal ok As Boolean)
Try
Dim hdl As IntPtr = GetSystemMenu(Me.Handle, False)
Dim arg As Integer = MF_ENABLED
If Not ok Then arg = MF_DISABLED + MF_GRAYED
EnableMenuItem(hdl, SC_CLOSE, arg)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'call EnableExit(False) in your form load event to disable the clode button
Just thought it might be worth posting this

I will wait for death with a smile and a big stick
-
Nov 7th, 2007, 07:26 PM
#3
Re: Disable Close Button and Prevent Form Being Moved
 Originally Posted by rabid lemming
Hey,
I thought I would post this and see if it helps...a simple way to disable the close button in vb 2005
Code Code:
Private Const SC_CLOSE As Integer = &HF060
Private Const MF_ENABLED As Integer = &H0
Private Const MF_GRAYED As Integer = &H1
Private Const MF_DISABLED As Integer = &H2
Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal revert As Boolean) As IntPtr
Declare Function EnableMenuItem Lib "user32" (ByVal hMenu As IntPtr, ByVal item As Integer, ByVal enab As Integer) As Boolean
Public Sub EnableExit(ByVal ok As Boolean)
Try
Dim hdl As IntPtr = GetSystemMenu(Me.Handle, False)
Dim arg As Integer = MF_ENABLED
If Not ok Then arg = MF_DISABLED + MF_GRAYED
EnableMenuItem(hdl, SC_CLOSE, arg)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
'call EnableExit(False) in your form load event to disable the clode button
Just thought it might be worth posting this 
Have you read my code? That's exactly how it works, but I wrapped it in a class that you can reuse instead of having to add that code to every form.
-
Nov 8th, 2007, 01:16 AM
#4
Re: Disable Close Button and Prevent Form Being Moved
I had run into that problem before where the button becomes reenabled after resizing the form. I no longer have that problem.
I think I fixed it by declaring the API PROPERLY!
Yes it's wrong that way, and many API will fail sometimes without an Alias.
For example the proper way:
Code:
Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
Where the alias name should be different than the function name.
I'll see if I can indeed verify that as the reason, unless the IntPtr may also cause a problem.
Last edited by TTn; Nov 8th, 2007 at 02:37 AM.
-
Nov 8th, 2007, 02:37 AM
#5
Re: Disable Close Button and Prevent Form Being Moved
I wasn't able to reproduce the quirk, but my comment still stands for API declares.
-
Nov 8th, 2007, 08:43 AM
#6
Addicted Member
-
Nov 22nd, 2007, 01:08 PM
#7
Re: Disable Close Button and Prevent Form Being Moved
 Originally Posted by TTn
I had run into that problem before where the button becomes reenabled after resizing the form. I no longer have that problem.
I think I fixed it by declaring the API PROPERLY!
Yes it's wrong that way, and many API will fail sometimes without an Alias.
For example the proper way:
Code:
Declare Function apiEnableMenuItem Lib "user32" Alias "EnableMenuItem" (ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
Where the alias name should be different than the function name.
I'll see if I can indeed verify that as the reason, unless the IntPtr may also cause a problem.
No, that incorrect as in .NET its most proper to use the dllimport attribute as alias' are not used or allowed as EntryPoint is used.
Code:
<System.Runtime.InteropServices.DllImport("User32.dll", CharSet:=Runtime.InteropServices.CharSet.Auto, EntryPoint:="EnableMenuItem")> _
Private Shared Function EnableMenuItem(ByVal hMenu As IntPtr, ByVal item As Int32, ByVal enab As Int32) As Boolean
End Function
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
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
|