WinUI3 使用Win32Api 实现窗口停靠常驻桌面功能。

瑞星  金牌会员 | 2022-9-30 04:04:43 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 873|帖子 873|积分 2619

  我们可以通过使用Win32Api来制作一些强大的功能,本文将通过示例代码来介绍使用Win32Api来之做桌面窗口停靠功能;
  效果图:
 
 
   一.通过Nuget 引入 Vanara.PInvoke.Shell32 和 PInvoke.User32 这两个库。

 
 
 
 
 
  二.功能列表
    1.Berth 函数,将窗口停靠在桌面的右侧;
      1).使用 Shell32.SHAppBarMessage 函数的两次调用将桌面的指定位置设置为"AppBar"区域;
      2).使用AppWindow将窗口的模式设置为菜单模式(该模式会将窗口的标题栏移除,并且禁用了用户更改窗口大小的功能);
      3).使用 User32.SetWindowLong函数将任务栏里面的应用图标隐藏
      4).使用User32.MoveWindow函数设置指定的大小,并且将窗口移动到指定的位置。
    2.Detach 函数,将窗口取消停靠;
      1).使用Shell32.SHAppBarMessage 函数移除 “AppBar” ,将桌面恢复正常;
      2).使用AppWindow 将窗口设置为普通模式(将原本隐藏的标题栏显示出来,已经更改为可以更改窗口大小);
      3).使用 User32.SetWindowLong函数将原本被移除的图标显示出来;
      4).使用User32.MoveWindow函数设置指定的大小,并且将窗口移动到指定的位置。
  三.所有代码
  1. 1 <StackPanel Background="Red" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
  2. 2         <Button x:Name="edge" >停靠边缘</Button>
  3. 3         <Button x:Name="detach">取消停靠</Button>
  4. 4         <Button x:Name="close">关闭应用</Button>
  5. 5     </StackPanel>
复制代码
Xaml
  1. 1 public sealed partial class MainWindow : Window
  2. 2     {
  3. 3         public MainWindow()
  4. 4         {
  5. 5             this.InitializeComponent();
  6. 6             Init();
  7. 7         }
  8. 8
  9. 9         void Init()
  10. 10         {
  11. 11             edge.Click += (s, e) => Berth(this);
  12. 12             detach.Click += (s, e) => Detach(this);
  13. 13             close.Click += (s, e) =>
  14. 14             {
  15. 15                 App.Current.Exit();
  16. 16             };
  17. 17             //监听窗口关闭事件
  18. 18             this.Closed += (s, e) =>
  19. 19             {
  20. 20                 //取消停靠
  21. 21                 Detach(this);
  22. 22             };
  23. 23         }
  24. 24
  25. 25         /// <summary>
  26. 26         /// 将窗口停靠到边缘
  27. 27         /// </summary>
  28. 28         void Berth(Window window, int width = 400)
  29. 29         {
  30. 30             //获取窗口句柄
  31. 31             var hwnd = WindowNative.GetWindowHandle(window);
  32. 32             //创建应用栏信息对象,并设置其大小和位置
  33. 33             var data = new APPBARDATA();
  34. 34             data.hWnd = hwnd;
  35. 35             data.uEdge = ABE.ABE_RIGHT;//设置方向
  36. 36             data.cbSize = (uint)Marshal.SizeOf(data);
  37. 37             data.rc.Top = 0;
  38. 38             data.rc.bottom = DisplayArea.Primary.OuterBounds.Height;
  39. 39             data.rc.left = DisplayArea.Primary.OuterBounds.Width - width;
  40. 40             data.rc.right = DisplayArea.Primary.OuterBounds.Width;
  41. 41             //调用 win32Api 设定指定位置为“AppBar”
  42. 42             SHAppBarMessage(ABM.ABM_NEW, ref data);
  43. 43             SHAppBarMessage(ABM.ABM_SETPOS, ref data);
  44. 44
  45. 45             //使用 AppWindow 类将窗口设置为菜单模式(将标题栏去掉,并且设置为不可更改大小);
  46. 46             var wid = Win32Interop.GetWindowIdFromWindow(hwnd);
  47. 47             var op = OverlappedPresenter.CreateForContextMenu();
  48. 48             var appWindow = AppWindow.GetFromWindowId(wid);
  49. 49             appWindow.SetPresenter(op);//将窗口设置为菜单模式
  50. 50
  51. 51             //使用win32Api 的SetWindowLong 函数将任务栏里面的应用图标去掉
  52. 52             var style = (User32.SetWindowLongFlags)User32.GetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE);
  53. 53             style |= User32.SetWindowLongFlags.WS_EX_TOOLWINDOW;
  54. 54             User32.SetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE, style);
  55. 55             //使用 win32Api MoveWindow 函数 更改窗口的大小和位置
  56. 56             User32.MoveWindow(hwnd, data.rc.Left, data.rc.top, width, data.rc.Height, true);
  57. 57         }
  58. 58
  59. 59         /// <summary>
  60. 60         /// 从边缘中取消停靠窗口
  61. 61         /// </summary>
  62. 62         void Detach(Window window)
  63. 63         {
  64. 64             //获取窗口句柄
  65. 65             var hwnd = WindowNative.GetWindowHandle(window);
  66. 66             var data = new APPBARDATA();
  67. 67             data.hWnd = hwnd;
  68. 68             data.cbSize = (uint)Marshal.SizeOf(data);
  69. 69             var d = SHAppBarMessage(ABM.ABM_REMOVE, ref data);
  70. 70         
  71. 71             //将窗口的模式设置为普通的模式,将标题栏显示出来
  72. 72             OverlappedPresenter op = OverlappedPresenter.Create();
  73. 73             var wid = Win32Interop.GetWindowIdFromWindow(hwnd);
  74. 74             var aw = AppWindow.GetFromWindowId(wid);
  75. 75             aw.SetPresenter(op);
  76. 76             //在任务栏上显示图标
  77. 77             var style = User32.SetWindowLongFlags.WS_VISIBLE;
  78. 78             User32.SetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE, style);
  79. 79             //设置窗口大小和位置
  80. 80             User32.MoveWindow(hwnd, 20, 200, 400, 400, true);
  81. 81         }
  82. 82     }
复制代码
后台代码 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

瑞星

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表