Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- myWindow.Activate();
- myWindow.TopMost = true;
- if (!Window.IsVisible)
- {
- Window.Show();
- }
- if (Window.WindowState == WindowState.Minimized)
- {
- Window.WindowState = WindowState.Normal;
- }
- Window.Activate();
- Window.Topmost = true; // important
- Window.Topmost = false; // important
- Window.Focus(); // important
- void Window_Loaded(object sender, RoutedEventArgs e)
- {
- // make sure the window is normal or maximised
- // this was the core of the problem for me;
- // even though the default was "Normal", starting it via shell minimised it
- this.WindowState = WindowState.Normal;
- // only required for some scenarios
- this.Activate();
- }
- public partial class Form1 : Form
- {
- [DllImportAttribute("User32.dll")]
- private static extern int FindWindow(String ClassName, String WindowName);
- [DllImportAttribute("User32.dll")]
- private static extern int SetForegroundWindow(int hWnd);
- foreach (Process proc in Process.GetProcesses())
- {
- tx = proc.MainWindowTitle.ToString();
- if (tx.IndexOf("Title of Your app WITHOUT FIRST LETTER") > 0)
- {
- tx = proc.MainWindowTitle;
- hWnd = proc.Handle.ToInt32(); break;
- }
- }
- hWnd = FindWindow(null, tx);
- if (hWnd > 0)
- {
- SetForegroundWindow(hWnd);
- }
- void hotkey_execute()
- {
- IntPtr handle = new WindowInteropHelper(Application.Current.MainWindow).Handle;
- BackgroundWorker bg = new BackgroundWorker();
- bg.DoWork += new DoWorkEventHandler(delegate
- {
- Thread.Sleep(10);
- SwitchToThisWindow(handle, true);
- });
- bg.RunWorkerAsync();
- }
- public class MoveToForeground
- {
- [DllImportAttribute("User32.dll")]
- private static extern int FindWindow(String ClassName, String WindowName);
- const int SWP_NOMOVE = 0x0002;
- const int SWP_NOSIZE = 0x0001;
- const int SWP_SHOWWINDOW = 0x0040;
- const int SWP_NOACTIVATE = 0x0010;
- [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
- public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
- public static void DoOnProcess(string processName)
- {
- var allProcs = Process.GetProcessesByName(processName);
- if (allProcs.Length > 0)
- {
- Process proc = allProcs[0];
- int hWnd = FindWindow(null, proc.MainWindowTitle.ToString());
- // Change behavior by settings the wFlags params. See http://msdn.microsoft.com/en-us/library/ms633545(VS.85).aspx
- SetWindowPos(new IntPtr(hWnd), 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW | SWP_NOACTIVATE);
- }
- }
- }
Add Comment
Please, Sign In to add comment