Main form wont appear on top

A

Andy Gilman

How can I make the main form of my application appear on top when it is
displayed for the first time. I have a 'splash' / 'loading' screen which
appears while the program is loading and then the main form is displayed.

But if i switch to another app (such as visual studio) then my main form
does not get focus and instead appears as a flashing icon on the taskbar.

I've tried this, to no avail

--------------------------------------------------
private bool isInitialPaint_ = true;

private void MainForm_Paint(object sender,
System.Windows.Forms.PaintEventArgs e) {
if (isInitialPaint_) {
isInitialPaint_ = false;

SplashForm.CloseForm();
this.Activate();
this.BringToFront();
}
}
 
S

Sahil Malik

Put the following code in your splash screen -

Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long,
ByVal hWndInsertAfter As Long, ByVal x As Long, y, 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_NOMOVE = &H2
Private Const SWP_NOSIZE = &H1
Private Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Public Sub MakeNormal(hwnd As Long)
SetWindowPos hwnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub

Public Sub MakeTopMost(hwnd As Long)
SetWindowPos hwnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End Sub

This will make it stay on top of all other windows.

- Sahil Malik
Independent Consultant
You can reach me thru my blog at -
http://www.dotnetjunkies.com/weblog/sahilmalik/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top