源码
工程设置
所需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有用
隐藏系统自带标题栏后,有自定义标题栏,拖动自定义标题栏,移动窗口
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();
}
}
}
开机自启动
通过写注册表实现
public static void SetStartWithWindows()
{
RegistryKey reg = Registry.CurrentUser.OpenSubKey(RegistRun, true);
if (reg == null)
{
reg = Registry.CurrentUser.CreateSubKey(RegistRun);
}
reg.SetValue(RegistWin32ApiExe, GetExePath);
}