Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes

Visual Basic Programming Code Examples

Visual Basic > Windows and Controls Code Examples

Disable a Windows Close Button

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Disable a Windows Close Button This sample will show you how to disable the little 'X' close button found on the upper right of every window. To do this what we will do is use a couple of API calls to remove the Close menu item from the windows system menu. Doing this will disable the close button. When you try out this sample, you may want to have an alternate way to close your window. Insert this code into a .bas module Option Explicit Public Declare Function GetSystemMenu Lib "user32" _ (ByVal hwnd As Long, _ ByVal bRevert As Long) As Long Public 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 Sub DisableCloseWindowButton(frm As Form) Dim hSysMenu As Long 'Get the handle to this windows 'system menu hSysMenu = GetSystemMenu(frm.hwnd, 0) 'Remove the Close menu item 'This will also disable the close button RemoveMenu hSysMenu, 6, MF_BYPOSITION 'Lastly, we remove the seperator bar RemoveMenu hSysMenu, 5, MF_BYPOSITION End Sub '--end code block Now call the DisableCloseWindowButton from your forms load event. Private Sub Form_Load() DisableCloseWindowButton Me End Sub '--end code block