wpf无边框窗体移动和大小调整

简介: 原文:wpf无边框窗体移动和大小调整   using System; using System.Windows; using System.
原文: 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控件和窗体一起放大一起缩小
242 0
|
C# 容器
WPF框架下,窗体的嵌套显示
WPF框架下,窗体的嵌套显示
226 0
|
C# 索引
WPF实用指南二:移除窗体的图标
原文:WPF实用指南二:移除窗体的图标 WPF没有提供任何功能来移除窗体上的icon图标。一般的做法是设置一个空白的图标,如下图1: 这种做法在窗体边框与标题之间仍然会保留一片空白。
1259 0
|
C#
WPF 创建无边框的圆角窗口
原文:WPF 创建无边框的圆角窗口 如题所述,在WPF中要创建一个没有边框且为圆角的窗体,有如下几步工作要进行: 第一步:去掉窗体默认样式的边框 首先将窗体的背景设为透明,将允许透明的属性设置为True,...
2704 0
|
C#
WPF去除窗体边框及白色边框
原文:WPF去除窗体边框及白色边框        0
1465 0
|
C# Windows
WPF 设置类库项目为启动项,设置窗体跟随。
原文:WPF 设置类库项目为启动项,设置窗体跟随。 1、添加用于启动的类Program.cs,需要一个静态的Main函数入口。
1031 0
|
C# Windows
WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
原文:WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮 在边框中加入一些元素,在应用程序的界面设计中,已经开始流行起来。
1383 0
|
C#
WPF 透明窗体
原文:WPF 透明窗体 窗体属性中设置:Background="Transparent" AllowsTransparency="True" WindowStyle="None"注:单独设置 Background="Transparent" 窗体默认显示为黑色。
1061 0
|
C#
wpf窗体定位
原文:wpf窗体定位 据WPF外包小编了解,通常,不需要在屏幕上明确定位窗口。而是简单地将WindowState属性设置为Normal,并忽略其他所有细节。另一方面,很少会将WindowStartupLocation属性设置为Manual,并使用Left属性和Right属性明确设置窗口的位置。
877 0
|
C# C++ Windows
WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案
原文:WPF中不规则窗体与WindowsFormsHost控件的兼容问题完美解决方案          首先先得瑟一下,有关WPF中不规则窗体与WindowsFormsHost控件不兼容的问题,网上给出的解决方案不能满足所有的情况,是有特定条件的,比如  WPF中不规则窗体与WebBrowser控件的兼容问题解决办法。
1318 0