wpf无边框窗体移动和大小调整
using System;
using System.Windows;
using System.Windows.Interop;
namespace Wpftest
{
public partial class MainWindow : Window
{
public MainWindow()
{
this.InitializeComponent();
// 在此点下面插入创建对象所需的代码。
}
protected override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
if (hwndSource != null)
{
hwndSource.AddHook(new HwndSourceHook(this.WndProc));
}
}
private const int WM_NCHITTEST = 0x0084;
private readonly int agWidth = 12; //拐角宽度
private readonly int bThickness = 4; // 边框宽度
private Point mousePoint = new Point(); //鼠标坐标
public enum HitTest : int
{
HTERROR = -2,
HTTRANSPARENT = -1,
HTNOWHERE = 0,
HTCLIENT = 1,
HTCAPTION = 2,
HTSYSMENU = 3,
HTGROWBOX = 4,
HTSIZE = HTGROWBOX,
HTMENU = 5,
HTHSCROLL = 6,
HTVSCROLL = 7,
HTMINBUTTON = 8,
HTMAXBUTTON = 9,
HTLEFT = 10,
HTRIGHT = 11,
HTTOP = 12,
HTTOPLEFT = 13,
HTTOPRIGHT = 14,
HTBOTTOM = 15,
HTBOTTOMLEFT = 16,
HTBOTTOMRIGHT = 17,
HTBORDER = 18,
HTREDUCE = HTMINBUTTON,
HTZOOM = HTMAXBUTTON,
HTSIZEFIRST = HTLEFT,
HTSIZELAST = HTBOTTOMRIGHT,
HTOBJECT = 19,
HTCLOSE = 20,
HTHELP = 21,
}
protected virtual IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case WM_NCHITTEST:
{
this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
this.mousePoint.Y = (lParam.ToInt32() >> 16);
//告诉系统你已经处理过该消息,不然设置为false
handled = true;
#region 测试鼠标位置
// 窗口左上角
if (this.mousePoint.Y - this.Top <= this.agWidth
&& this.mousePoint.X - this.Left <= this.agWidth)
{
return new IntPtr((int)HitTest.HTTOPLEFT);
}
// 窗口左下角
else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth
&& this.mousePoint.X - this.Left <= this.agWidth)
{
return new IntPtr((int)HitTest.HTBOTTOMLEFT);
}
// 窗口右上角
else if (this.mousePoint.Y - this.Top <= this.agWidth
&& this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)
{
return new IntPtr((int)HitTest.HTTOPRIGHT);
}
// 窗口右下角
else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth
&& this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth)
{
return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
}
// 窗口左侧
else if (this.mousePoint.X - this.Left <= this.bThickness)
{
return new IntPtr((int)HitTest.HTLEFT);
}
// 窗口右侧
else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)
{
return new IntPtr((int)HitTest.HTRIGHT);
}
// 窗口上方
else if (this.mousePoint.Y - this.Top <= this.bThickness)
{
return new IntPtr((int)HitTest.HTTOP);
}
// 窗口下方
else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)
{
return new IntPtr((int)HitTest.HTBOTTOM);
}
else // 窗口移动(也可在窗体MouseLeftButtonDown事件中调用DragMove()方法),双击最大化,所有剩余鼠标操作映射到标题栏
{
return new IntPtr((int)HitTest.HTCAPTION);
}
#endregion
}
}
return IntPtr.Zero;
}
}
}
弊端 窗体 中控件 事件 失效
wpf无边框窗体移动和大小调整的更多相关文章
- WPFの无边框窗体以及控件的移动
对于WPF,一旦隐藏了标题栏,就无法移动,这时候需要重写移动方法,下面列举常见的三种方式方式. 方式一:重写OnMouseLeftButtonDown protected override void ...
- C# 改变无边框窗体的尺寸大小
以下代码为修改窗体尺寸的代码: const int HTLEFT = 10; ; ; ; ; ; const int HTBOTTOMLEFT = 0x10; ; protected override ...
- WinForm 无边框窗体改变尺寸及移动窗体
#region 无边框窗体移动改变大小 [DllImport("user32.dll")] public static extern bool ReleaseCapture(); ...
- 【WPF】无边框窗体
之前写了一个支持尺寸变换的无边框窗体的一个基窗体,代码如下: public class LBaseWindow : Window { /// <summary> /// 基窗体 /// & ...
- 【转】【WPF】 WPF 调用API修改窗体风格实现真正的无边框窗体
WPF中设置无边框窗体似乎是要将WindowStyle设置为None,AllowTransparency=true,这样才能达到WinForm中无边框窗体的样式.但是AllowTransparency ...
- Qt 无边框窗体改变大小 完美实现(全部自己实现)
近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...
- 利用WPF创建含多种交互特性的无边框窗体
咳咳,标题一口气读下来确实有点累,让我先解释一下.另外文章底部有演示程序的下载. 本文介绍利用WPF创建一个含有以下特性的窗口: 有窗口阴影,比如QQ窗口外围只有几像素的阴影: 支持透明且无边框,为了 ...
- 01.WPF中制作无边框窗体
[引用:]http://blog.csdn.net/johnsuna/article/details/1893319 众所周知,在WinForm中,如果要制作一个无边框窗体,可以将窗体的FormB ...
- Qt 无边框窗体改变大小 完美实现
近期,做项目用到无边框窗体,令人蛋疼的是无边框窗体大小的改变要像右边框那样,上下左右四周,而且要流畅. 网上也找了些代码,发现居然还要连接到windows事件,这显然不合常理,后来自己新建了demo, ...
随机推荐
- 计算机图形学(二)输出图元_3_画线算法_2_DDA算法
DDA算法 数字微分分析仪(digital differential analyzer, DDA)方法是一种线段扫描转换算法.基于使用等式(3.4)或等式(3.5)计算的&x或& ...
- P2P网络借贷系统简要解读
P2P网络借贷系统的核心功能其实很简单,就是若干投资者(借款人)集体把钱贷给贷款人,到期后,贷款人把利息和本金按时还给借款人.平台的主要作用是"撮合",收取一定的服务费和管理费. ...
- Win10安装后必做的优化,解决磁盘100%占用
Win10安装后必做的优化,解决磁盘100%占用 01关闭家庭组 控制面板–管理工具–服务– HomeGroup Listener和HomeGroup Provider禁用. 02关闭磁盘碎片整理.自 ...
- [TypeScript] Understand lookup types in TypeScript
Lookup types, introduced in TypeScript 2.1, allow us to dynamically create types based on the proper ...
- Android 获取签名证书的具体信息(Eclipse和Android studio通用)
今天要用到签名证书的MD5,可是这个仅仅有在第一次生成的时候我看到了,这可怎么办呢,幸亏我们有google,我们执行以下的命令就OK了. keytool -list -v -keystore 签名证书 ...
- 将Sublime Text2 加入右键菜单
在googleread里面看有人推荐sublime text2.说开发很方便.就下载一个试试.写html还真的挺爽. 于是按照vim加入鼠标右键的方法.果然可以.这里和大家分享 1. 运行中输入 re ...
- Javascript 获取页面高度(多种浏览器)
//2015年8月13日11:00:50 网页可见区域宽: document.body.clientWidth网页可见区域高: document.body.clientHeight网页可见区域宽: d ...
- [SCSS] Use Standard Built-in SCSS Functions for Common Operations
We can use javascript for color and opacity variations, math, list and map logic or to see if someth ...
- GCD网络多线程---同步运行,异步运行,串行队列,并行队列
总结:同步(无论是串行还是并行)----不又一次开辟子线程 异步(无论是串行还是并行)----开辟子线程 GCD: dispatch queue 主线程的main queue 并行队列 global ...
- source insight totalcmd 中文目录
@echo off for /F "usebackq delims=" %%a in (`echo %1^^^|iconv -f utf-8 -t gb18030`) do ( s ...