从.NET应用程序抓取并移动应用程序窗口?
时间:2020-03-06 14:47:50 来源:igfitidea点击:
.NET应用程序能否抓住当前打开的所有窗口句柄,并移动/调整这些窗口的大小?
我很确定可以使用P / Invoke,但是我想知道是否有一些用于此功能的托管代码包装器。
解决方案
是的,可以使用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函数的参数:
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)