【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体

WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式。
但是AllowTransparency=true后WPF性能会大大降低,动画很卡。无奈只好关闭;
仅设置WindowStyle为None,似乎达不到理想效果,还是有边框:

立刻想到可以通过API修改窗体风格:SetWindowLong
网上查阅资料,发现去掉了WS_CATION = 0x00C00000L 似乎还是没有效果,看到别人的文章,发现WS_CATION = 0x00C0000L,
两个值不一样,于是全部用上,SetWindowLong(hWnd, GWL_STYLE, oldstyle & (~(0x00C00000L |0x00C0000L)));
终于成功了

真正的无边框窗体,因为公司做触屏程序,需要这样的效果。
wziyx513225244的文章中封装的类修改了一下,http://blog.csdn.net/wziyx513225244/article/details/6988494
直接一个函数设置风格就OK了。
相信很多人应该都会遇到这个问题,贴出来供大家用:

复制代码
public class NativeMethods
    {
        /// <summary> 

        /// 带有外边框和标题的windows的样式 

        /// </summary> 
        public const int WS_CAPTION = 0x00C00000;
        public const int WS_CAPTION_2 = 0X00C0000;
        // public const long WS_BORDER = 0X0080000L; 
        /// <summary> 

        /// window 扩展样式 分层显示 

        /// </summary> 
        public const int WS_EX_LAYERED = 0x00080000;
        public const int WS_CHILD = 0x40000000;
        /// <summary> 

        /// 带有alpha的样式 

        /// </summary> 
        public const int LWA_ALPHA = 0x00000002;
        /// <summary> 

        /// 颜色设置 

        /// </summary> 
        public const int LWA_COLORKEY = 0x00000001;
        /// <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, int newStyle);
        /// <summary> 

        /// 获取窗体指定的样式. 

        /// </summary> 

        /// <param name="handle">操作窗体的句柄</param> 

        /// <param name="style">要进行返回的样式</param> 

        /// <returns>当前window的样式</returns> 
        [System.Runtime.InteropServices.DllImport("User32.dll")]
        public static extern int 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, int colorKey, byte alpha, int flags);
        /// <summary>
        /// 设置窗体为无边框风格
        /// </summary>
        /// <param name="hWnd"></param>
        public static void SetWindowNoBorder(IntPtr hWnd)
        {
            //// 获取窗体句柄  
            //IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper(this).Handle;
            int oldstyle = NativeMethods.GetWindowLong(hWnd, NativeMethods.GWL_STYLE);
            SetWindowLong(hWnd, GWL_STYLE, oldstyle & (~(WS_CAPTION | WS_CAPTION_2)));
            //SetWindowLong(hWnd, GWL_EXSTYLE, WS_CHILD);
            // 设置窗体为透明窗体  
            //NativeMethods.SetLayeredWindowAttributes(hWnd, 1 | 2 << 8 | 3 << 16, 0, NativeMethods.LWA_ALPHA);
            //// 创建圆角窗体  12 这个值可以根据自身项目进行设置  
            //NativeMethods.SetWindowRgn(hwnd, NativeMethods.CreateRoundRectRgn(0, 0, Convert.ToInt32(this.ActualWidth), Convert.ToInt32(this.ActualHeight), 12, 12), true);  
        }
    }
复制代码

 

原文地址:http://blog.csdn.net/detecyang/article/details/7946237

 

posted on   梦琪小生  阅读(581)  评论(0编辑  收藏  举报

编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示