unity 打包后程序能拓展并能一直显示在屏幕最前端

最近做项目碰到这个问题 是通过WIN获取句柄来实现的,是要把程序设置成窗口模式 然后把边框去除 找到程序名称句柄把他显示在最前端

 [DllImport("User32.dll")]
    private static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
    [DllImport("user32.dll")]
    private static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
    [DllImport("user32.dll")]
    private static extern IntPtr GetForegroundWindow();
    [DllImport("User32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Auto)]
    private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
    [System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "SetForegroundWindow")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);//设置此窗体为活动窗体

   
    private const uint SWP_SHOWWINDOW = 0x0040;
    private const int GWL_STYLE = -16;
    private const int WS_BORDER = 1;

    private IntPtr handler;
    int m_Wight;
    int m_Height;
      private void Start()
    {
#if !UNITY_EDITOR     
        string name = Application.productName;
        handler = FindWindow(null, name);
        if (handler == IntPtr.Zero){
            handler = GetForegroundWindow();
        }
        SetForegroundWindow(handler);
        SetWindowLong(handler, GWL_STYLE, WS_BORDER);
      //  CreateDirectory(Application.streamingAssetsPath + folder);
     
        bool result = SetWindowPos(handler, -1, 0, 0, m_Wight, m_Height, SWP_SHOWWINDOW);
#endif
    }

有几点注意事项
1.因为是多屏幕 所以代码里不能有 Screen.SetResolution(this.m_Wight, this.m_Height, true);这样程会在主屏幕(一个屏)中全屏显示
2.使用 Screen.SetResolution(this.m_Wight, this.m_Height, false)后 边框不能去除(我也不知道原因)
所以全局查找一遍删除全部的Screen.SetResolution();

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值