Win10下Unity程序显示托盘图标

using UnityEngine;
using System.Windows.Forms;//该命名空间需要导入System.Windows.Forms.dll
                           //可在路径:Unity安装目录/Editor\Data\Mono\lib\mono\2.0里找到
using System.Drawing;//该命名空间需要导入System.Drawing.dll
                     //可在路径:Unity安装目录/Editor\Data\Mono\lib\mono\2.0里找到
using System;
using System.Runtime.InteropServices;

public class Tray
{
    //NotifyIcon 设置托盘相关参数
    private static NotifyIcon _notifyIcon = new NotifyIcon();
    //托盘图标的宽高
    private static int _width = 40, _height = 40;
    //做托盘图标的图片,这里用了.png格式
    private static Texture2D iconTexture2D;
    private static IntPtr currentWindowPtr;

    //调用该方法将运行程序显示到托盘
    public static void InitTray()
    {
        currentWindowPtr = WindowsForm.GetForegroundWindow();//记住当前窗口句柄
        _notifyIcon.BalloonTipText = "我的托盘";//托盘气泡显示内容
        _notifyIcon.Text = "这是托盘提示信息";
        _notifyIcon.Icon = new System.Drawing.Icon(SystemIcons.Warning, 40, 40);//托盘图标
        //_notifyIcon.Icon = CustomTrayIcon(@UnityEngine.Application.streamingAssetsPath + "/icon.png", _width, _height);
        _notifyIcon.ShowBalloonTip(2000);//托盘气泡显示时间
        _notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;//双击托盘图标响应事件
        //ShowTray();
        HideTray();
    }
    /// <summary>
    /// 设置程序托盘图标
    /// </summary>
    /// <param name="iconPath">图标路径</param>
    /// <param name="width">宽</param>
    /// <param name="height">高</param>
    /// <returns>图标</returns>
    private static Icon CustomTrayIcon(string iconPath, int width, int height)
    {
        Bitmap bt = new Bitmap(iconPath);
        Bitmap fitSizeBt = new Bitmap(bt, width, height);
        return Icon.FromHandle(fitSizeBt.GetHicon());
    }
    private static Icon CustomTrayIcon(System.Drawing.Image img, int width, int height)
    {
        Bitmap bt = new Bitmap(img);
        Bitmap fitSizeBt = new Bitmap(bt, width, height);
        return Icon.FromHandle(fitSizeBt.GetHicon());
    }
    /// <summary>
    /// byte[]转换成Image
    /// </summary>
    /// <param name="byteArrayIn">二进制图片流</param>
    /// <returns>Image</returns>
    public static System.Drawing.Image ByteArrayToImage(byte[] byteArrayIn)
    {
        if (byteArrayIn == null)
            return null;
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArrayIn))
        {
            System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
            ms.Flush();
            return returnImage;
        }
    }



    public static void DestroyDoubleClick()
    {
        _notifyIcon.MouseDoubleClick -= NotifyIcon_MouseDoubleClick;
    }

    /// <summary>
    /// 显示托盘图标
    /// </summary>
    public static void ShowTray()
    {
        _notifyIcon.Visible = true;//托盘按钮是否可见
    }
    /// <summary>
    /// 隐藏托盘图标
    /// </summary>
    public static void HideTray()
    {
        _notifyIcon.Visible = false;//托盘按钮是否可见
    }

    /// <summary>
    /// 双击托盘图标、程序最大化、并 托盘图标隐藏
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private static void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            WindowsForm.OnClickMaximize(currentWindowPtr);
            _notifyIcon.Visible = false;//托盘按钮是否可见
        }
    }
}
public class WindowsForm
{
    [DllImport("user32.dll")]//需要导入该dll
    public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
    [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr GetForegroundWindow();

    const int SW_Hide = 0;//隐藏任务栏图标  
    const int SW_SHOWRESTORE = 1;//还原  
    const int SW_SHOWMINIMIZED = 2; //{最小化, 激活}  
    const int SW_SHOWMAXIMIZED = 3;//最大化  
    const int GWL_STYLE = -16;
    const int WS_POPUP = 0x800000;
    const uint SWP_SHOWWINDOW = 0x0040;

    /// <summary>
    /// 最大化   
    /// </summary>
    public static void OnClickMaximize(IntPtr hwnd)
    {
        ShowWindow(hwnd, SW_SHOWMAXIMIZED);
    }
    /// <summary>
    /// 最小化
    /// </summary>
    /// <param name="hwnd"></param>
    public static void OnClickMinimize(IntPtr hwnd)
    {
        ShowWindow(hwnd, SW_SHOWMINIMIZED);
    }
}


//测试类
    public class TestTray : MonoBehaviour
    {
        /// <summary>
        /// 测试代码  放到继承自Mono的类中 
        /// Q键生成托盘图标
        /// E键显示图标
        /// 图标双击事件为最大化窗体
        /// </summary>
        private void Update()
        {
            if (Input.GetKeyDown(KeyCode.Q))
            {
                Debug.Log("Tray.InitTray();");
                Tray.InitTray();
            }
            else if (Input.GetKeyDown(KeyCode.E))
            {
                Tray.ShowTray();
            }
        }
    }

 

  • 9
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
你可以按照以下步骤安装Unity在Windows 10上: 1. 首先,访问Unity官方网站(https://unity.com/),点击"Get Unity"按钮。 2. 在Unity下载页面上,选择适合您的需求的版本。如果您是个人用户,可以选择免费版Unity Personal。如果您是企业用户或需要更高级功能,可以选择付费版本。 3. 点击下载按钮,选择保存文件的位置,并等待下载完成。 4. 下载完成后,双击安装程序运行安装向导。 5. 在安装向导中,您可以选择安装Unity编辑器、Unity模块和其他附加组件。根据您的需求进行选择,并点击"Next"继续。 6. 在下一个页面上,可以选择安装位置和其他选项。默认选项通常是适合大多数用户的。点击"Next"继续。 7. 在接下来的页面上,您可以选择是否创建桌面快捷方式和开始菜单快捷方式。根据您的喜好进行选择,并点击"Next"继续。 8. 在确认安装页面上,检查您的选择并点击"Install"开始安装。 9. 安装过程可能需要一些时间,请耐心等待。 10. 安装完成后,您可以选择启动Unity编辑器。首次启动时,可能需要一些时间来初始化和设置。 11. 接下来,您可以创建一个Unity帐户或登录现有帐户。这是可选的,但如果您想使用Unity的特定功能(如Unity商店),则需要一个帐户。 12. 完成这些步骤后,您就可以开始使用Unity编辑器进行游戏开发了。 希望以上步骤对您有所帮助!如果您有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值