c# 窗口控制 SetForegroundWindow 函数不起作用的原因

c#  窗口控制 SetForegroundWindow 函数不起作用的原因

 

原因是窗口最小化后, 不能使用SetForegroundWindow(

 

要先把窗口还原, 再使用SetForegroundWindow

 

窗口还原函数  ShowWindow(findPtr, SW_RESTORE); //将窗口还原

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,您可以使用 SetForegroundWindow API 函数来设置多个窗口。但是,这个函数只能将一个窗口设置为前台窗口。如果您想要将多个窗口设置为前台窗口,您需要按照以下步骤执行: 1. 枚举所有需要设置为前台窗口窗口句柄。 2. 对于每个窗口句柄,使用 SetForegroundWindow 函数将其设置为前台窗口。 下面是一个示例代码: ```csharp using System.Runtime.InteropServices; [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] static extern bool IsWindowVisible(IntPtr hWnd); [DllImport("user32.dll")] static extern int GetWindowText(IntPtr hWnd, StringBuilder lpWindowText, int nMaxCount); [DllImport("user32.dll")] static extern bool SetForegroundWindow(IntPtr hWnd); delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam); static void SetForegroundWindows(string windowTitle) { List<IntPtr> handles = new List<IntPtr>(); EnumWindows(delegate (IntPtr hWnd, IntPtr lParam) { if (IsWindowVisible(hWnd)) { StringBuilder sb = new StringBuilder(1024); GetWindowText(hWnd, sb, sb.Capacity); if (sb.ToString().Contains(windowTitle)) { handles.Add(hWnd); } } return true; }, IntPtr.Zero); foreach (IntPtr handle in handles) { SetForegroundWindow(handle); } } ``` 在这个示例代码中,EnumWindows 函数用于枚举所有可见窗口,GetWindowText 函数用于获取窗口标题,SetForegroundWindow 函数用于将窗口设置为前台窗口SetForegroundWindows 函数用于设置多个窗口为前台窗口,其参数为窗口标题。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值