unity3d:win32api,托盘运行,开机自启动,浏览文件对话框,无标题栏,自定义标题栏拖动

unity3d:win32api,托盘运行,开机自启动,浏览文件对话框,无标题栏,自定义标题栏拖动_第1张图片

源码

csdn下载
github

工程设置

在这里插入图片描述

所需dll

在这里插入图片描述

托盘运行

隐藏窗口,显示托盘图标,点击托盘图标恢复窗口
在这里插入图片描述
在这里插入图片描述

  public static void Hide()//最小化到托盘
    {
        try
        {
            if (File.Exists(Application.streamingAssetsPath + "/icon.png"))
            {
   
                window = GetForegroundWindow();
                ShowWindow(window, SW_HIDE);
                //_notifyIcon.BalloonTipText = "Heroage";//托盘气泡显示内容

                _notifyIcon.Text = "托盘悬浮提示";//鼠标悬浮时显示的内容

                _notifyIcon.Visible = true;//托盘按钮是否可见
                _notifyIcon.Icon = CustomTrayIcon(Application.streamingAssetsPath + "/icon.png", _width, _height);//托盘图标
                //_notifyIcon.ShowBalloonTip(2000);//托盘气泡显示时间

                System.Windows.Forms.MenuItem closeMenu = new System.Windows.Forms.MenuItem("关闭");
                System.Windows.Forms.MenuItem[] childen = new System.Windows.Forms.MenuItem[] { closeMenu };
                _notifyIcon.ContextMenu = new System.Windows.Forms.ContextMenu(childen);

                closeMenu.Click += OnMenuClose;
                
                _notifyIcon.MouseClick += notifyIcon_MouseClick;//双击托盘图标响应事件
     
            }
        }
        catch (Exception e)
        {
            //Debug.Log(e.ToString());
            MsgBoxBase.Show(e.ToString(), "异常", WinForms.MessageBoxButtons.OKCancel);
        }
    }

隐藏标题栏

exe安装打包,打包的时候给exe加上运行参数xx.exe -popupwindow
-popupwindow 只针对unity构建出来的exe有用
unity3d:win32api,托盘运行,开机自启动,浏览文件对话框,无标题栏,自定义标题栏拖动_第2张图片

隐藏系统自带标题栏后,有自定义标题栏,拖动自定义标题栏,移动窗口
UGUI

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static PInvoke;
[RequireComponent(typeof(Graphic))]
public class WindowMoveHandler : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerExitHandler
{
    static bool isDrag = false;
    void IPointerDownHandler.OnPointerDown(PointerEventData eventData) => isDrag = eventData.pointerId == -1;
    void IPointerExitHandler.OnPointerExit(PointerEventData eventData) => isDrag = false;
    void IPointerUpHandler.OnPointerUp(PointerEventData eventData) => isDrag = !(eventData.pointerId == -1);
    private void Update()
    {
        if (!Application.isEditor && isDrag)
        {
            DragWindow();
        }
    }
}

NGUI

using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static PInvoke;

public class WindowMoveHandler : MonoBehaviour
{
    static bool isDrag = false;
    public GameObject m_obj;
    private void Update()
    {
        if (Input.GetMouseButton(0) && UICamera.LastRayHitObject == m_obj)
        {
            Debug.Log("MoveWindows");
            DragWindow();
        }
    }
}

开机自启动

通过写注册表实现
unity3d:win32api,托盘运行,开机自启动,浏览文件对话框,无标题栏,自定义标题栏拖动_第3张图片

    public static void SetStartWithWindows()
    {
        RegistryKey reg = Registry.CurrentUser.OpenSubKey(RegistRun, true);
        if (reg == null)
        {
            reg = Registry.CurrentUser.CreateSubKey(RegistRun);
        }

        reg.SetValue(RegistWin32ApiExe, GetExePath);

    }

你可能感兴趣的:(Unity3d技术笔记,unity3d,win32api,托盘运行,开机自启动,自定义标题栏拖动)