PDA

View Full Version : Splash Screen



ltheoret
11-02-2014, 21:07
Hi,
I am working with a encyclopaedia of about 54000 records. This takes about 4 seconds to load and configure. Is there a way I could put a splash screen at the start and get rid of the Console all together. I am not a fan of software that do not show life write from the start.
Here is my code:



' Basic Template for custom dialog
' Start Date 12-25-2013
' Created by Luc Theoret

'------------------------------------------------------------------------------
'thinAir Visual Designer
'------------------------------------------------------------------------------
' Project: Visual Designer generated code
' File: Your file name
' Created: On 12-25-2013 at 21:44:14
'------------------------------------------------------------------------------

'---Needed thinBasic modules
Uses "Console"
Uses "UI"
Uses "File"

'---Controls IDs---
Begin ControlID
%IDC_LISTBOX_1
%IDC_STATICLABEL_1
%IDC_EDIT_SEARCH
%IDC_BUTTON_SEARCH
%IDC_EDIT_TOPIC
%IDC_BUTTON_CLOSE
%IDC_EDIT_DISP
End ControlID

Global Records As Long
Global Index() As String
Global Dict() As String


'------------------------------------------------------------------------------
' Main thinBasic function
'------------------------------------------------------------------------------
Function TBMain() As Long

Dim FileName As String
Dim tick, tack As Long
Dim Total As Double

FileName = APP_ScriptPath + "Data Sort.dat" ' Build filename

If Not(FILE_Exists(FileName)) Then
PrintL
PrintL "Cannot find input file: " + FileName
PrintL "program terminated"
PrintL "Press any key..."
While Not Console_InKey
Wend
Stop
End If

PrintL "Loading file: " + "Data.dat" + " ..."
tick = GetTickCount
Records = LoadFile(FileName)
tack = GetTickCount
Total = (tack-tick)/1000
PrintL "File Loaded - " + Records + " Records loaded " + Total + " Secounds"

' PrintL "Sorting Records ..."
' tick = GetTickCount
' Adv_Bubble()
' tack = GetTickCount
' Total = (tack-tick)/1000
' Print: "Sorting done" + Total + " Secounds"

MainWindow_Create(%HWND_DESKTOP)

End Function

'------------------------------------------------------------------------------
' Create main Window
'------------------------------------------------------------------------------
Function MainWindow_Create(ByVal hParent As Long) As Long

Local hDlg As Long
Local hFont As Long
Local lStyle As Long
Local lStyleEx As Long
lStyle = _
%WS_DLGFRAME | _
%WS_CAPTION | _
%WS_SYSMENU | _
%WS_OVERLAPPEDWINDOW | _
%WS_CLIPCHILDREN | _
%WS_CLIPSIBLINGS | _
%DS_CENTER
lStyleEx = 0
Local Tick, Tack As Long
Local Total As Double

Dialog New Pixels, hParent, "Probert Encyclopaedia", -1, -1, 602, 402, lStyle, lStyleEx, To hDlg

'---Set the minimum size of the dialog
Dialog Set MinClientSize hDlg, 602, 402

hFont = Font_Create("MS Sans Serif", 8)
Dialog Send hDlg, %WM_SETFONT, hFont, 0



Control Add LISTBOX, hDlg, %IDC_LISTBOX_1, , 8, 8, 152, 384, %WS_BORDER Or %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %WS_VSCROLL Or %LBS_NOINTEGRALHEIGHT Or %LBS_NOTIFY, %WS_EX_RIGHTSCROLLBAR
Control Add Label, hDlg, %IDC_STATICLABEL_1, "Search", 168, 8, 80, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_VISIBLE Or %SS_NOTIFY, 0
Control Add Textbox, hDlg, %IDC_EDIT_SEARCH, "Search", 248, 8, 248, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDC_BUTTON_SEARCH, "Search", 504, 8, 88, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %BS_PUSHBUTTON, 0
Control Add Textbox, hDlg, %IDC_EDIT_TOPIC, "Topic", 168, 40, 328, 24, %ES_READONLY Or %ES_AUTOVSCROLL Or %ES_MULTILINE Or %WS_VSCROLL, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDC_BUTTON_CLOSE, "Close", 504, 40, 88, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %BS_PUSHBUTTON, 0
Control Add Textbox, hDlg, %IDC_EDIT_DISP, "Topic", 168, 72, 424, 320, %ES_READONLY Or %ES_AUTOVSCROLL Or %ES_MULTILINE Or %WS_VSCROLL, %WS_EX_CLIENTEDGE

