C#中把进程移动到副屏显示
时间: 2024-03-27 16:41:23 浏览: 80
移动窗口,移动其他程序到副显示屏
在C#中,可以使用System.Windows.Forms.Screen类来获取计算机上所有屏幕的信息,包括主屏幕和副屏幕的位置、大小等信息。要将进程移动到副屏幕上,可以使用Process类来获取要移动的进程句柄,然后使用Win32 API函数SetWindowPos将其移动到副屏幕上。
下面是一个示例代码,可以将指定进程的窗口移动到副屏幕上:
```csharp
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class MoveProcessWindowToSecondaryScreen
{
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOMOVE = 0x0002;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_SHOWWINDOW = 0x0040;
public static void MoveToSecondaryScreen(Process process)
{
Screen secondaryScreen = Screen.AllScreens.FirstOrDefault(s => !s.Primary);
if (secondaryScreen != null && process != null && !process.HasExited)
{
IntPtr handle = process.MainWindowHandle;
int width = secondaryScreen.WorkingArea.Width;
int height = secondaryScreen.WorkingArea.Height;
int x = secondaryScreen.WorkingArea.X;
int y = secondaryScreen.WorkingArea.Y;
SetWindowPos(handle, IntPtr.Zero, x, y, width, height, SWP_NOZORDER | SWP_SHOWWINDOW);
}
}
}
```
使用示例:
```csharp
Process process = Process.GetProcessesByName("myprocess").FirstOrDefault();
MoveProcessWindowToSecondaryScreen.MoveToSecondaryScreen(process);
```
其中,"myprocess" 是要移动的进程的名称。
阅读全文