WPF无边框窗体-可移动可调整大小
来源:互联网 发布:windows tracert 多个 编辑:程序博客网 时间:2024/05/21 07:55
Window属性设置
<Window x:Class="MainInterface.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
WindowStyle="None"
Background="{x:Null}"
AllowsTransparency="True"
Title="iHealth"
Height="594"
MinHeight="400"
Width="288"
MaxWidth="500">
<Grid>
<Border Background="Orange"/>
</Grid>
</Window>
借助 HwndSource 添加 Hook,截取 Windows 消息
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.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
namespace MainInterface
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
#region 无边框窗体移动和大小调整
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 override void OnSourceInitialized(EventArgs e)
{
base.OnSourceInitialized(e);
HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
if (hwndSource != null)
{
hwndSource.AddHook(new HwndSourceHook(this.WndProc));
}
}
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;
}
#endregion
}
}
- WPF无边框窗体-可移动可调整大小
- wpf无边框窗体移动和大小调整
- WPF 下无边框窗体改变大小和移动
- wpf无边框窗体移动和大小调整
- QT无边框可移动可改变大小窗体
- 无边框,可调整大小的FORM
- WPF 无边框窗体
- 【WPF无边框窗体】
- WPF中的无边框透明可缩放窗体
- 无边框窗体移动
- WPF (2).非常奇怪的移动无边框窗体
- winform 无边框窗口 可改变窗体大小
- Qt 无边框、透明、可移动的个性窗体
- Qt 无边框、透明、可移动的个性窗体
- QT实现无边框的可移动窗体
- Qt 无边框、透明、可移动、的个性窗体
- 无边框窗体改变大小并移动位置
- C# 无边框窗体移动和改变大小的实现
- 基于Mahout的图书推荐系统
- tableView 分组间距
- 开发基本守则
- 基于Docker的PHP开发环境
- C++ 类的 复制构造函数 和 赋值构造函数
- WPF无边框窗体-可移动可调整大小
- Android Studio中如何跳到某一个方法
- linux下操作cpu寄存器
- codeforces 496C Removing Columns (模拟,思维)
- php变量什么情况下加大括号{}
- Objective-C NSString NSData Byte CGFloat等转换
- 智图—源于QQ空间图片WebP化的思考
- linux组管理
- 一个懒得程序员才是一个好的程序员