Control Set Resize hDlg, %IDC_LISTBOX_1 , 0, 0, 1, 1
Control Set Resize hDlg, %IDC_EDIT_DISP , 1, 1, 1, 1

PrintL "Loading ListBox ..."
tick = GetTickCount
SampleListBox(hDlg, %IDC_LISTBOX_1, Records)
tack = GetTickCount
Total = (tack-tick)/1000
PrintL "Done " + Total + " Secounds"

Dialog Show Modal hDlg, Call MainWindow_Proc

Win_DeleteObject hFont

End Function

'------------------------------------------------------------------------------
' Main WIndow CallBack handler
'------------------------------------------------------------------------------
CallBack Function MainWindow_Proc() As Long
Local tmpStr As String
Local tmpLong As Long

Local pNMHDR As NMHDR Ptr

Local PageNo As Long

Local hFontTab As Long

Select Case (CBMSG)

Case %WM_INITDIALOG
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, Index(1)
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(1)
Case %WM_COMMAND
Select Case CBCTL
Case %IDC_LISTBOX_1
Select Case CBCTLMSG
Case %LBN_SELCHANGE
LISTBOX Get Text CBHNDL, %IDC_LISTBOX_1 To tmpStr
LISTBOX Get Selected CBHNDL, %IDC_LISTBOX_1 To tmpLong
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, tmpStr
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(tmpLong)
End Select
Case %IDC_BUTTON_SEARCH
Control Set Text CBHNDL, %IDC_BUTTON_SEARCH, "Searching ..."
Control Get Text CBHNDL, %IDC_EDIT_SEARCH To tmpStr
tmpLong = SearchDict(tmpStr)
If tmpLong > 0 Then
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, Index(tmpLong)
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(tmpLong)
Else
tmpStr = tmpStr + " Not Found"
Control Set Text CBHNDL, %IDC_EDIT_SEARCH, tmpStr
End If
Control Set Text CBHNDL, %IDC_BUTTON_SEARCH, "Search"
Case %IDC_BUTTON_CLOSE
Dialog End CBHNDL
End Select
Select Case LOWRD(CBWPARAM)
Case %IDOK
Case %IDCANCEL
End Select

Case %WM_NOTIFY
Case %WM_DESTROY
End Select
End Function

'--------------------------------------------------------------------------------
' Load Encyclopaedia into the LixtBox
'--------------------------------------------------------------------------------
Function SampleListBox(ByVal hDlg As Long,ByVal lID As Long,ByVal lCount As Long) As Long

Local i As Long

For i = 1 To lCount - 1
LISTBOX Add hDlg, lID, Index(i)
Next i

End Function

'--------------------------------------------------------------------------------
' Load Dictionary
'--------------------------------------------------------------------------------
Function LoadFile(FileName As String) As Long
Local Temp As String
Local FileHandle As DWord
Local Count, CLoop As Long

FileHandle = FILE_Open(FileName, "INPUT")

Count = 1

While Not FILE_EOF(FileHandle)
Temp = FILE_LineInput(FileHandle)
Temp = FILE_LineInput(FileHandle)
Count = Count + 1
Wend
FILE_Close(FileHandle)

ReDim Index(Count - 1) As String
ReDim Dict(Count - 1) As String

FileHandle = FILE_Open(FileName, "INPUT")

Count = 1

