2

My WPF application has a button that when pressed, opens notepad. Now i need to open notepad in x, y coordinates, how do i do that? I basically want to open my program, then open notepad and place it in (500,1000) (x, y).

<Window x:Class="MoveWindow.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:MoveWindow"
    mc:Ignorable="d"
    Title="MainWindow" WindowStartupLocation="Manual" Height="350" Width="500">
<FrameworkElement Width="110" />




</Window>

This is the xaml.cs part:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace MoveWindow
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{


    public MainWindow()
    {
        InitializeComponent();
        Left = 0;
        Top = 0;

       // Process.Start("notepad.exe");








}


}
}
Adan
  • 453
  • 1
  • 10
  • 27
  • You can check this : http://stackoverflow.com/questions/3032246/c-sharp-opening-process-and-changing-window-position – rehan ahmed Jul 17 '15 at 06:30

1 Answers1

1

You can use (@as rehan said), MoveWindow.

Check this solution (in your case would be):

public MainWindow()
{
   InitializeComponent();

   var notepadProcess = Process.Start("notepad.exe");
   if (notepadProcess != null)
   {
       notepadProcess.WaitForInputIdle();

       // positioning at x=100, y=100 with width of: 500 and height of: 200
       CustomMove(notepadProcess, 100, 100, 500, 200); 
   }
}

public void CustomMove(Process process, int x, int y, int width, int height)
{
   var ok = MoveWindow(process.MainWindowHandle, x, y, width, height, true);
   if (ok == false)
      MessageBox.Show("Couldn't move your window!");
}

[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);

The first parameter of MoveWindow is the Window Handler pointer (which is your process's handle).

The second & third is x and y position on your screen

The fourth & fifth is width and height of your window

The sixth parameter :

Indicates whether the window is to be repainted. If this parameter is TRUE, the window receives a message. If the parameter is FALSE, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of moving a child window.

However if you need more details about this method, checkout this link: https://msdn.microsoft.com/en-us/library/windows/desktop/ms633534(v=vs.85).aspx


UPDATE

MainWindow class

public partial class MainWindow : Window
    {
        private readonly Process notepadProcess = null;

        public MainWindow()
        {
            InitializeComponent();

            notepadProcess = Process.Start("notepad.exe");
            if (notepadProcess != null)
            {
                notepadProcess.WaitForInputIdle();
                CustomMove(notepadProcess, (int) Application.Current.MainWindow.Top, (int) Application.Current.MainWindow.Left, 500, 200);
            }
        }

        public void CustomMove(Process process, int x, int y, int width, int height)
        {
            var ok = MoveWindow(process.MainWindowHandle, x, y, 300, 200, true);
            if (ok == false)
                MessageBox.Show("Couldn't move your window!");
        }

        [DllImport("user32.dll", SetLastError = true)]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);

        private void Window_LocationChanged(object sender, EventArgs e)
        {
            CustomMove(notepadProcess, (int)Application.Current.MainWindow.Top, (int)Application.Current.MainWindow.Left, 500, 200);
        }
    }

MainWindow.xaml

<Window x:Class="WpfApplication2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"
        LocationChanged="Window_LocationChanged">
    <Window.Resources>
    </Window.Resources>
    <Grid>
    </Grid>
</Window>
Shmwel
  • 1,697
  • 5
  • 26
  • 43
  • Thank you very much, i copy-pasted and this worked and did what i asked. <3. One last question; how do i get my wpf form's x,y location? since i can move it around by click-hold it's current position will always change, i'd like notepad to move along with my wpf form and finding it's current position is needed. – Adan Jul 17 '15 at 07:23
  • @AdanRamirez you can check this link: http://stackoverflow.com/questions/9580067/getting-position-width-height-of-mainwindow. So you want to show it exactly over your wpf window? Or? – Shmwel Jul 17 '15 at 07:59
  • Use LocationChanged event and Left and Top Property of your Form. – GreenEyedAndy Jul 17 '15 at 08:07
  • @SDJ Application.Current.MainWindow.Left/Top is what im looking for thanks!!! you gotta give me your paypal email so i can tip you haha, one final question: im assuming that the program has to continuously scan for main window position changes so that notepad can continuously move as mainWindow moves, got any suggestions? – Adan Jul 17 '15 at 08:34
  • 1
    @SDJ for a sec i got an error but i fixed it, replaced wpfApplication2 (xaml side) to MoveWindow' which is the real name of my project, funny error haha anywho thx so much! – Adan Jul 17 '15 at 09:51
  • @SDJ i tweaked the code so that notepad moves exactly like the mainwindow, but... it's hard because when i move the main window, notepad runs alittle slower and it's trying to catch up resulting in a separation of windows, is there a way to match the movement rate with the main window? – Adan Jul 17 '15 at 11:07