【转】【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 = -;
/// <summary> /// window的扩展样式 /// </summary>
public const int GWL_EXSTYLE = -;
/// <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
【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体的更多相关文章
- WPF 调用API修改窗体风格实现真正的无边框窗体
原文:WPF 调用API修改窗体风格实现真正的无边框窗体 WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm ...
- 【Winfrom-无边框窗体】Winform如何拖动无边框窗体?
去掉边框 this.FormBorderStyle = FormBorderStyle.None; 方法一: Point mouseOff;//鼠标移动位置变量 bool leftFlag;//标签是 ...
- C# 鼠标点击移动窗体代码,可以实现无边框窗体的拖动
private static bool IsDrag = false; private int enterX; private int enterY; private void setForm_Mou ...
- wpf无边框窗体移动和大小调整
原文:wpf无边框窗体移动和大小调整 using System; using System.Windows; using System.Windows.Interop; namespace Wpf ...
- 01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...
- WPFの无边框窗体以及控件的移动
对于WPF,一旦隐藏了标题栏,就无法移动,这时候需要重写移动方法,下面列举常见的三种方式方式. 方式一:重写OnMouseLeftButtonDown protected override void ...
- WPF中制作无边框窗体
原文:WPF中制作无边框窗体 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormBorderStyle属性设置为None来完成.如果要制作成异形窗体,则需要使用图片或者使用G ...
- 【WPF】无边框窗体
之前写了一个支持尺寸变换的无边框窗体的一个基窗体,代码如下: public class LBaseWindow : Window { /// <summary> /// 基窗体 /// & ...
- 无边框窗体和timer控件
一.无边框窗体 1.控制按钮如何制作就是放置可以点击的控件,不局限于使用按钮或是什么别的,只要放置的控件可以点击能触发点击事件就可以了 做的好看一点,就是鼠标移入(pictureBox1_MouseE ...
随机推荐
- oc 字符串
#import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepool { ...
- IOS 应用跳转 (IOS9白名单)
跳转到指定app的实现 IOS中应用的跳转是通过URL实现的,因此在实现应用跳转之前我们要设置一下对应的URL. 图一(寻找配置软件的URL) 图二(具体配置选项) 注意: 如果IOS版本为IOS9 ...
- C# C/S WPF 远程操作服务器上面的文件
作的时候用的是WPF,需要做一个上传附件的功能,服务器上有一个文件夹,附件都上传到里面,只知道URL路径. 文件夹是在服务器上的IIS里面(就比如说你发布一个网站,把文件夹建在网站下面,当然这个网站啥 ...
- Java中读取properties资源文件
一.通过ResourceBundle来读取.properties文件 /** * 通过java.util.resourceBundle来解析properties文件. * @param String ...
- 编译时:virtual memory exhausted: Cannot allocate memory
一.问题 当安装虚拟机时系统时没有设置swap大小或设置内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩 ...
- 【原】基于 HAproxy 1.6.3 Keeplived 在 Centos 7 中实现mysql mariadb galera cluster 集群分发读写 —— 上篇
前言 有一段时间没有写blogs,乘着周末开始整理下haproxy + keeplived 实现 mysql mariadb galera cluster 集群访问环境的搭建工作. 本文集中讲hapr ...
- 虚拟机centos6.5 --设置静态ip
编辑网卡文件,vi /etc/sysconfig/network-scripts/ifcfg-eth0修改以下内容: ONBOOT=no #改为yes, BOOTPROTO=dbcp #改为stati ...
- 爆零后的感受外加一道强联通分量HDU 4635的题解
今天又爆零了,又是又,怎么又是又,爆零爆多了,又也就经常挂嘴边了,看到这句话,你一定很想说一句””,弱菜被骂傻,也很正常啦. 如果你不开心,可以考虑往下看. 翻到E(HDU 4635 Strongly ...
- 利用模板在RM里部署VM
Refer to: https://www.azure.cn/documentation/articles/virtual-machines-windows-ps-template/ 过程中遇到的几个 ...
- Linked List Cycle
Given a linked list, determine if it has a cycle in it. /** * Definition for singly-linked list. * s ...