【问题标题】:Grab and move application windows from a .NET app?从 .NET 应用程序中抓取和移动应用程序窗口?
【发布时间】:2010-09-13 13:11:12
【问题描述】:

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

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

【问题讨论】:

  • 使用机械土耳其人 - 只需弹出一个消息框并要求用户为您执行此操作。请务必提供详细说明。 :P 对不起,只是在这里自娱自乐...

标签: .net window-handles


【解决方案1】:

是的,可以使用 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 消息被发送,允许 要重绘的客户区。作为一个 除此之外,屏幕坐标可以是 使用类似的调用获得 GetClientRect(GetDesktopWindow(), &rcDesktop) 以 rcDesktop 为 RECT 类型的变量,由 参考。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 1970-01-01
    • 2018-07-23
    • 1970-01-01
    • 2011-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多