While Not FILE_EOF(FileHandle)
Index(Count) = FILE_LineInput(FileHandle)
Dict(Count) = FILE_LineInput(FileHandle)
Count = Count + 1
Wend

FILE_Close(FileHandle)

For CLoop = 1 To Count - 1
Index(CLoop) = LTrim$(Index(CLoop), "*")
Next

Return Count
End Function

'--------------------------------------------------------------------------------
' Search Dictionary
'--------------------------------------------------------------------------------
Function SearchDict(ItemName As String) As Long
Static TmpCount, ItemLen As Long
Static TmpStr As String
TmpCount = 1

ItemName = Ucase$(ItemName)
ItemLen = Len(ItemName)
TmpStr = Ucase$(LEFT$(Index(TmpCount), ItemLen))
While TmpCount < (Records-1) And ItemName <> TmpStr
TmpCount = TmpCount + 1
TmpStr = Ucase$(LEFT$(Index(TmpCount), ItemLen))
Wend
If tmpCount = (Records-1) And ItemName <> TmpStr Then
Return -1
Else
Return TmpCount
End If

End Function

'--------------------------------------------------------------------------------
' Sort Dictionary
'--------------------------------------------------------------------------------
Function Adv_Bubble()
Local Count1, Count2 As Long
Local CleanPass As Integer = 0
Local TmpInd, TmpTopic As String

For Count1 = 1 To Records - 2 And Not CleanPass
CleanPass = 1
For Count2 = Count1 + 1 To Records - 1
If (Index(Count1) > Index(Count2)) Then
TmpInd = Index(Count1)
TmpTopic = Dict(Count1)
Index(Count1) = Index(Count2)
Dict(Count1) = Dict(Count2)
Index(Count2) = TmpInd
Dict(Count2) = TmpTopic
CleanPass = 0
End If
Next
Next
End Function

Petr Schreiber
11-02-2014, 22:15
You can use the splashscreen unit I use myself, see the attachement.


Petr

ltheoret
11-02-2014, 22:23
For the splash screen I was envisioning something like an about box but saying initializing.
in the above code on line 54 add splashscreen.show and on line 121 add splashscreen.hide. but I do not know how it could be done. I could also move the LoadFile from line 57 to line 114 and do all the splash screen there if that is possible.

Thanks

ltheoret
11-02-2014, 22:29
Thanks Petr that is what I was looking for.

Luc

ltheoret
11-02-2014, 23:20
Yup that works for me.
Here is my code



' Program to read and display the Probert Encyclopedia database
' Basic Template for custom dialog
' Start Date 12-25-2013
' Created by Luc Theoret

'------------------------------------------------------------------------------
'thinAir Visual Designer
'------------------------------------------------------------------------------
' Project: Visual Designer generated code
' File: Your file name
' Created: On 12-25-2013 at 21:44:14
'------------------------------------------------------------------------------

'---Needed thinBasic modules
' Uses "Console"
Uses "UI"
Uses "File"

#INCLUDE "tSplashScreen.tBasicU"

'---Controls IDs---
Begin ControlID
%IDC_LISTBOX_1
%IDC_STATICLABEL_1
%IDC_EDIT_SEARCH
%IDC_BUTTON_SEARCH
%IDC_EDIT_TOPIC
%IDC_BUTTON_CLOSE
%IDC_EDIT_DISP
End ControlID

Global Records As Long
Global Index() As String
Global Dict() As String


'------------------------------------------------------------------------------
' Main thinBasic function
' Load the database and create the window
'------------------------------------------------------------------------------
Function TBMain() As Long

Dim FileName As String
Dim tick, tack As Long
Dim Total As Double

FileName = APP_ScriptPath + "Data Sort.dat" ' Build filename

If Not(FILE_Exists(FileName)) Then
MsgBox(,"Cannot find input file: " + FileName +". Program terminated")
Stop
End If


MainWindow_Create(%HWND_DESKTOP, FileName)

End Function

