Program.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // See https://aka.ms/new-console-template for more information
  2. using System.Runtime.InteropServices;
  3. // 常量定义
  4. const int GWL_STYLE = -16;
  5. const int WS_BORDER = 0x00800000; // 边框
  6. const int WS_CAPTION = 0x00C00000; // 标题栏
  7. const int WS_THICKFRAME = 0x00040000; // 可调整大小的框
  8. const int SW_SHOWNORMAL = 1;
  9. if (args.Length < 5)
  10. {
  11. Console.WriteLine("用法: WndTricker <窗口标题> <x坐标> <y坐标> <宽度> <高度> [重试次数] [重试间隔毫秒]");
  12. return -1;
  13. }
  14. // 从命令行参数获取窗口标题、x、y、宽度和高度
  15. string windowTitle = args[0];
  16. if (!int.TryParse(args[1], out int x) ||
  17. !int.TryParse(args[2], out int y) ||
  18. !int.TryParse(args[3], out int width) ||
  19. !int.TryParse(args[4], out int height))
  20. {
  21. Console.WriteLine("参数无效,请确保 x, y, 宽度和高度是整数");
  22. return -2;
  23. }
  24. // 解析可选参数:重试次数和重试间隔
  25. int retries = 0; // 默认不重试
  26. int retryInterval = 1000; // 默认重试间隔 1000 毫秒
  27. if (args.Length >= 6 && !int.TryParse(args[5], out retries))
  28. {
  29. Console.WriteLine("重试次数无效,请确保是整数");
  30. return -3;
  31. }
  32. if (args.Length >= 7 && !int.TryParse(args[6], out retryInterval))
  33. {
  34. Console.WriteLine("重试间隔无效,请确保是整数");
  35. return -4;
  36. }
  37. IntPtr hWnd = IntPtr.Zero;
  38. int attempts = 0;
  39. // 如果需要重试,尝试查找窗口
  40. while (hWnd == IntPtr.Zero && attempts <= retries)
  41. {
  42. hWnd = FindWindow(null, windowTitle);
  43. if (hWnd == IntPtr.Zero && attempts < retries)
  44. {
  45. attempts++;
  46. Console.WriteLine($"标题为 {windowTitle} 的窗口未找到,间隔 {retryInterval} 毫秒后,尝试第 {attempts} 次重试...");
  47. Thread.Sleep(retryInterval);
  48. }
  49. }
  50. if (hWnd == IntPtr.Zero)
  51. {
  52. Console.WriteLine($"标题为 {windowTitle} 的窗口未找到,退出程序");
  53. return -10;
  54. }
  55. // 去掉窗口的边框
  56. var currentStyle = GetWindowLong(hWnd, GWL_STYLE);
  57. SetWindowLong(hWnd, GWL_STYLE, currentStyle & ~(WS_BORDER | WS_CAPTION | WS_THICKFRAME));
  58. // 移动窗口到指定位置并设置大小
  59. SetWindowPos(hWnd, IntPtr.Zero, x, y, width, height, 0);
  60. // 显示窗口
  61. ShowWindow(hWnd, SW_SHOWNORMAL);
  62. Console.WriteLine($"标题为 {windowTitle} 的窗口已去掉边框并移动到 ({x}, {y}),大小已设置为 {width}x{height}");
  63. return 0;
  64. // Windows API 函数声明
  65. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  66. static extern IntPtr FindWindow(string? lpClassName, string lpWindowName);
  67. [DllImport("user32.dll")]
  68. static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
  69. [DllImport("user32.dll")]
  70. static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int width, int height, uint uFlags);
  71. [DllImport("user32.dll")]
  72. static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  73. // 获取窗口样式
  74. [DllImport("user32.dll")]
  75. static extern int GetWindowLong(IntPtr hWnd, int nIndex);