Visual Basic Code Snippets #4
In this next update I’ll continue to re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to Forms.
Save and Load the Position of the Form
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long
Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Public Function GetFromINI(Section As String, Key As String, Directory As String) As String
Dim strBuffer As String
strBuffer = String(750, Chr(0))
Key$ = LCase$(Key$)
GetFromINI$ = Left(strBuffer, GetPrivateProfileString(Section$, ByVal Key$, "", strBuffer, Len(strBuffer), Directory$))
End Function
Public Sub WriteToINI(Section As String, Key As String, KeyValue As String, Directory As String)
Call WritePrivateProfileString(Section$, UCase$(Key$), KeyValue$, Directory$)
End Sub
Add the following code in the Form_Unload event...
Call WriteToINI("Main", "Xpos", Form1.Left, App.Path & "\data.ini")
Call WriteToINI("Main", "Ypos", Form1.Top, App.Path & "\data.ini")
Add the following code in the Form_Load event...
Xpos = GetFromINI("Main", "Xpos", App.Path & "\data.ini")
Ypos = GetFromINI("Main", "Ypos", App.Path & "\data.ini")
Form1.Top = Ypos
Form1.Left = Xpos
Disable the X Button on a Form
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function GetMenuItemCount Lib "user32" (ByVal hMenu 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 Declare Function DrawMenuBar Lib "user32" (ByVal hwnd As Long) As Long
Public Const MF_BYPOSITION = &H400&
Public Const MF_DISABLED = &H2&
Public Sub DisableX(Frm As Form)
Dim hMenu As Long
Dim nCount As Long
hMenu = GetSystemMenu(Frm.hwnd, 0)
nCount = GetMenuItemCount(hMenu)
Call RemoveMenu(hMenu, nCount - 1, MF_DISABLED Or MF_BYPOSITION)
DrawMenuBar Frm.hwnd
End Sub
Add the following code wherever you want the code to execute...
Call DisableX(Me)
Make Form Stay On-Top
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Const HWND_NOTOPMOST = -2
Public Const HWND_TOPMOST = -1
Public Sub StayOnTop(frm As Form)
'For best results put in the formpaint
Call SetWindowPos(frm.hWnd, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
End Sub
Public Sub NotOnTop(frm As Form)
'For best results put in the formpaint
Call SetWindowPos(frm.hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS)
End Sub
Add the following code wherever you want the StayOnTop code to execute...
Call StayOnTop(Form1) 'Form1 being the form.
Add the following code wherever you want the NotOnTop code to execute...
Call NotOnTop(Form1) 'Form1 being the form.