'------------------------------------------------------------------------------
' Create main Window
' and load the listbox
'------------------------------------------------------------------------------
Function MainWindow_Create(ByVal hParent As Long, FileName As String) As Long

Local hDlg As Long
Local hFont As Long
Local lStyle As Long
Local lStyleEx As Long

' -- Create splash screen object
Dim SplashScreen1 As tSplashScreen

lStyle = _
%WS_DLGFRAME | _
%WS_CAPTION | _
%WS_SYSMENU | _
%WS_OVERLAPPEDWINDOW | _
%WS_CLIPCHILDREN | _
%WS_CLIPSIBLINGS | _
%DS_CENTER
lStyleEx = 0
' Local Tick, Tack As Long
' Local Total As Double

Dialog New Pixels, hParent, "Probert Encyclopaedia", -1, -1, 602, 402, lStyle, lStyleEx, To hDlg

'---Set the minimum size of the dialog
Dialog Set MinClientSize hDlg, 602, 402

hFont = Font_Create("MS Sans Serif", 8)
Dialog Send hDlg, %WM_SETFONT, hFont, 0



Control Add LISTBOX, hDlg, %IDC_LISTBOX_1, , 8, 8, 152, 384, %WS_BORDER Or %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %WS_VSCROLL Or %WS_HSCROLL Or %LBS_NOINTEGRALHEIGHT Or %LBS_NOTIFY, %WS_EX_RIGHTSCROLLBAR
Control Add Label, hDlg, %IDC_STATICLABEL_1, "Search", 168, 8, 80, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_VISIBLE Or %SS_NOTIFY, 0
Control Add Textbox, hDlg, %IDC_EDIT_SEARCH, "Search", 248, 8, 248, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDC_BUTTON_SEARCH, "Search", 504, 8, 88, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %BS_PUSHBUTTON, 0
Control Add Textbox, hDlg, %IDC_EDIT_TOPIC, "Topic", 168, 40, 328, 24, %ES_READONLY Or %ES_AUTOVSCROLL Or %ES_MULTILINE Or %WS_VSCROLL, %WS_EX_CLIENTEDGE
Control Add Button, hDlg, %IDC_BUTTON_CLOSE, "Close", 504, 40, 88, 24, %WS_CHILD Or %WS_CLIPSIBLINGS Or %WS_TABSTOP Or %WS_VISIBLE Or %BS_PUSHBUTTON, 0
Control Add Textbox, hDlg, %IDC_EDIT_DISP, "Topic", 168, 72, 424, 320, %ES_READONLY Or %ES_AUTOVSCROLL Or %ES_MULTILINE Or %WS_VSCROLL, %WS_EX_CLIENTEDGE

Control Set Resize hDlg, %IDC_LISTBOX_1 , 0, 0, 1, 1
Control Set Resize hDlg, %IDC_EDIT_DISP , 1, 1, 1, 1

' -- Display splash screen
Splashscreen1.Display(APP_SourcePath+"splash.bmp", 387, 194, %BLACK, %WHITE, %RED)

Records = LoadFile(FileName)

SampleListBox(hDlg, %IDC_LISTBOX_1, Records)

' -- Close the splashscreen
Splashscreen1.Close()

Dialog Show Modal hDlg, Call MainWindow_Proc

Win_DeleteObject hFont

End Function

'------------------------------------------------------------------------------
' Main WIndow CallBack handler
' This is where everything happens
'------------------------------------------------------------------------------
CallBack Function MainWindow_Proc() As Long
Local tmpStr As String
Local tmpLong As Long

Local pNMHDR As NMHDR Ptr

Local PageNo As Long

Local hFontTab As Long

Select Case (CBMSG)

