code_junkie

Bring a window to the front in WPF

Nov 12th, 2011
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.81 KB | None | 0 0
  1. myWindow.Activate();
  2.  
  3. myWindow.TopMost = true;
  4.  
  5. if (!Window.IsVisible)
  6. {
  7. Window.Show();
  8. }
  9.  
  10. if (Window.WindowState == WindowState.Minimized)
  11. {
  12. Window.WindowState = WindowState.Normal;
  13. }
  14.  
  15. Window.Activate();
  16. Window.Topmost = true; // important
  17. Window.Topmost = false; // important
  18. Window.Focus(); // important
  19.  
  20. void Window_Loaded(object sender, RoutedEventArgs e)
  21. {
  22. // make sure the window is normal or maximised
  23. // this was the core of the problem for me;
  24. // even though the default was "Normal", starting it via shell minimised it
  25. this.WindowState = WindowState.Normal;
  26.  
  27. // only required for some scenarios
  28. this.Activate();
  29. }
  30.  
  31. public partial class Form1 : Form
  32. {
  33. [DllImportAttribute("User32.dll")]
  34. private static extern int FindWindow(String ClassName, String WindowName);
  35. [DllImportAttribute("User32.dll")]
  36. private static extern int SetForegroundWindow(int hWnd);
  37.  
  38. foreach (Process proc in Process.GetProcesses())
  39. {
  40. tx = proc.MainWindowTitle.ToString();
  41. if (tx.IndexOf("Title of Your app WITHOUT FIRST LETTER") > 0)
  42. {
  43. tx = proc.MainWindowTitle;
  44. hWnd = proc.Handle.ToInt32(); break;
  45. }
  46. }
  47. hWnd = FindWindow(null, tx);
  48. if (hWnd > 0)
  49. {
  50. SetForegroundWindow(hWnd);
  51. }
  52.  
  53. void hotkey_execute()
  54. {
  55. IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
  56. BackgroundWorker bg = new BackgroundWorker();
  57. bg.DoWork += new DoWorkEventHandler(delegate
  58. {
  59. Thread.Sleep(10);
  60. SwitchToThisWindow(handle, true);
  61. });
  62. bg.RunWorkerAsync();
  63. }
  64.  
  65. public class MoveToForeground
  66. {
  67. [DllImportAttribute("User32.dll")]
  68. private static extern int FindWindow(String ClassName, String WindowName);
  69.  
  70. const int SWP_NOMOVE = 0x0002;
  71. const int SWP_NOSIZE = 0x0001;
  72. const int SWP_SHOWWINDOW = 0x0040;
  73. const int SWP_NOACTIVATE = 0x0010;
  74. [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
  75. public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
  76.  
  77. public static void DoOnProcess(string processName)
  78. {
  79. var allProcs = Process.GetProcessesByName(processName);
  80. if (allProcs.Length > 0)
  81. {
  82. Process proc = allProcs[0];
  83. int hWnd = FindWindow(null, proc.MainWindowTitle.ToString());
  84. // Change behavior by settings the wFlags params. See http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx
  85. SetWindowPos(new IntPtr(hWnd), 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
  86. }
  87. }
  88. }
Add Comment
Please, Sign In to add comment