【WPF无边框窗体】

WPF是Windows操作系统中一次重大变革,与早期的GDI+/GDI不同。

WPF是基于DirectX引擎的,支持GPU硬件加速,在不支持硬件加速时也可以使用软件绘制。

利用WPF创建无边框的窗体:

第一种方式:设置WindowStyle="None" AllowsTransparency="True"

<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>
第二种方式:在窗体加载时调用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" 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要搞一些。建议使用第二种方法进行处理。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值