Case %WM_INITDIALOG
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, Index(1)
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(1)
Case %WM_COMMAND
Select Case CBCTL
Case %IDC_LISTBOX_1
Select Case CBCTLMSG
Case %LBN_SELCHANGE
LISTBOX Get Text CBHNDL, %IDC_LISTBOX_1 To tmpStr
LISTBOX Get Selected CBHNDL, %IDC_LISTBOX_1 To tmpLong
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, tmpStr
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(tmpLong)
End Select
Case %IDC_BUTTON_SEARCH
Control Set Text CBHNDL, %IDC_BUTTON_SEARCH, "Searching ..."
Control Get Text CBHNDL, %IDC_EDIT_SEARCH To tmpStr
tmpLong = SearchDict(tmpStr)
If tmpLong > 0 Then
Control Set Text CBHNDL, %IDC_EDIT_TOPIC, Index(tmpLong)
Control Set Text CBHNDL, %IDC_EDIT_DISP, Dict(tmpLong)
LISTBOX Select CBHNDL, %IDC_LISTBOX_1, tmpLong
Else
tmpStr = tmpStr + " Not Found"
Control Set Text CBHNDL, %IDC_EDIT_SEARCH, tmpStr
End If
Control Set Text CBHNDL, %IDC_BUTTON_SEARCH, "Search"
Case %IDC_BUTTON_CLOSE
Dialog End CBHNDL
End Select
Select Case LOWRD(CBWPARAM)
Case %IDOK
Case %IDCANCEL
End Select

Case %WM_NOTIFY
Case %WM_DESTROY
End Select
End Function

'--------------------------------------------------------------------------------
' Load Encyclopaedia into the LixtBox
'--------------------------------------------------------------------------------
Function SampleListBox(ByVal hDlg As Long,ByVal lID As Long,ByVal lCount As Long) As Long

Local i As Long

For i = 1 To lCount - 1
LISTBOX Add hDlg, lID, Index(i)
Next i

End Function

'--------------------------------------------------------------------------------
' Load Dictionary from file and load it into two arrays
'--------------------------------------------------------------------------------
Function LoadFile(FileName As String) As Long
Local Temp As String
Local FileHandle As DWord
Local Count, CLoop As Long

FileHandle = FILE_Open(FileName, "INPUT")

Count = 1

While Not FILE_EOF(FileHandle)
Temp = FILE_LineInput(FileHandle)
Temp = FILE_LineInput(FileHandle)
Count = Count + 1
Wend
FILE_Close(FileHandle)

ReDim Index(Count - 1) As String
ReDim Dict(Count - 1) As String

FileHandle = FILE_Open(FileName, "INPUT")

Count = 1

While Not FILE_EOF(FileHandle)
Index(Count) = FILE_LineInput(FileHandle)
Dict(Count) = FILE_LineInput(FileHandle)
Count = Count + 1
Wend

FILE_Close(FileHandle)

For CLoop = 1 To Count - 1
Index(CLoop) = LTrim$(Index(CLoop), "*")
Next

Return Count
End Function

'--------------------------------------------------------------------------------
' Search Dictionary and return results
'--------------------------------------------------------------------------------
Function SearchDict(ItemName As String) As Long
Static TmpCount, ItemLen As Long
Static TmpStr As String
TmpCount = 1

ItemName = Ucase$(ItemName)
ItemLen = Len(ItemName)
TmpStr = Ucase$(LEFT$(Index(TmpCount), ItemLen))
While TmpCount < (Records-1) And ItemName <> TmpStr
TmpCount = TmpCount + 1
TmpStr = Ucase$(LEFT$(Index(TmpCount), ItemLen))
Wend
If tmpCount = (Records-1) And ItemName <> TmpStr Then
Return -1
Else
Return TmpCount
End If

End Function


Still have to work on the progress bar but every thing else works.
Luc

ErosOlmi
11-02-2014, 23:22
Wow, Petr was much quicker than me and with a great "OOP like" example :D
Great code Petr, thanks.


Here my more "human" contribution of a "Splash Screen alike" example:


Uses "UI"


Begin ControlID
%ID_SPLASH_LISTVIEW
%ID_SPLASH_PROGRESS
End ControlID


Global hSplash As Long
Global lNumberOfSteps As Long = 200
Global lStep As Long


