wpf - XAML C# How to move notepad(or another programs window) by (x , y) -


my wpf application has button when pressed, opens notepad. need open notepad in x, y coordinates, how do that? want open program, open notepad , place 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 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 mainwindow.xaml /// </summary> public partial class mainwindow : window {       public mainwindow()     {         initializecomponent();         left = 0;         top = 0;         // process.start("notepad.exe");         }   } } 

you can use (@as rehan said), movewindow.

check solution (in case be):

public mainwindow() {    initializecomponent();     var notepadprocess = process.start("notepad.exe");    if (notepadprocess != null)    {        notepadprocess.waitforinputidle();         // positioning @ x=100, y=100 width of: 500 , 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 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 window handler pointer (which process's handle).

the second & third x , y position on screen

the fourth & fifth width , height of window

the sixth parameter :

indicates whether window repainted. if parameter true, window receives message. if parameter false, no repainting of kind occurs. applies client area, nonclient area (including title bar , scroll bars), , part of parent window uncovered result of moving child window.

however if need more details method, checkout 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 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> 

Comments