抓住并从.net应用程序中移动应用程序窗口?应用程序、并从、中移动、窗口

2023-09-04 09:38:34 作者:细数繁华落尽的凄凉

是否有可能为.net应用程序来获取所有的窗口句柄当前打开,和移动/调整这些窗口?

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

我倒是pretty的确定它可能使用P / Invoke,但我不知道是否有一些管理code包装这个功能。

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

推荐答案

是的,这是可以使用Windows API。

Yes, it is possible using the Windows API.

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

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);
          }
       }         
    }
 }

然后你可以移动使用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功能:

Here are the parameters for the MoveWindow function:

为了移动窗口,我们使用   中的MoveWindow函数,该函数   窗口句柄,所述坐标   为上角,以及   希望的宽度和高度   窗口中,根据屏幕上   坐标。该功能的MoveWindow   定义为:

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,诠释nX的,INT   纽约,INT nWidth,nHeight参数INT,BOOL   bRepaint);

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

在bRepaint标志   确定客户端区   应该无效,引起   要发送WM_PAINT消息,允许   客户区重新绘制。作为   一旁,屏幕坐标可以是   使用类似于一个呼叫得到   GetClientRect(GetDesktopWindow(),   &安培; rcDesktop)与rcDesktop是一个   RECT类型的变量,经过   参考。

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)