question

CharlesHe-MSFT avatar image
0 Votes"
CharlesHe-MSFT asked ·

App won't come to the foreground when started

This is a MSDN question asked by Will Pittenger, the source is App won't come to the foreground when started.



I have a WPF application that when I start it from Visual Studio, its main Window goes to the background rather than becoming the foremost window. I tried adding the code below to the Window, but had no effect.

 protected override void OnInitialized(System.EventArgs e)
 {
 base.OnInitialized(e);
 Activate();
 }

What am I doing wrong?





windows-wpf
· 1
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Does this happen with any simple WPF program made on your computer? Maybe the problem is cause by other parts of your application.

0 Votes 0 ·
AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

Hi,

Welcome to our Microsoft Q&A platform!

You can try the following code:

  public partial class MainWindow : Window
     {
         [DllImport("user32.dll")]
         private static extern int SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);
         static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
         const int SWP_SHOWWINDOW = 0x0040;
         public MainWindow()
         {
             InitializeComponent();
             IntPtr handle = new WindowInteropHelper(this).Handle;
             SetWindowPos(handle, HWND_TOPMOST, 0, 0, 100, 200, SWP_SHOWWINDOW);
         }   
     }

Thanks.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

WillPittenger avatar image
0 Votes"
WillPittenger answered ·

Only one reply from the original thread was copied. The above solution had no effect.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.