【WPF無邊框窗體】
阿新 • • 發佈:2019-01-03
WPF是Windows作業系統中一次重大變革,與早期的GDI+/GDI不同。
WPF是基於DirectX引擎的,支援GPU硬體加速,在不支援硬體加速時也可以使用軟體繪製。
利用WPF建立無邊框的窗體:
第一種方式:設定WindowStyle="None" AllowsTransparency="True"
第二種方式:在窗體載入時呼叫WIndowAPI建立無邊框窗體<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="146" Width="637" WindowStyle="None" AllowsTransparency="True"><!----> <Grid> </Grid> </Window>
<Window x:Class="WpfApplication1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="146" Width="637" Loaded="Window_Loaded" ><!----> <Grid> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication1 { /// <summary> /// MainWindow.xaml 的互動邏輯 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { IntPtr hwnd = new WindowInteropHelper(this).Handle; NativeMethods.SetWindowNoBorder(hwnd); } } public class NativeMethods { /// <summary> /// 帶有外邊框和標題的windows的樣式 /// </summary> public const long WS_CAPTION = 0x00C00000L; public const long WS_CAPTION_2 = 0X00C0000L; // public const long WS_BORDER = 0X0080000L; /// <summary> /// window 擴充套件樣式 分層顯示 /// </summary> public const long WS_EX_LAYERED = 0x00080000L; public const long WS_CHILD = 0x40000000L; /// <summary> /// 帶有alpha的樣式 /// </summary> public const long LWA_ALPHA = 0x00000002L; /// <summary> /// 顏色設定 /// </summary> public const long LWA_COLORKEY = 0x00000001L; /// <summary> /// window的基本樣式 /// </summary> public const int GWL_STYLE = -16; /// <summary> /// window的擴充套件樣式 /// </summary> public const int GWL_EXSTYLE = -20; /// <summary> /// 設定窗體的樣式 /// </summary> /// <param name="handle">操作窗體的控制代碼</param> /// <param name="oldStyle">進行設定窗體的樣式型別.</param> /// <param name="newStyle">新樣式</param> [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern void SetWindowLong(IntPtr handle, int oldStyle, long newStyle); /// <summary> /// 獲取窗體指定的樣式. /// </summary> /// <param name="handle">操作窗體的控制代碼</param> /// <param name="style">要進行返回的樣式</param> /// <returns>當前window的樣式</returns> [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern long GetWindowLong(IntPtr handle, int style); /// <summary> /// 設定窗體的工作區域. /// </summary> /// <param name="handle">操作窗體的控制代碼.</param> /// <param name="handleRegion">操作窗體區域的控制代碼.</param> /// <param name="regraw">if set to <c>true</c> [regraw].</param> /// <returns>返回值</returns> [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern int SetWindowRgn(IntPtr handle, IntPtr handleRegion, bool regraw); /// <summary> /// 建立帶有圓角的區域. /// </summary> /// <param name="x1">左上角座標的X值.</param> /// <param name="y1">左上角座標的Y值.</param> /// <param name="x2">右下角座標的X值.</param> /// <param name="y2">右下角座標的Y值.</param> /// <param name="width">圓角橢圓的width.</param> /// <param name="height">圓角橢圓的height.</param> /// <returns>hRgn的控制代碼</returns> [System.Runtime.InteropServices.DllImport("gdi32.dll")] public static extern IntPtr CreateRoundRectRgn(int x1, int y1, int x2, int y2, int width, int height); /// <summary> /// Sets the layered window attributes. /// </summary> /// <param name="handle">要進行操作的視窗控制代碼</param> /// <param name="colorKey">RGB的值</param> /// <param name="alpha">Alpha的值,透明度</param> /// <param name="flags">附帶引數</param> /// <returns>true or false</returns> [System.Runtime.InteropServices.DllImport("User32.dll")] public static extern bool SetLayeredWindowAttributes(IntPtr handle, ulong colorKey, byte alpha, long flags); //================================================================================= /// <summary> /// 設定窗體為無邊框風格 /// </summary> /// <param name="hWnd"></param> public static void SetWindowNoBorder(IntPtr hWnd) { long oldstyle = NativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE); SetWindowLong(hWnd, GWL_STYLE, oldstyle & (~(WS_CAPTION | WS_CAPTION_2))); } } }
使用第一中方式在處理影象渲染是所佔用的記憶體和CPU要搞一些。建議使用第二種方法進行處理。