Splash Screen - stay on top

Hi,

I have a splash screen in my app. Right now it is loaded in sub main before the main form loads. I am using windows api to make the splash screen stay on top of all applications. Unfortunately sometimes my application takes a long time to load, so having the form stay on top of everything is annoying. How can I make it so that it will only stay on top of my application, while still having it start up before my app.

Thanks,

Zaphod.
LVL 7
Z_BeeblebroxAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
amebaConnect With a Mentor Commented:
>so having the form stay on top of everything is annoying.

Good catch!  I will never understand why *all* samples put Splash 'Always_On_Top'.

>How can I make it so that it will only stay on top of my application

Show it the 'normal' way:
    frmSplash.Show
and then show your main form *behind* it:
    ' use second parameter in SetWindowPos - insert main form AFTER frmSplash
    SetWindowPos frmMain.hwnd, frmSplash.hwnd, 0, 0, 0, 0, SWP_SHOWWINDOW Or _
        SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE

I'll post a little sample...
0
 
kiprimshotCommented:
Why not just load your app in the background...dont show it until all is loaded
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Actually, i have generally this type of code:

private gfrmMain as frmMain


Sub Main()
  dim frmSplash as frmSplash
 
  set gfrmMain = new frmMain
  set frmSplash = new frmSplash

  gfrmMain.Visible = true
  frmSplash.Show vbModeless, gfrmMain
  doevents

  ...
  continue with the startup of app
  ...


  frmSplash.hide  
  unload frmSplash
  set frmSplash = nothing

End Sub



This needs that there is NO code in the Form_Load() event of frmMain , and the initial size should be quite small.

Cheers
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
MicrosoftCommented:
Sub Timer1_Timer ()  
Unload Form1
Load Form2

Form2.Show  
End Sub'The above code tells Visual Basic that
'     after the Timer control waits for the ti
'     me specified by the Interval property, i
'     t should unload Form1 (your splash scree
'     n) and then load and display Form2 (whic
'     h contains your 'program's first screen)
'     .


Sub Image1_Click ()  
Timer1.Enabled = False
Unload Form1  
Load Form2
Form2.Show  
End Sub'Experiment with creating splash screens


 

0
 
Z_BeeblebroxAuthor Commented:
ameba, your answer seems closest to what I want, but it doesn't seem to work. Regardless of what I do, the splash screen comes up behind the main form.

Any ideas?

Zaphod.
0
 
amebaCommented:
' frmMain ------------------------------------------------------------
Option Explicit

Private Sub Form_Load()
    Sleep 3000
End Sub

' frmSplash borderstyle=None, caption="" -----------------------------
Option Explicit

Private Sub Form_Load()
    BackColor = vbInfoBackground  'background color of tooltips
End Sub

' module1  -----------------------------------------------------------
Option Explicit
' API declarations
Private Declare Function GetActiveWindow Lib "user32" () As Long
Private 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
Private Const HWND_TOPMOST = -1
Private Const HWND_NOTOPMOST = -2
Private Const SWP_NOSIZE = &H1
Private Const SWP_NOACTIVATE = &H10
Private Const SWP_SHOWWINDOW = &H40
Private Const SWP_NOMOVE = &H2
Private Const SWP_NOZORDER = &H4
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sub main()
    frmSplash.Show
    DoEvents
   
    Load frmMain ' this
    DoEvents
    If GetActiveWindow = frmSplash.hwnd Then
        ' frmsplash is on top
        ' temporary put frmSplash TOPMOST, just to allow frmMain to show with focus
        SetWindowPos frmSplash.hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or _
                SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOMOVE
        frmMain.Show
        SetWindowPos frmSplash.hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOACTIVATE Or _
                SWP_SHOWWINDOW Or SWP_NOSIZE Or SWP_NOMOVE
    Else
        ' frmsplash is not on top
        ' use second parameter in SetWindowPos - insert mainform AFTER frmSplash, without focus
        SetWindowPos frmMain.hwnd, frmSplash.hwnd, 0, 0, 0, 0, SWP_SHOWWINDOW Or _
                SWP_NOSIZE Or SWP_NOMOVE Or SWP_NOACTIVATE
    End If
    DoEvents
   
    Sleep 2000
    DoEvents
    Sleep 2000

    Unload frmSplash
    Set frmSplash = Nothing
End Sub
0
 
Z_BeeblebroxAuthor Commented:
Hi,

Heres what I did:

Put this into a module, then just before showing the main form (inside the main form load event) call set owner. It works quite well and is not too complicated. ameba, since you inspired me, I will give you the points.

Zaphod.

Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long)
Private Const GWL_HWNDPARENT = (-8)
Public lngSplashHWND As Long

Public Function SetOwner(ByVal HwndtoUse, ByVal HwndofOwner) As Long
    SetOwner = SetWindowLong(HwndtoUse, GWL_HWNDPARENT, HwndofOwner)
End Function
0
 
amebaCommented:
>ameba, since you inspired me, I will give you the points.

:-) Thanks!

or maybe set owner without API

' in frmMain
Private Sub Form_Load()
    frmSplash.Show vbModeless, Me   ' set owner form
    frmSplash.Refresh
    Show
    DoEvents
   
    Sleep 3000 ' do something
   
    Unload frmSplash
End Sub
0
 
Z_BeeblebroxAuthor Commented:
The reason I do it with api is that i show the splash screen before I start loading the main form, and then later on I set its owner.

Zaphod.
0
 
amebaCommented:
Yes, I understand it, it's very good method.
I just wanted to add something useful if somebody buys this PAQ.  ;-)

I'm glad to meet someone who cares so much for the users, and notices those little details.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.