ExternExeHelper.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. namespace HistoryView.Helper;
  2. public static class SetWindow
  3. {
  4. private static IntPtr intPtr; //第三方应用窗口的句柄
  5. public static void ResizeWindow()
  6. {
  7. _ = ShowWindow(intPtr, 0); //先将窗口隐藏
  8. _ = ShowWindow(intPtr, 3); //再将窗口最大化,可以让第三方窗口自适应容器的大小
  9. }
  10. public static bool FindWindow(string formName)
  11. {
  12. intPtr = FindWindow(null!, formName);
  13. return intPtr != IntPtr.Zero;
  14. }
  15. public static void Clear()
  16. {
  17. _ = ShowWindow(intPtr, 0);
  18. }
  19. public static void ExitParent()
  20. {
  21. _ = ShowWindow(intPtr, 0);
  22. SetParent(IntPtr.Zero);
  23. _ = ShowWindow(intPtr, 9);
  24. }
  25. public static void SetParent(IntPtr hWndNewParent)
  26. {
  27. _ = ShowWindow(intPtr, 0); //先将窗体隐藏,防止出现闪烁
  28. _ = SetParent(intPtr, hWndNewParent); //将第三方窗体嵌入父容器
  29. Thread.Sleep(100); //略加延时
  30. _ = ShowWindow(intPtr, 3); //让第三方窗体在容器中最大化显示
  31. //RemoveWindowTitle(intPtr); // 去除窗体标题
  32. ResizeWindow();
  33. }
  34. //private static void RemoveWindowTitle(IntPtr vHandle)
  35. //{
  36. // long style = GetWindowLong(vHandle, -16);
  37. // style &= ~0x00C00000;
  38. // SetWindowLong(vHandle, -16, style);
  39. //}
  40. [DllImport("user32.dll ", EntryPoint = "SetParent")]
  41. private static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent); //将外部窗体嵌入程序
  42. [DllImport("user32.dll")]
  43. public static extern IntPtr FindWindow(string lpszClass, string lpszWindow); //按照窗体类名或窗体标题查找窗体
  44. [DllImport("user32.dll", EntryPoint = "ShowWindow", CharSet = CharSet.Auto)]
  45. private static extern int ShowWindow(IntPtr hwnd, int nCmdShow); //设置窗体属性
  46. [DllImport("user32.dll", EntryPoint = "SetWindowLong", CharSet = CharSet.Auto)]
  47. public static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, long dwNewLong);
  48. [DllImport("user32.dll", EntryPoint = "GetWindowLong", CharSet = CharSet.Auto)]
  49. public static extern long GetWindowLong(IntPtr hWnd, int nIndex);
  50. }