XAML C# 如何将记事本(或其他程序 window)移动 (x , y)

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

我的 WPF 应用程序有一个按钮,按下该按钮会打开记事本。 现在我需要在 x、y 坐标中打开记事本,我该怎么做? 我基本上想打开我的程序,然后打开记事本并将它放在 (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>

这是 xaml.cs 部分:

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");








}


}
}

你可以使用(@as rehan 说),MoveWindow

检查这个解决方案(在你的情况下是):

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);

MoveWindow 的第一个参数是 Window 处理程序指针(这是您的进程的句柄)。

第二个和第三个是 xy 在屏幕上的位置

第四个和第五个是你的 window

widthheight

第六个参数:

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.

但是,如果您需要有关此方法的更多详细信息,请查看此 link:https://msdn.microsoft.com/en-us/library/windows/desktop/ms633534(v=vs.85).aspx


更新

主要Window 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>