Unity中WinForm窗口闪烁或任务栏图标闪烁的代码

最近在做Unity PC平台下特性功能调研,比如PC扫码登录和WinForm窗口闪烁。说到平台特性我们可以吧Unity看做一个可控制的视频,在Android上他是一个Activity,在iOS下他是一个view,而在PC下他就是WinForm所以对于有PC开发经验的人来说一些特性做起来就非常容易。
今天笔者分享下WinForm窗口闪烁例子如下:
WinForm窗口闪烁示例
代码的作用找到Unity主窗口句柄,设置起闪烁
需要注意的一点是要让Unity后台运行
设置方法在这里
请将Build Setting->Player Setting->Resolution and Pressentation -> Run In Background 勾选上
废话少说上代码

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using UnityEngine;

public class FlashWinTool{
    public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
    [DllImport("user32.dll")] //闪烁窗体
    public static extern bool FlashWindowEx(ref FLASHWINFO pwfi);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);

    [DllImport("user32.dll", SetLastError = true)]
    public static extern IntPtr GetParent(IntPtr hWnd);

    [DllImport("user32.dll")]
    public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);

    [DllImport("kernel32.dll")]
    public static extern void SetLastError(uint dwErrCode);

    [StructLayout(LayoutKind.Sequential)]
    public struct FLASHWINFO
    {
        public UInt32 cbSize;
        public IntPtr hwnd;
        public UInt32 dwFlags;
        public UInt32 uCount;
        public UInt32 dwTimeout;
    }

    //闪烁窗体参数
    public const UInt32 FLASHW_STOP = 0;//停止闪动.系统将窗体恢复到初始状态.
    public const UInt32 FLASHW_CAPTION = 1;//闪动窗体的标题.
    public const UInt32 FLASHW_TRAY = 2;//闪动任务栏按钮
    public const UInt32 FLASHW_ALL = 3;//闪动窗体标题和任务栏按钮
    public const UInt32 FLASHW_TIMER = 4;//连续不停的闪动,直到此参数被设置为:FLASHW_STOP
    public const UInt32 FLASHW_TIMERNOFG = 12;//连续不停的闪动,直到窗体用户被激活.通常用法将参数设置为: FLASHW_ALL | FLASHW_TIMERNOFG

    /// <summary>
    /// 窗口闪烁
    /// </summary>
    /// <param name="handle">窗口句柄</param>
    public static void FlashWindow(IntPtr handle)
    {
        FLASHWINFO fInfo = new FLASHWINFO();

        fInfo.cbSize = Convert.ToUInt32(System.Runtime.InteropServices.Marshal.SizeOf(fInfo));
        fInfo.hwnd = handle;
        fInfo.dwFlags = FLASHW_ALL | FLASHW_TIMERNOFG;//这里是闪动窗标题和任务栏按钮,直到用户激活窗体
        fInfo.uCount = UInt32.MaxValue;
        fInfo.dwTimeout = 0;

        FlashWindowEx(ref fInfo);
    }

    /// <summary>
    /// 获取窗口句柄
    /// </summary>
    /// <returns>窗口句柄</returns>
    public static IntPtr GetProcessWnd()
    {
        IntPtr ptrWnd = IntPtr.Zero;
        uint pid = (uint)Process.GetCurrentProcess().Id;  // 当前进程 ID  

        bool bResult = EnumWindows(new WNDENUMPROC(delegate (IntPtr hwnd, uint lParam)
        {
            uint id = 0;

            if (GetParent(hwnd) == IntPtr.Zero)
            {
                GetWindowThreadProcessId(hwnd, ref id);
                if (id == lParam)    // 找到进程对应的主窗口句柄  
                {
                    ptrWnd = hwnd;   // 把句柄缓存起来  
                    SetLastError(0);    // 设置无错误  
                    return false;   // 返回 false 以终止枚举窗口  
                }
            }

            return true;

        }), pid);

        return (!bResult && Marshal.GetLastWin32Error() == 0) ? ptrWnd : IntPtr.Zero;
    }
}

工程源码
http://download.csdn.net/download/tj134679258/10228073

猜你喜欢

转载自blog.csdn.net/tj134679258/article/details/79186978