C#附加到进程的用法

在这里插入图片描述

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
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" 是要移动的进程的名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

观天小蚁

需要广大市民的支持哦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值