Technical Details
Internal implementation principles and technical details of cToast.
Table of Contents
- Internal Architecture
- Form Implementation
- Stacking Algorithm
- Event Mechanism
- Lifecycle Management
- Performance Optimization
- Extension Suggestions
Internal Architecture
Class Structure
cToast (Main class)
├── Private members
│ ├── m_ePos ' Current position
│ ├── m_eState ' Current state
│ ├── m_eTheme ' Current theme
│ ├── m_lIndex ' Stack index
│ ├── m_sTagName ' Unique identifier
│ └── m_colToasts ' Collection of all popups
│
├── Public methods (chain call)
│ ├── Pos()
│ ├── State()
│ ├── Theme()
│ ├── InstIndex()
│ ├── Tag()
│ └── Show()
│
├── Management methods
│ ├── CloseMe()
│ ├── CloseAll()
│ └── Exists()
│
└── Properties
├── Count
├── ActiveKeys
└── Events
├── OnToastCountChange
└── OnCloseAllData Flow
User Call → Set Properties → Check Tag → Calculate Index → Create Form → Display → Timer Countdown → Close → Remove from CollectionForm Implementation
FToastCenter - Center Form
Used Positions: Center, CenterTop, CenterBottom
Key Features:
- Center display mode
- Bottom color bar indicating state
- Simple and clean design
- Suitable for important notifications
Technical Implementation:
vb
' Form properties
.BorderStyle = 0 ' No border
.Caption = "" ' No title
.ControlBox = False ' No control box
.Moveable = False ' Non-movable
.ShowInTaskbar = False ' Not shown in taskbar
.StartUpPosition = 0 ' Manual positioning
' Shadow effect (using cShadow component)
Set Shadow = New cShadow
Shadow.Add MeDisplay Algorithm:
- Calculate screen center position
- Apply vertical offset based on stack index
- CenterTop: Positive offset (stack down)
- CenterBottom: Negative offset (stack up)
- Center: No offset (overlay mode)
FToastDrawer - Side Form
Used Positions: LeftTop, LeftBottom, LeftCenter, RightTop, RightBottom, RightCenter
Key Features:
- Side drawer style
- Side color bar indicating state
- Contains title and content two lines
- Suitable for stacking multiple messages
Technical Implementation:
vb
' Form properties
.BorderStyle = 0 ' No border
.Caption = "" ' No title
.ControlBox = False ' No control box
.Moveable = False ' Non-movable
.ShowInTaskbar = False ' Not shown in taskbar
.StartUpPosition = 0 ' Manual positioning
' Layout
' Left side: Color bar (based on state)
' Right side: Title and contentDisplay Algorithm:
- Calculate screen edge position
- Apply horizontal/vertical offset based on stack index
- Top positions: Positive vertical offset (stack down)
- Bottom positions: Negative vertical offset (stack up)
- Center positions: No vertical offset (overlay mode)
Stacking Algorithm
Auto Stack Index Calculation
vb
Private Function GetNextIndex() As Long
' Get all popups with current position
Dim colPos As Collection
Set colPos = GetToastsByPosition(m_ePos)
' Return current count as new index
GetNextIndex = colPos.Count
End FunctionStack Order Rules
Top Positions (CenterTop, LeftTop, RightTop):
- Index 0: Topmost
- Index increases downward
- New popups appear at the bottom
Index 0 ━━━━━━━━━━━
Index 1 ━━━━━━━━━━━
Index 2 ━━━━━━━━━━━ (Newest)Bottom Positions (CenterBottom, LeftBottom, RightBottom):
- Index 0: Bottommost
- Index increases upward
- New popups appear at the top
Index 2 ━━━━━━━━━━━ (Newest)
Index 1 ━━━━━━━━━━━
Index 0 ━━━━━━━━━━━Center Positions (Center, LeftCenter, RightCenter):
- Overlay mode, only one popup visible
- New popups overlay old ones
- Index calculation exists but does not affect display
Offset Calculation
vb
Private Sub CalculatePosition()
Dim x As Long, y As Long
Dim offset As Long
' Base position based on position type
Select Case m_ePos
Case CenterTop
x = Screen.Width / 2 - Me.Width / 2
offset = m_lIndex * (Me.Height + 10)
y = 10 + offset
Case CenterBottom
x = Screen.Width / 2 - Me.Width / 2
offset = m_lIndex * (Me.Height + 10)
y = Screen.Height - Me.Height - 10 - offset
Case LeftTop
x = 10
offset = m_lIndex * (Me.Height + 10)
y = 10 + offset
Case LeftBottom
x = 10
offset = m_lIndex * (Me.Height + 10)
y = Screen.Height - Me.Height - 10 - offset
Case RightTop
x = Screen.Width - Me.Width - 10
offset = m_lIndex * (Me.Height + 10)
y = 10 + offset
Case RightBottom
x = Screen.Width - Me.Width - 10
offset = m_lIndex * (Me.Height + 10)
y = Screen.Height - Me.Height - 10 - offset
' Center positions - no offset
Case Center
x = Screen.Width / 2 - Me.Width / 2
y = Screen.Height / 2 - Me.Height / 2
Case LeftCenter
x = 10
y = Screen.Height / 2 - Me.Height / 2
Case RightCenter
x = Screen.Width - Me.Width - 10
y = Screen.Height / 2 - Me.Height / 2
End Select
Me.Move x, y
End SubEvent Mechanism
Event Declaration
vb
Public Event OnToastCountChange(ByVal TagName As String, _
ByVal IsDelete As Boolean, _
ByVal CurrentCount As Long)
Public Event OnCloseAll(ByVal ClosedCount As Long)Event Trigger Timing
OnToastCountChange:
- Triggered when a new popup is created
- Triggered when a popup is closed
- Provides change details (add/delete, current count)
OnCloseAll:
- Triggered after all popups are closed by CloseAll
- Only triggered once for batch operations
- Provides total number of popups closed
Event Implementation in Form
vb
' In FToastCenter and FToastDrawer forms
Private Sub Form_Unload(Cancel As Integer)
' Notify parent class when form closes
On Error Resume Next
RaiseEvent OnClose
On Error GoTo 0
End Sub
' cToast handles event
Private Sub HandlePopupClose()
m_colToasts.Remove m_sTagName
RaiseEvent OnToastCountChange(m_sTagName, True, m_colToasts.Count)
End SubEvent Best Practices
vb
' In user code
Private WithEvents Toast As cToast
Private Sub Toast_OnToastCountChange(ByVal TagName As String, _
ByVal IsDelete As Boolean, _
ByVal CurrentCount As Long)
' Update UI
UpdateCountLabel CurrentCount
' Log
If IsDelete Then
Log "Popup " & TagName & " closed"
End If
End SubLifecycle Management
Creation Lifecycle
1. User calls .Show()
↓
2. Check if Tag exists
↓
3. If exists, return (no duplicate creation)
↓
4. If not exists, calculate position and index
↓
5. Create form (FToastCenter or FToastDrawer)
↓
6. Set form properties and content
↓
7. Add to collection
↓
8. Trigger OnToastCountChange event
↓
9. Start timer countdown
↓
10. Display formDisplay Lifecycle
1. Form displayed
↓
2. Timer starts countdown
↓
3. User can hover to pause countdown
↓
4. Countdown completes or user manually closes
↓
5. Form starts unload
↓
6. Remove from collection
↓
7. Trigger OnToastCountChange event
↓
8. Form completely unloadedResource Cleanup
vb
' cToast cleanup
Public Sub CloseAll()
Dim i As Long
Dim count As Long
count = m_colToasts.Count
' Close all popups
For i = count To 1 Step -1
On Error Resume Next
Dim frm As Object
Set frm = m_colToasts(i)
Unload frm
On Error GoTo 0
Next i
' Clear collection
Set m_colToasts = New Collection
' Trigger event
RaiseEvent OnCloseAll(count)
End Sub
' Form cleanup
Private Sub Form_Unload(Cancel As Integer)
' Release shadow object
If Not Shadow Is Nothing Then
Set Shadow = Nothing
End If
' Release other resources
' ...
End SubPerformance Optimization
Memory Management
- Prompt Resource Release
vb
' User code should release object references promptly
Set Toast = Nothing- Avoid Memory Leaks
vb
' Check if exists before creating
If Not Toast.Exists("msg1") Then
Toast.Tag("msg1").Show "Message", 0
End If- Collection Management
vb
' Forms are automatically removed from collection on unload
' No manual intervention neededDisplay Performance
- Delay Loading
vb
' Forms are only created when needed
' Not pre-loaded at program start- Optimized Drawing
vb
' Use simple controls
' Avoid complex graphics operations
' Ensure smooth animations- Timer Optimization
vb
' Each form has its own timer
' No shared timer conflictsEvent Optimization
- Avoid Frequent Triggers
vb
' OnCloseAll only triggers once for batch operations
' Avoids excessive event triggers- Event Debouncing
vb
' User can implement their own debouncing logic
Private Sub Toast_OnToastCountChange(...)
Static lastTime As Long
Dim currentTime As Long
currentTime = GetTickCount()
If currentTime - lastTime < 100 Then Exit Sub
lastTime = currentTime
' Process event
End SubExtension Suggestions
Custom Theme Extension
vb
' Add custom theme enumeration
Public Enum EnumCustomTheme
CustomLight = 0
CustomDark = 1
CustomBlue = 2
CustomGreen = 3
End Enum
' Extend Theme method to support custom themes
Public Function ThemeCustom(ByVal eTheme As EnumCustomTheme) As cToast
' Set custom theme
' ...
Set ThemeCustom = Me
End FunctionCustom Icon Extension
vb
' Add custom icon support
Public Function Icon(ByVal IconPath As String) As cToast
' Set custom icon
m_sIconPath = IconPath
Set Icon = Me
End FunctionAnimation Extension
vb
' Add animation effect
Public Enum EnumAnimation
None = 0
FadeIn = 1
SlideIn = 2
ScaleIn = 3
End Enum
Public Function Animation(ByVal eAnim As EnumAnimation) As cToast
' Set animation type
m_eAnimation = eAnim
Set Animation = Me
End FunctionSound Extension
vb
' Add sound notification
Public Function Sound(ByVal SoundFile As String) As cToast
' Play sound on show
PlaySound SoundFile
Set Sound = Me
End FunctionClick Action Extension
vb
' Add click action
Public Sub OnClick(ByVal Action As String)
m_sClickAction = Action
End Sub
' Handle form click event
Private Sub Form_Click()
' Execute action
ExecuteAction m_sClickAction
End SubProgress Bar Extension
vb
' Add progress bar support
Public Function Progress(ByVal Value As Long, ByVal Max As Long) As cToast
' Show progress bar
m_lProgress = Value
m_lProgressMax = Max
UpdateProgressBar
Set Progress = Me
End FunctionMulti-Line Content Extension
vb
' Support for richer text formatting
Public Function RichText(ByVal HTML As String) As cToast
' Support HTML or RTF format
m_sRichContent = HTML
Set RichText = Me
End FunctionAuto Dismiss on Click Extension
vb
' Click to close
Public Function DismissOnClick() As cToast
m_bDismissOnClick = True
Set DismissOnClick = Me
End Function
' Handle form click
Private Sub Form_Click()
If m_bDismissOnClick Then
Unload Me
End If
End SubSecurity Considerations
Content Injection Prevention
vb
' Validate user input before displaying
Private Function ValidateContent(ByVal Content As String) As String
' Remove dangerous characters
' Prevent XSS injection
' ...
ValidateContent = Sanitize(Content)
End FunctionTag Name Security
vb
' Prevent malicious Tag names
Private Function ValidateTagName(ByVal TagName As String) As Boolean
' Check length
If Len(TagName) > 100 Then Exit Function
' Check for illegal characters
If InStr(TagName, "|") > 0 Then Exit Function
If InStr(TagName, ";") > 0 Then Exit Function
ValidateTagName = True
End FunctionCompatibility Notes
VB6 Version Compatibility
- Minimum required: VB6 SP6
- Recommended: VB6 SP6 with latest updates
Windows Version Compatibility
- Windows XP and later
- Best performance on Windows 7 and later
Dependency Components
- cShadow (for shadow effects)
- Standard VB6 runtime libraries
Debugging Tips
Enable Debug Output
vb
' Add debug logging
Private Sub DebugLog(ByVal msg As String)
Debug.Print "[cToast] " & msg
End Sub
' Log at key points
DebugLog "Creating popup: " & TagName
DebugLog "Closing popup: " & TagNameCheck Popup Status
vb
' List all active popups
Sub ListActiveToasts()
Dim Toast As New cToast
Dim Keys As Collection
Dim Key As Variant
Set Keys = Toast.ActiveKeys
Debug.Print "Active popups: " & Toast.Count
For Each Key In Keys
Debug.Print " - " & CStr(Key)
Next Key
End SubMonitor Event Triggers
vb
Private WithEvents Toast As cToast
Private Sub Toast_OnToastCountChange(ByVal TagName As String, _
ByVal IsDelete As Boolean, _
ByVal CurrentCount As Long)
Debug.Print "Event: " & TagName & " " & _
IIf(IsDelete, "Deleted", "Added") & _
", Count: " & CurrentCount
End SubKnown Limitations
- Maximum Number of Popups: Limited by system resources, recommended not to exceed 50
- Display Position: 9 preset positions only, does not support arbitrary positioning
- Content Length: Extremely long text may affect display效果
- Multi-Monitor Support: Default is primary monitor, multi-monitor support requires modification
- Form Style: Cannot modify basic form properties (borderless, no control box)