|
When we first released our disk based file sort routine as a piece of freeware we wanted to show a splash screen while the sort ran as a piece of advertising.
The challenge of calling a splash screen from a DLL (or from any library code module) was that we did not know if the the currently displayed form in the application running was modal or not. If it was not modal then we could show the splash screen in a non-modal form and let the sort process continue without interruption. If the routine had been called by a modal form then we still wanted our splash screen to be seen but we did not want to stop the sort process - we would have to wait until it completed and then show the splash screen.
The following code snippets show how this was achieved.
In a code module we included the following Windows API declarations
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_TOPMOST = -1 Public Const HWND_NOTOPMOST = -2 Public Const SWP_NOMOVE = &H2 Public Const SWP_NOSIZE = &H1
The splash screen displayed our company logo and contact information and had just one control - a Timer. We wanted our splash screen to be seen so we set it as the topmost from on the desktop in the Form_Resize event.
The sum total of the code on the splash screen was as follows:
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer) SetWindowPos Me.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, 0 If Timer1.Enabled Then Timer1.Enabled = False End If End Sub
Private Sub Form_Resize() SetWindowPos Me.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Timer1.Interval = 4000 Timer1.Enabled = True End Sub
Private Sub Timer1_Timer() Unload Me End Sub
The subroutine that managed the sort called the Show method of the splash form as it started but trapped error 401 (generated if you try to show a non modal form from a modal one) in the error recovery routine. The error recovery set a boolean (ShowFormLate) and this was tested at the end of the sort routine to see if the splash screen now needed to be shown in modal form. The code fragments are:
AditSplash.Show ‘at the start of the routine DoEvents ... ‘This was followed by the sort routine code and afterwards If ShowFormLate Then AditSplash.Show vbModal Else Do Until DateDiff("s", SplashTime, Time) >= 4 DoEvents Loop AditSplash.Hide End If ... ‘this last bit of code was in the error recovery routine Case 401 'This routine is being run from a Modal form so... ShowFormLate = True Resume Next
|