Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Posted

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!!!

Posted
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?
Posted

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 :)

  • *Experts*
Posted

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

"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
  • 2 months later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...