Grab and move application windows from a .NET app?

空扰寡人 提交于 2019-12-22 04:49:15

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/140993/grab-and-move-application-windows-from-a-net-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!