问题:当移动时,如何让记事本跟随我的主窗口?运行该程序会产生两个并排的窗口,但只要主窗口通过点击保持移动,记事本就会留在灰尘中,我试图找到记事本跟随它的方法但是将它紧紧地放在主窗口旁边该程序如何运行。 如果主窗口移动,我成功地使记事本移动,但它只是不成比例,因为记事本将以不同的速度移动到另一个方向,所以这还不够。 摘要:我需要一个功能,当用户点击移动窗口时,记事本不断移动(更新)它相对于主窗口位置的位置。
MainWindow.xaml
<Window x:Class="MoveWindow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" WindowStartupLocation="Manual" Height="500" Width="500">
</Window>
MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
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
{
private readonly Process notepadProcess = null;
public MainWindow()
{
InitializeComponent();
Left = 0;
Top = 0;
//these variables are to find the location of mainWindow and add it to it's width --> (x) so that notepad knows where to position itself next to mainwindow
var xx = Application.Current.MainWindow.Top;
var yy = Application.Current.MainWindow.Left;
var zz = Application.Current.MainWindow.Width;
var answer = ((yy + zz) + ((yy + zz)*.5));
int myInt = (int)answer;
var notepadProcess = Process.Start("notepad.exe");
if (notepadProcess != null)
{
notepadProcess.WaitForInputIdle();
// This is the function that sets location of notepad, myInt = 750(x)
CustomMove(notepadProcess, myInt, 0, 500, 500);
}
}
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!");
}
//DLL call to make 'moveWindow' functional
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
}
}
答案 0 :(得分:0)
LocationChanged
上有window
个事件,您可以在每次举起活动时收听并更新记事本的位置。
答案 1 :(得分:0)
这解决了答案,随着窗口位置的变化,你必须明确地和数学地告诉记事本去哪里;
private void Window_LocationChanged(object sender, EventArgs e)
{
var xx = Application.Current.MainWindow.Top;
var yy = Application.Current.MainWindow.Left;
var zz = Application.Current.MainWindow.Width;
var zzz = Application.Current.MainWindow.Height;
var answer = ((yy + zz) + ((yy + zz) * .5));
var answer2 = (((xx + zzz) + ((xx + zzz) * .5))-zzz);
int myInt = (int)answer;
int myIntx = (int)answer2;
CustomMove(notepadProcess, myInt, myIntx, 0, 0);
}
数学的作用是使用主窗口位置的值,并用它来确定记事本x,y。