7

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

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

4

1 回答 1

14

是的,可以使用 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 函数定义为:

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

bRepaint 标志确定是否应使客户区无效,从而导致发送 WM_PAINT 消息,从而允许客户区被重新绘制。顺便说一句,可以使用类似于 GetClientRect(GetDesktopWindow(), &rcDesktop) 的调用来获取屏幕坐标,其中 rcDesktop 是 RECT 类型的变量,通过引用传递。

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

于 2008-09-26T18:05:51.790 回答