アプリケーションの位置移動

マルチモニタを使っているときなど、アプリケーションがどこに行ったか見えない場合がある。

強制的にアプリケーションの位置を変更するツールC#で作成。

ソースは以下。(XAMLは省略)

    public partial class MainWindow : Window
    {
        //MoveWindow関数の宣言
        [System.Runtime.InteropServices.DllImport("user32.dll")]
        private static extern int MoveWindow(IntPtr hwnd, int x, int y,
            int nWidth, int nHeight, int bRepaint);
 
        public MainWindow()
        {
            InitializeComponent();
        }
 
        private void btn_Click(object sender, RoutedEventArgs e)
        {
            System.Diagnostics.Process[] plist = System.Diagnostics.Process.GetProcessesByName(ProcessName.Text);
            foreach (var p in plist)
            {
                p.WaitForInputIdle();
                MoveWindow(p.MainWindowHandle,
                    GetValue(PositionX, 0),
                    GetValue(PositionY, 10),
                    GetValue(SizeX, 100),
                    GetValue(SizeY, 200),
                    1);
 
            }
        }
 
        private int GetValue(TextBox inputVal, int initVal)
        {
            int result;
            if(int.TryParse(inputVal.Text, out result))
            {
                return result;
            }
            return initVal;
        }
 
    }