Solved

Splash Screen - stay on top

Posted on 2001-06-07
10
676 Views
Last Modified: 2010-08-05
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.
0
Comment
Question by:Z_Beeblebrox
10 Comments
 
LVL 2

Expert Comment

by:kiprimshot
ID: 6164067
Why not just load your app in the background...dont show it until all is loaded
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 6164093
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
 
LVL 2

Expert Comment

by:Microsoft
ID: 6164103
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
 
LVL 15

Accepted Solution

by:
ameba earned 50 total points
ID: 6164861
>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
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 6165032
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:ameba
ID: 6165088
' 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
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 6165127
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
 
LVL 15

Expert Comment

by:ameba
ID: 6165159
>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
 
LVL 7

Author Comment

by:Z_Beeblebrox
ID: 6165456
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
 
LVL 15

Expert Comment

by:ameba
ID: 6165591
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now