ITeye博客

Unity3d 去掉exe版本的边框

wgcode · 2015-05-13 14:38 · Unity · 移动开发 · Markdown

[csharp] view plaincopy

 

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System;
  5. public class Test : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 窗口宽度
  9. /// </summary>
  10. public int winWidth;
  11. /// <summary>
  12. /// 窗口高度
  13. /// </summary>
  14. public int winHeight;
  15. /// <summary>
  16. /// 窗口左上角x
  17. /// </summary>
  18. public int winPosX;
  19. /// <summary>
  20. /// 窗口左上角y
  21. /// </summary>
  22. public int winPosY;
  23. [DllImport("user32.dll")]
  24. static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
  25. [DllImport("user32.dll")]
  26. static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  27. [DllImport("user32.dll")]
  28. static extern IntPtr GetForegroundWindow();
  29. const uint SWP_SHOWWINDOW = 0x0040;
  30. const int GWL_STYLE = -16;
  31. const int WS_BORDER = 1;
  32. const int WS_POPUP = 0x800000;
  33. // Use this for initialization
  34. void Start()
  35. {
  36. winWidth = 610;
  37. winHeight = 350;
  38. //显示器支持的所有分辨率
  39. int i = Screen.resolutions.Length;
  40. int resWidth = Screen.resolutions[i - 1].width;
  41. int resHeight = Screen.resolutions[i - 1].height;
  42. winPosX = resWidth / 2 - winWidth / 2;
  43. winPosY = resHeight / 2 - winHeight / 2;
  44. SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
  45. bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);
  46. }
  47. }

原文地址点击这里

 

测试发现GUI坐标偏差,修改如下

 

[csharp] view plaincopy在CODE上查看代码片派生到我的代码片

 

  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Runtime.InteropServices;
  4. using System;
  5. public class Test : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// 窗口宽度
  9. /// </summary>
  10. public int winWidth;
  11. /// <summary>
  12. /// 窗口高度
  13. /// </summary>
  14. public int winHeight;
  15. /// <summary>
  16. /// 窗口左上角x
  17. /// </summary>
  18. public int winPosX;
  19. /// <summary>
  20. /// 窗口左上角y
  21. /// </summary>
  22. public int winPosY;
  23. [DllImport("user32.dll")]
  24. static extern IntPtr SetWindowLong(IntPtr hwnd, int _nIndex, int dwNewLong);
  25. [DllImport("user32.dll")]
  26. static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  27. [DllImport("user32.dll")]
  28. static extern IntPtr GetForegroundWindow();
  29. [DllImport("User32.dll", EntryPoint = "GetSystemMetrics")]
  30. public static extern IntPtr GetSystemMetrics(int nIndex);
  31. const int SM_CXSCREEN = 0x00000000;
  32. const int SM_CYSCREEN = 0x00000001;
  33. const uint SWP_SHOWWINDOW = 0x0040;
  34. const int GWL_STYLE = -16;
  35. const int WS_BORDER = 1;
  36. const int WS_POPUP = 0x800000;
  37. public GUIText t;
  38. // Use this for initialization
  39. void Start()
  40. {
  41. winWidth = 610;
  42. winHeight = 350;
  43. //当前屏幕分辨率
  44. int resWidth = (int)GetSystemMetrics(SM_CXSCREEN);
  45. int resHeight = (int)GetSystemMetrics(SM_CYSCREEN);
  46. winPosX = resWidth / 2 - winWidth / 2;
  47. winPosY = resHeight / 2 - winHeight / 2 -1;
  48. SetWindowLong(GetForegroundWindow(), GWL_STYLE, WS_POPUP);
  49. //测试发现左下角坐标为(0,1),修改如下
  50. bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);
  51. //bool result = SetWindowPos(GetForegroundWindow(), 0, winPosX, winPosY, winWidth, winHeight, SWP_SHOWWINDOW);
  52. }
  53. void OnGUI()
  54. {
  55. if (Input.GetMouseButtonDown(0))
  56. {
  57. t.text = "X:"+Input.mousePosition.x + " Y:" + Input.mousePosition.y;
  58. }
  59. }
  60. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。