C# 如何在C中获取和设置另一个应用程序的窗口位置#
如何使用C#获取和设置另一个应用程序的位置 例如,我想得到记事本的左上角坐标(假设它在100400的某个地方浮动)和这个窗口在0,0的位置C# 如何在C中获取和设置另一个应用程序的窗口位置#,c#,window,window-position,C#,Window,Window Position,如何使用C#获取和设置另一个应用程序的位置 例如,我想得到记事本的左上角坐标(假设它在100400的某个地方浮动)和这个窗口在0,0的位置 最简单的方法是什么?尝试使用()获取目标窗口的HWND。然后您可以使用()来移动它。您需要使用som p/Invoke interop来实现这一点。基本思想是首先找到窗口(例如,使用),然后使用获取窗口位置。我实际上为这类事情编写了一个开源DLL。 这将允许您查找、枚举、调整大小、重新定位其他应用程序窗口及其控件,或对其执行任何操作。 还添加了读取和写入窗
最简单的方法是什么?尝试使用()获取目标窗口的HWND。然后您可以使用()来移动它。您需要使用som p/Invoke interop来实现这一点。基本思想是首先找到窗口(例如,使用),然后使用获取窗口位置。我实际上为这类事情编写了一个开源DLL。 这将允许您查找、枚举、调整大小、重新定位其他应用程序窗口及其控件,或对其执行任何操作。 还添加了读取和写入窗口/控件的值/文本以及对其执行单击事件的功能。它基本上是用来做屏幕抓取的,但是所有的源代码都包含在里面,所以你想用windows做的一切都包含在里面。提供了关键的指针和有用的链接 要在实现问题中示例场景的自包含示例中使用它们,请通过p/Invoke使用Windows API(
System.Windows.Forms
):
非常有用的例子!非常好的图书馆!
using System;
using System.Runtime.InteropServices; // For the P/Invoke signatures.
public static class PositionWindowDemo
{
// P/Invoke declarations.
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
const uint SWP_NOSIZE = 0x0001;
const uint SWP_NOZORDER = 0x0004;
public static void Main()
{
// Find (the first-in-Z-order) Notepad window.
IntPtr hWnd = FindWindow("Notepad", null);
// If found, position it.
if (hWnd != IntPtr.Zero)
{
// Move the window to (0,0) without changing its size or position
// in the Z order.
SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
}
}
}