我們可以通過使用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 <StackPanel Background="Red" Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"> 2 <Button x:Name="edge" >停靠邊緣</Button> 3 <Button x:Name="detach">取消停靠</Button> 4 <Button x:Name="close">關閉應用</Button> 5 </StackPanel>Xaml
1 public sealed partial class MainWindow : Window 2 { 3 public MainWindow() 4 { 5 this.InitializeComponent(); 6 Init(); 7 } 8 9 void Init() 10 { 11 edge.Click += (s, e) => Berth(this); 12 detach.Click += (s, e) => Detach(this); 13 close.Click += (s, e) => 14 { 15 App.Current.Exit(); 16 }; 17 //監聽視窗關閉事件 18 this.Closed += (s, e) => 19 { 20 //取消停靠 21 Detach(this); 22 }; 23 } 24 25 /// <summary> 26 /// 將視窗停靠到邊緣 27 /// </summary> 28 void Berth(Window window, int width = 400) 29 { 30 //獲取視窗句柄 31 var hwnd = WindowNative.GetWindowHandle(window); 32 //創建應用欄資訊物件,并設定其大小和位置 33 var data = https://www.cnblogs.com/chifan/archive/2022/09/30/new APPBARDATA(); 34 data.hWnd = hwnd; 35 data.uEdge = ABE.ABE_RIGHT;//設定方向 36 data.cbSize = (uint)Marshal.SizeOf(data); 37 data.rc.Top = 0; 38 data.rc.bottom = DisplayArea.Primary.OuterBounds.Height; 39 data.rc.left = DisplayArea.Primary.OuterBounds.Width - width; 40 data.rc.right = DisplayArea.Primary.OuterBounds.Width; 41 //呼叫 win32Api 設定指定位置為“AppBar” 42 SHAppBarMessage(ABM.ABM_NEW, ref data); 43 SHAppBarMessage(ABM.ABM_SETPOS, ref data); 44 45 //使用 AppWindow 類將視窗設定為選單模式(將標題欄去掉,并且設定為不可更改大小); 46 var wid = Win32Interop.GetWindowIdFromWindow(hwnd); 47 var op = OverlappedPresenter.CreateForContextMenu(); 48 var appWindow = AppWindow.GetFromWindowId(wid); 49 appWindow.SetPresenter(op);//將視窗設定為選單模式 50 51 //使用win32Api 的SetWindowLong 函式將任務欄里面的應用圖示去掉 52 var style = (User32.SetWindowLongFlags)User32.GetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE); 53 style |= User32.SetWindowLongFlags.WS_EX_TOOLWINDOW; 54 User32.SetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE, style); 55 //使用 win32Api MoveWindow 函式 更改視窗的大小和位置 56 User32.MoveWindow(hwnd, data.rc.Left, data.rc.top, width, data.rc.Height, true); 57 } 58 59 /// <summary> 60 /// 從邊緣中取消停靠視窗 61 /// </summary> 62 void Detach(Window window) 63 { 64 //獲取視窗句柄 65 var hwnd = WindowNative.GetWindowHandle(window); 66 var data = https://www.cnblogs.com/chifan/archive/2022/09/30/new APPBARDATA(); 67 data.hWnd = hwnd; 68 data.cbSize = (uint)Marshal.SizeOf(data); 69 var d = SHAppBarMessage(ABM.ABM_REMOVE, ref data); 70 71 //將視窗的模式設定為普通的模式,將標題欄顯示出來 72 OverlappedPresenter op = OverlappedPresenter.Create(); 73 var wid = Win32Interop.GetWindowIdFromWindow(hwnd); 74 var aw = AppWindow.GetFromWindowId(wid); 75 aw.SetPresenter(op); 76 //在任務欄上顯示圖示 77 var style = User32.SetWindowLongFlags.WS_VISIBLE; 78 User32.SetWindowLong(hwnd, User32.WindowLongIndexFlags.GWL_EXSTYLE, style); 79 //設定視窗大小和位置 80 User32.MoveWindow(hwnd, 20, 200, 400, 400, true); 81 } 82 }后臺代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/510639.html
標籤:.NET技术
上一篇:.Net下的分布式唯一ID
