如何在C#中获取和设置另一个应用程序的窗口位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1364440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get and set the window position of another application in C#
提问by James
How can I get and set the position of another application using C#?
如何使用 C# 获取和设置另一个应用程序的位置?
For example, I would like to get the top left hand coordinates of Notepad (let's say it's floating somewhere at 100,400) and the position this window at 0,0.
例如,我想获得记事本的左上角坐标(假设它漂浮在 100,400 的某个位置)以及该窗口在 0,0 的位置。
What's the easiest way to achieve this?
实现这一目标的最简单方法是什么?
采纳答案by DataDink
I actually wrote an open source DLL just for this sort of thing. Download Here
我实际上为这种事情编写了一个开源 DLL。 在这里下载
This will allow you to find, enumerate, resize, reposition, or do whatever you want to other application windows and their controls. There is also added functionality to read and write the values/text of the windows/controls and do click events on them. It was basically written to do screen scraping with - but all the source code is included so everything you want to do with the windows is included there.
这将允许您查找、枚举、调整大小、重新定位或对其他应用程序窗口及其控件执行任何您想要的操作。还添加了读取和写入窗口/控件的值/文本并对其执行单击事件的功能。它基本上是为了进行屏幕抓取而编写的 - 但是包含了所有源代码,因此您想要对 Windows 执行的所有操作都包含在其中。
回答by David
Try using FindWindow(signature) to get the HWND of the target window. Then you can use SetWindowPos(signature) to move it.
尝试使用FindWindow(签名)来获取目标窗口的 HWND。然后你可以使用SetWindowPos( signature) 来移动它。
回答by driis
You will need to use som P/Invoke interop to achieve this. The basic idea would be to find the window first (for instance, using the EnumWindows function), and then getting the window position with GetWindowRect.
您将需要使用 som P/Invoke interop 来实现这一点。基本思想是首先找到窗口(例如,使用EnumWindows 函数),然后使用GetWindowRect获取窗口位置。
回答by mklement0
David's helpful answerprovides the crucial pointers and helpful links.
David 的有用回答提供了关键的指示和有用的链接。
To put them to use in a self-contained example that implements the sample scenario in the question, using the Windows API via P/Invoke (System.Windows.Forms
is notinvolved):
把它们放在一个自包含的例子使用实现示例场景中的问题,通过使用的P / Invoke(Windows的APISystem.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);
}
}
}