D. Edward Mitchell 16:00, 14 April 2020 (UTC) Hello World!    groupKOS Developer Share —usually UNDER CONSTRUCTION

VB6 source code modPersistMetrics

From groupKOS Developer Share
Jump to navigation Jump to search

edit template

ding ding ding retroDEVELOPMENT ding ding ding
 Microsoft Visual Basic 6.0 Relics of the Dot-Com Bubble

 modPersistMetrics   °   frmLocateFile(w/vanilla controls)   °  

Are there yet VB6 people writing code in under-funded government divisions with CRT monitors
?

Write me an article for this wiki category!  Your commiserater —  Don aght groupKOS doght cφm


What this is

VB6 source code modPersistMetrics is a Microsoft Visual Basic 6.0 (VB6) BASIC language source code.

PersistMetrics.bas allows saving VB Form position across run-times, called 'persistence' in the vernacular of the day when women wore shoulder pads.

The size and location are stored when the program exits from the Form_Unload() event by the putMetrics() function.

The Form_Load() event reads the window location and size metrics from the previous runtime by the getMetrics() function. getMetrics returns a user-data-type to the VB calling form. See the Test-Harness of the 2nd dark window below for the proper implementation.

Tricks o' the trade

The functions in a VB Code Module can pass user-data types (windowMetrics) if the function is 'public' AND (you need to know) the user-data-type is ALSO dimensioned publicly AND from a VB Code Module (or a public property in a class module —let's not go there with the antique show for two functions and a user-type).


See the Example test-harness VB Form.

How to use this source code module

  1. Begin a new Visual Basic 6.0 project
  2. Paste the module source code following into a new VB Code Module and save it as 'PersistMetrics.bas'
  3. Paste the form-code (2nd window below) into a new VB Form Code Module (double-click the form to open the code window), and save it as 'PersistMetrics.bas'.

VB 6.0 Code Module source code

Reworked recently by DonEM from VB6 code from back in the day, circa Win95.
Things to find in Grandpa's vault

Copy and paste the code below (1st window) into a VB 6 Code Module, and save it as 'PersistMetrics.bas'

'''   File:               PersistMetrics.bas version 1
'''   Description:        Put and Get windowMetrics user-type data to/from a one-record binary data file.
'''   Author & copyright: Don@groupKOS.com
'''   Last edit date:     Feb. 15, 2020
'''   Company:            Bucket List Adventures at 7.8 kiloFoot-s

Option Explicit           ' A compile flag: require variable declaration.

Public Type windowMetrics
     top        As Long
     left       As Long
     height     As Long
     width      As Long
     winstate   As Long
End Type


Public Function getMetrics(dataFilePathName As String) As windowMetrics
    Dim handle As Integer
    Dim metrics As windowMetrics ' Create a new type instance.
    
    On Error GoTo error_trap
    
    handle = FreeFile
    Open dataFilePathName For Random As handle Len = Len(metrics)
    Get handle, cRecordIndex, metrics ' Read one data file record into the metrics type.
    Close handle
    
    ' Return the user-type loaded by the Get statement.
    getMetrics = metrics
    
    Exit Function
error_trap:
    ' Exit without the module-scope openFileName variable set.
End Function

Public Function putMetrics(metrics As windowMetrics, dataFile As String) As Boolean
    ' Returns true/false on success/failure.
    ' Only one record is ever written to the data file.
    
    Dim handle          As Integer
    Dim recordIndex     As Long
    
    On Error GoTo error_trap
    
    recordIndex = 1
    
    handle = FreeFile
    Open dataFile For Random As handle Len = Len(metrics)
        Put handle, recordIndex, metrics    ' Write the metrics data as one data file record.
    Close handle
    
    putMetrics = True ' Return success.
    Exit Function
    
error_trap:
    ' Exit without the module-scope openFileName variable set.
    putMetrics = False ' Return failure.
End Function

Test-Harness for PersistMetrics.bas

It's a VB Form which form persists its size and position at shut-down, and restarts to where it was located on the display screen when shut-down prior.

VB 6.0 Form source code (Dbl-click the form for the Form code window)

Useage example (Form code)

This example calls getMetrics() and putMetrics() in the module file PersistMetrics.bas.
Include PersistMetrics.bas above in a code module a VB project with this form.
Copy and paste the form-code below into the code-window of a VB 6 form
Hit F5 (Run)
' Global variables
Dim metrics As windowMetrics
Dim MetricsDataFile As String


Private Sub Form_Load()
    ' Initialize data file in the app folder
    MetricsDataFile = App.Path & "\" & "metrics.dat"
    ' Me.KeyPreview = True   ' Enables KeyPress() event for quick exit (see below)
    If Len(Dir$(MetricsDataFile)) Then
        metrics = getMetrics(MetricsDataFile)
        With Me
            .height = metrics.height
            .left = metrics.left
            .top = metrics.top
            .width = metrics.width
            .WindowState = metrics.winstate
        End With
    End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
    ' Store window state
    metrics.winstate = Me.WindowState

    Select Case Me.WindowState
        Case vbNormal
            With metrics
                .height = Me.height
                .left = Me.left
                .top = Me.top
                .width = Me.width
            End With
        End Select
    putMetrics metrics, MetricsDataFile
End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)
    ' ESC unloads form.
    If KeyAscii = 27 Then Unload Me
End Sub