tft每日頭條

 > 生活

 > win32對話框教程

win32對話框教程

生活 更新时间:2024-09-11 17:17:48

 我們可以通過使用win32Api來制作一些強大的功能,本文将通過示例代碼來介紹使用Win32Api來之做桌面窗口停靠功能;

  效果圖:

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)1

  一.通過Nuget 引入 Vanara.PInvoke.Shell32 和 PInvoke.User32 這兩個庫。

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)2

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)3

  二.功能列表

    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函數設置指定的大小,并且将窗口移動到指定的位置。

  三.所有代碼

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)4

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>

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)5

win32對話框教程(WinUI3使用Win32Api實現窗口停靠常駐桌面的功能)6

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 = 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 = 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 }

,

更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!

查看全部

相关生活资讯推荐

热门生活资讯推荐

网友关注

Copyright 2023-2024 - www.tftnews.com All Rights Reserved