简体   繁体   English

如何在C#中获取和设置另一个应用程序的窗口位置

[英]How to get and set the window position of another application in C#

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?实现这一目标的最简单方法是什么?

I actually wrote an open source DLL just for this sort of thing. 我实际上是为了这种事情而编写了一个开源DLL。 Download Here 在这里下载

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做的一切都包含在那里。

David's helpful answer provides the crucial pointers and helpful links. 大卫的有用答案提供了关键的指针和有用的链接。

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 not involved): 要将它们用于实现问题中的示例场景的自包含示例,请通过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);
        }
    }

}

Try using FindWindow ( signature ) to get the HWND of the target window. 尝试使用FindWindow签名 )获取目标窗口的HWND。 Then you can use SetWindowPos ( signature ) to move it. 然后,您可以使用SetWindowPos签名 )移动它。

You will need to use som P/Invoke interop to achieve this. 您将需要使用som P / Invoke互操作来实现此目的。 The basic idea would be to find the window first (for instance, using the EnumWindows function ), and then getting the window position with GetWindowRect . 基本思想是首先找到窗口(例如,使用EnumWindows函数 ),然后使用GetWindowRect获取窗口位置。

How to write unit testing for windows position of application in c#?如何在c#中为应用程序的windows位置编写单元测试? Pleae can anyone help same code as above for getting windows position of application but not getting how to write unit test case请任何人都可以帮助与上述相同的代码来获取应用程序的窗口位置,但不知道如何编写单元测试用例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM