从 .NET 应用程序抓取并移动应用程序窗口?

发布于 2024-07-06 09:45:57 字数 104 浏览 5 评论 0原文

.NET 应用程序是否可以获取当前打开的所有窗口句柄,并移动/调整这些窗口的大小?

我很确定使用 P/Invoke 是可能的,但我想知道是否有一些用于此功能的托管代码包装器。

Is it possible for a .NET application to grab all the window handles currently open, and move/resize these windows?

I'd pretty sure its possible using P/Invoke, but I was wondering if there were some managed code wrappers for this functionality.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

难理解 2024-07-13 09:45:57

是的,可以使用 Windows API。

这篇文章提供了有关如何从活动进程获取所有窗口句柄的信息:http: //www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=35545

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
       Process[] procs = Process.GetProcesses();
       IntPtr hWnd;
       foreach(Process proc in procs)
       {
          if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
          {
             Console.WriteLine("{0} : {1}", proc.ProcessName, hWnd);
          }
       }         
    }
 }

然后您可以使用 Windows API 移动窗口: http://www.devasp.net/net/articles/display/689.html

[DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);

...

MoveWindow((IntPtr)handle, (trackBar1.Value*80), 20 , (trackBar1.Value*80)-800, 120, true);

以下是 MoveWindow 函数的参数:

为了移动窗口,我们使用
MoveWindow 函数,它需要
窗口句柄,坐标
对于顶角,以及
所需的宽度和高度
窗口,基于屏幕
坐标。 移动窗口函数
定义为:

MoveWindow(HWND hWnd, int nX, int
nY、int nWidth、int nHeight、BOOL
b重绘);

bRepaint 标志
判断是否为客户区
应该被无效,导致
要发送的 WM_PAINT 消息,允许
要重新绘制的客户区。 作为一个
除此之外,屏幕坐标可以是
使用类似的调用获得
获取客户端矩形(获取桌面窗口(),
&rcDesktop),其中 rcDesktop 是
RECT 类型的变量,通过
参考。

(http://windows-programming.suite101.com/article.cfm/client_area_size_with_movewindow)

Yes, it is possible using the Windows API.

This post has information on how to get all window handles from active processes: http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=35545

using System;
using System.Diagnostics;

class Program
{
    static void Main()
    {
       Process[] procs = Process.GetProcesses();
       IntPtr hWnd;
       foreach(Process proc in procs)
       {
          if ((hWnd = proc.MainWindowHandle) != IntPtr.Zero)
          {
             Console.WriteLine("{0} : {1}", proc.ProcessName, hWnd);
          }
       }         
    }
 }

And then you can move the window using the Windows API: http://www.devasp.net/net/articles/display/689.html

[DllImport("User32.dll", ExactSpelling = true, CharSet = System.Runtime.InteropServices.CharSet.Auto)]
        private static extern bool MoveWindow(IntPtr hWnd, int x, int y, int cx, int cy, bool repaint);

...

MoveWindow((IntPtr)handle, (trackBar1.Value*80), 20 , (trackBar1.Value*80)-800, 120, true);

Here are the parameters for the MoveWindow function:

In order to move the window, we use
the MoveWindow function, which takes
the window handle, the co-ordinates
for the top corner, as well as the
desired width and height of the
window, based on the screen
co-ordinates. The MoveWindow function
is defined as:

MoveWindow(HWND hWnd, int nX, int
nY, int nWidth, int nHeight, BOOL
bRepaint);

The bRepaint flag
determines whether the client area
should be invalidated, causing a
WM_PAINT message to be sent, allowing
the client area to be repainted. As an
aside, the screen co-ordinates can be
obtained using a call similar to
GetClientRect(GetDesktopWindow(),
&rcDesktop) with rcDesktop being a
variable of type RECT, passed by
reference.

(http://windows-programming.suite101.com/article.cfm/client_area_size_with_movewindow)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文