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.
Visual Basic Code Snippets #3
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.
Center a Form on the Screen
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Sub Form_Center(Frm As Form)
'Usually used in form_load
Frm.Left = Screen.Width / 2 - Frm.Width / 2
Frm.Top = Screen.Height / 2 - Frm.Height / 2
End Sub
Add a form named Form1 and add the following code wherever you want the code to execute...
Call Form_Center(Form1)
Center Form at a Given Point on the Screen
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Sub Form_CenterAt(Frm As Form, X As Integer, Y As Integer)
'X: X coordinate to center form at
'Y: Y coordinate to center form at
'Example...
'Form_CenterAt me, screen.width/2, screen.height/2
Frm.Left = X - Frm.Width / 2
Frm.Top = Y - Frm.Height / 2
End Sub
Add a form named Form1 and add the following code wherever you want the code to execute...
Call Form_CenterAt(Form1, screen.width/2, screen.height/2)
Visual Basic Code Snippets #2
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.
Make a Form Cover the Entire Screen
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 Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
Public Const SM_CXSCREEN = 0
Public Const SM_CYSCREEN = 1
Public Const HWND_TOP = 0
Public Const SWP_SHOWWINDOW = &H40
Add the following code wherever you want the code to execute...
Dim cx As Long
Dim cy As Long
Dim RetVal As Long
If Me.WindowState = vbMaximized Then
Me.WindowState = vbNormal
End If
cx = GetSystemMetrics(SM_CXSCREEN)
cy = GetSystemMetrics(SM_CYSCREEN)
RetVal = SetWindowPos(Me.hwnd, HWND_TOP, 0, 0, cx, cy, SWP_SHOWWINDOW)
Flash a Forms Border
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function FlashWindow Lib "User32" (ByVal hWnd As Long, ByVal bInvert As Long) As Long
Add a Timer to a form named Timer1 with an interval of 220 then add the following code into Timer1_Timer...
Dim nReturnValue As Long
nReturnValue = FlashWindow(Form1.hWnd, True)
Visual Basic Code Snippets #1
In this update I'll re-post my Visual Basic code snippets (which are older than five years by the way) that pertain to Forms.
Move a Form Without a Title Bar
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Const HTCAPTION = 2
Public Const WM_NCLBUTTONDOWN = &HA1
Add the following code into the MouseDown event of a Form, PictureBox, or whatever...
ReleaseCapture
SendMessage(hwnd, WM_NCLBUTTONDOWN, HTCAPTION, 0&)
Make a Form Transparent
Compatibility: Win. 98-XP
Add the following code into a module within your project...
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_TRANSPARENT = &H20&
Public Const SWP_FRAMECHANGED = &H20
Public Const SWP_NOMOVE = &H2
Public Const SWP_NOSIZE = &H1
Public Const SWP_SHOWME = SWP_FRAMECHANGED Or SWP_NOMOVE Or SWP_NOSIZE
Public Const HWND_NOTOPMOST = -2
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
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 Function formTransparent(frm As Form)
SetWindowLong frm.hwnd, GWL_EXSTYLE, WS_EX_TRANSPARENT
SetWindowPos frm.hwnd, HWND_NOTOPMOST, 0&, 0&, 0&, 0&, SWP_SHOWME
End Function
Add the following code wherever you want the code to execute...
Call formTransparent(Form1) 'Form1 being the form you want to be transparent.
These two snippets are just the tip of the iceberg! Much more to come!
My Old Visual Basic Public Code/Source
I recently came across some of my really old Visual Basic tutorials and source code snippets I used to have up at my very first version of this site. Well, it's been over five years since these snippets have seen the light of day (or more like, been lit up in pixels on screens) and I believe they still have some use for archival purposes and at the very least helping Visual Basic newbies with the basic concepts of programming with Windows.
I'll go ahead and release them on this site as separate posts and I'll try to categorize them as much as I can. Stay tuned!
(Please note that these code snippets were all written using Visual Basic 6.)
Visual Basic Code Snippets #1
Visual Basic Code Snippets #2
Visual Basic Code Snippets #3
Visual Basic Code Snippets #4
Visual Basic Code Snippets #5
Visual Basic Code Snippets #6
Visual Basic Code Snippets #7
Visual Basic Code Snippets #8
Visual Basic Code Snippets #9
Visual Basic Code Snippets #10
Visual Basic Code Snippets #11
