当前位置:   article > 正文

Unity打包EXE自定义(拖拽)窗口大小_unity pc exe 等比例修改窗口大小

unity pc exe 等比例修改窗口大小

代码

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System;
  5. using System.Runtime.InteropServices;
  6. public class MyWindow : MonoBehaviour
  7. {
  8. [DllImport("user32.dll")]
  9. private static extern IntPtr GetActiveWindow();
  10. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  11. public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);
  12. [DllImport("user32.dll")]
  13. public static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
  14. [StructLayout(LayoutKind.Sequential)]
  15. public struct RECT
  16. {
  17. public int Left; //最左坐标
  18. public int Top; //最上坐标
  19. public int Right; //最右坐标
  20. public int Bottom; //最下坐标
  21. }
  22. [DllImport("user32.dll")]
  23. public static extern bool MoveWindow(IntPtr hWnd, int x, int y, int width, int height, bool repaint);
  24. //***********************
  25. IntPtr myintptr;
  26. RECT rect;
  27. float w_h;
  28. int w;
  29. int h;
  30. int x;
  31. int y;
  32. void Start()
  33. {
  34. myintptr = GetActiveWindow();
  35. w_h = 9f / 16f; //窗口横纵比例
  36. GetWindowRect(myintptr, ref rect);
  37. w = rect.Right - rect.Left; //窗口的宽度
  38. h = rect.Bottom - rect.Top; //窗口的高度
  39. }
  40. void LateUpdate()
  41. {
  42. SetWindow();
  43. }
  44. void SetWindow()
  45. {
  46. GetWindowRect(myintptr, ref rect);
  47. w = rect.Right - rect.Left; //窗口的宽度
  48. h = rect.Bottom - rect.Top; //窗口的高度
  49. x = rect.Left;
  50. y = rect.Top;
  51. float z = w / h;
  52. if (z > w_h + 0.01f || z < w_h - 0.01f)
  53. {
  54. h = (int)(w / w_h);
  55. MoveWindow(myintptr, x, y, w, h, true);
  56. }
  57. }
  58. }

设置

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/100080
推荐阅读
相关标签
  

闽ICP备14008679号