micropathic Posted January 26, 2004 Posted January 26, 2004 This code is supposed to disable the "X" (close) on a windows form: Private Declare Function RemoveMenu Lib "user32" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Long) As IntPtr Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr Private Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu As IntPtr) As Integer Private Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As IntPtr) As Boolean ' Private Const MF_BYPOSITION = &H400 Private Const MF_REMOVE = &H1000 Private Const MF_DISABLED = &H2 ' Public Sub DisableCloseButton(ByVal hwnd As IntPtr) Dim hMenu As IntPtr Dim menuItemCount As Integer ' 'Obtain the handle to the form's system menu hMenu = GetSystemMenu(hwnd, False) ' 'Obtain the number of items in the menu menuItemCount = GetMenuItemCount(hMenu) ' 'Remove the system menu Close menu item. 'The menu item is 0-based, so the last 'item on the menu is menuItemCount - 1 Call RemoveMenu(hMenu, menuItemCount - 1, _ MF_DISABLED Or MF_BYPOSITION) ' 'Remove the system menu separator line Call RemoveMenu(hMenu, menuItemCount - 2, _ MF_DISABLED Or MF_BYPOSITION) ' 'Force a redraw of the menu. This 'refreshes the titlebar, dimming the X Call DrawMenuBar(hwnd) End Sub ' ' '------------------- USAGE ------------------- 'Put this in the Load Event of a Form ' DisableCloseButton(Me.Handle) This code doesn't seem to work for me though because it says that a declaration was expected when I put my cursor over the DisableCloseButton(Me.Handle) which by the way is underlined with the blue "squigly" underline that vb.net uses to show something is wrong. If someone could let me know what the flaw could be I would be greatly appreciative. Thanks Quote
Administrators PlausiblyDamp Posted January 26, 2004 Administrators Posted January 26, 2004 '------------------- USAGE ------------------- 'Put this in the Load Event of a Form ' DisableCloseButton(Me.Handle) you did put it in the Form's load event didn't you? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
micropathic Posted January 27, 2004 Author Posted January 27, 2004 Actually I didn't. Why would that make a difference? Thanks, micropathic Quote
samsmithnz Posted January 27, 2004 Posted January 27, 2004 Well apart from the fact that it says: '------------------- USAGE ------------------- 'Put this in the Load Event of a Form the code doesn't run unless you run it. Putting it in the Form_load event causes it to run when the form loads, and hence your button disappears!!! Quote Thanks Sam http://www.samsmith.co.nz
micropathic Posted January 27, 2004 Author Posted January 27, 2004 Yeah, I noticed the comment, but I just don't understand why it doesn't work as any other function does. Is it because this is not something that can be changed on the fly or is it that this code is configured to only work in that manner? Quote
NK2000 Posted January 27, 2004 Posted January 27, 2004 maybe because the win32 calls require it, but before you ask a lot just test it and if it works then dont touch it any longer :) (it could get broken again :D ) 2 real answers: 1) you maybe can put it anywhere but then you dont get the desired effect 2) you must call this win32 functions before the form is visible i am open for new and/or better answers :) Quote
Administrators PlausiblyDamp Posted January 27, 2004 Administrators Posted January 27, 2004 It's a function call - it has to be called as part of another procedure, just leaving it in the declarations section breaks the syntax of the language. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
*Experts* Nerseus Posted January 27, 2004 *Experts* Posted January 27, 2004 Simple question - do you need the Mininmize and Maximize buttons? If not, you can disable all 3 (including the "X") by setting the corresponding properties to false. -nerseus Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
NK2000 Posted January 27, 2004 Posted January 27, 2004 hm i write it out of my mind but as i looked in vs you can just check the Form.ControlBox to false and i think that is the desired effect :) Quote
hungheykwun Posted March 31, 2004 Posted March 31, 2004 Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing e.Cancel = True End Sub Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.