hSplash = Splash_Create(%HWND_DESKTOP)
Splash_SetRange(hSplash, 1, lNumberOfSteps)


For lStep = 1 To lNumberOfSteps


If Splash_StillAlive(hSplash) Then
'---do something that last some time
Sleep 50
Splash_AddInfo(hSplash, "Performing step " & lStep & "|" & Expand$("Some additional info regarding step $lStep here"))
Splash_SetPos(hSplash, lStep)
Else
MsgBox 0, "Process aborted by user"
Exit For
End If


Next
Splash_Close(hSplash)




Function Splash_Create(ByVal hParent As Long)
Dim localhSplash As Long

Dialog New Pixels, hParent, "Wait while loading resources ...", -1, -1, 450, 250,
%WS_DLGFRAME | _
%ds_center | _
%WS_CAPTION | _
%WS_SYSMENU _
To localhSplash


Control Add Listview , localhSplash, %ID_SPLASH_LISTVIEW , "", 5, 5, 440, 220, %WS_CHILD Or %WS_VISIBLE
ListView_SetView localhSplash, %ID_SPLASH_LISTVIEW, %LV_VIEW_DETAILS
ListView_InsertColumn localhSplash, %ID_SPLASH_LISTVIEW, 1, 100, "Step", %LVCF_FMT | %LVCF_TEXT | %LVCF_WIDTH, %LVCFMT_LEFT
ListView_InsertColumn localhSplash, %ID_SPLASH_LISTVIEW, 2, 320, "Info", %LVCF_FMT | %LVCF_TEXT | %LVCF_WIDTH, %LVCFMT_LEFT


Control Add ProgressBar , localhSplash, %ID_SPLASH_PROGRESS , "", 5, 235, 440, 10, %WS_CHILD Or %WS_VISIBLE

'---Show dialog
Dialog Show Modeless localhSplash', Call Splash_CallBack


Function = localhSplash

End Function


Function Splash_Close(ByVal hWindow As Long) As Long
Dialog End hWindow
End Function


Function Splash_SetRange(ByVal hWindow As Long, ByVal lMin As Long, ByVal lMax As Long) As Long
ProgressBar_SetRange hWindow, %ID_SPLASH_PROGRESS, lMin, lMax
End Function


Function Splash_SetPos(ByVal hWindow As Long, ByVal lPos As Long) As Long
ProgressBar_SetPos hWindow, %ID_SPLASH_PROGRESS, lPos
DoEvents
End Function


Function Splash_AddInfo(ByVal hWindow As Long, ByVal sText As String) As Long
ListView_InsertItem hWindow, %ID_SPLASH_LISTVIEW, 1, sText
DoEvents
End Function


Function Splash_StillAlive(ByVal hWindow As Long) As Long
Function = IsWindow(hWindow)
End Function

Petr Schreiber
11-02-2014, 23:42
I was faster, but I like your flexible example more so... I created a hybrid :D and OOPified yours.
Usage is like this, no need to pass handles around, smart TYPE can remember them:

' SmartSplash test

#INCLUDE "SmartSplash.tBasicU"

Function TBMain()

Dim splash As SmartSplash

Long lNumberOfSteps = 200
Long lStep

splash.Create(%HWND_DESKTOP)
splash.SetRange(1, lNumberOfSteps)


For lStep = 1 To lNumberOfSteps

If splash.StillAlive() Then
'---do something that last some time
Sleep 50
splash.AddInfo("Performing step " & lStep & "|" & Expand$("Some additional info regarding step $lStep here"))
splash.SetPos(lStep)
Else
MsgBox 0, "Process aborted by user"
Exit For
End If

Next
splash.Close()

End Function


Full code in the attachement :)


Petr

ErosOlmi
11-02-2014, 23:49
Your OOPification is GREAT!

:D

Forgot to say: ltheoret welcome to to thinBasic community.

ltheoret
12-02-2014, 00:09
Well thank you. And thank you for the unexpected fast response.

Luc