• WPF窗口设置NoResize属性后自定义窗口拖动缩放


    在xmal中添加如下控件,分别标记左、右、上、下各条边以及左上、右上、左下、右下四个角,当鼠标移入、移出、鼠标点击时分别触发设置鼠标图标、重置图标、触发resize等操作

    <Path x:Name="ResizeNW" VerticalAlignment="Top" HorizontalAlignment="Left"
          Stroke="Green" StrokeThickness="4" Margin="0" MouseEnter="DisplayResizeCursor"
               MouseLeave="ResetCursor" PreviewMouseLeftButtonDown="Resize">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure StartPoint="0,10">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <QuadraticBezierSegment Point1="0,0" Point2="10,0" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path x:Name="ResizeNE" VerticalAlignment="Top" HorizontalAlignment="Right"
          Stroke="Green" StrokeThickness="4" Margin="0,0,-2,0" MouseEnter="DisplayResizeCursor"
               MouseLeave="ResetCursor" PreviewMouseLeftButtonDown="Resize">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure StartPoint="0,0">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <QuadraticBezierSegment Point1="10,0" Point2="10,10" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path x:Name="ResizeSE" VerticalAlignment="Bottom" HorizontalAlignment="Right"
          Stroke="Green" StrokeThickness="4" Margin="0,0,-2,-2" MouseEnter="DisplayResizeCursor"
               MouseLeave="ResetCursor" PreviewMouseLeftButtonDown="Resize">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure StartPoint="10,0">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <QuadraticBezierSegment Point1="10,10" Point2="0,10" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
    <Path x:Name="ResizeSW" VerticalAlignment="Bottom" HorizontalAlignment="Left"
          Stroke="Green" StrokeThickness="4" Margin="0,0,0,-2" MouseEnter="DisplayResizeCursor"
               MouseLeave="ResetCursor" PreviewMouseLeftButtonDown="Resize">
        <Path.Data>
            <PathGeometry>
                <PathGeometry.Figures>
                    <PathFigureCollection>
                        <PathFigure StartPoint="0,0">
                            <PathFigure.Segments>
                                <PathSegmentCollection>
                                    <QuadraticBezierSegment Point1="0,10" Point2="10,10" />
                                </PathSegmentCollection>
                            </PathFigure.Segments>
                        </PathFigure>
                    </PathFigureCollection>
                </PathGeometry.Figures>
            </PathGeometry>
        </Path.Data>
    </Path>
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76

    自定义窗口的实现代码如下:

    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;
    using System.Runtime.InteropServices;
    
    namespace ResizableWindow
    {
        /// 
        /// Interaction logic for Window1.xaml
        /// 
        public partial class Window1 : Window
        {
            private const int WM_SYSCOMMAND = 0x112;
            private HwndSource hwndSource;
    
            private enum ResizeDirection
            {
                Left = 61441,
                Right = 61442,
                Top = 61443,
                TopLeft = 61444,
                TopRight = 61445,
                Bottom = 61446,
                BottomLeft = 61447,
                BottomRight = 61448,
            }
    
            [DllImport( "user32.dll", CharSet = CharSet.Auto )]
            private static extern IntPtr SendMessage( IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam );
    
            public Window1()
            {
                SourceInitialized += Window1_SourceInitialized;
    
                InitializeComponent();
            }
    
            private void Window1_SourceInitialized( object sender, EventArgs e )
            {
                hwndSource = PresentationSource.FromVisual( (Visual)sender ) as HwndSource;
            }
    
            private void ResizeWindow( ResizeDirection direction )
            {
                SendMessage( hwndSource.Handle, WM_SYSCOMMAND, (IntPtr)direction, IntPtr.Zero );
            }
    
            protected void ResetCursor( object sender, MouseEventArgs e )
            {
                if( Mouse.LeftButton != MouseButtonState.Pressed )
                {
                    this.Cursor = Cursors.Arrow;
                }
            }
    
            protected void Resize( object sender, MouseButtonEventArgs e )
            {
                var clickedShape = sender as Shape;
    
                switch( clickedShape.Name )
                {
                    case "ResizeN":
                        this.Cursor = Cursors.SizeNS;
                        ResizeWindow( ResizeDirection.Top );
                        break;
                    case "ResizeE":
                        this.Cursor = Cursors.SizeWE;
                        ResizeWindow( ResizeDirection.Right );
                        break;
                    case "ResizeS":
                        this.Cursor = Cursors.SizeNS;
                        ResizeWindow( ResizeDirection.Bottom );
                        break;
                    case "ResizeW":
                        this.Cursor = Cursors.SizeWE;
                        ResizeWindow( ResizeDirection.Left );
                        break;
                    case "ResizeNW":
                        this.Cursor = Cursors.SizeNWSE;
                        ResizeWindow( ResizeDirection.TopLeft );
                        break;
                    case "ResizeNE":
                        this.Cursor = Cursors.SizeNESW;
                        ResizeWindow( ResizeDirection.TopRight );
                        break;
                    case "ResizeSE":
                        this.Cursor = Cursors.SizeNWSE;
                        ResizeWindow( ResizeDirection.BottomRight );
                        break;
                    case "ResizeSW":
                        this.Cursor = Cursors.SizeNESW;
                        ResizeWindow( ResizeDirection.BottomLeft );
                        break;
                    default:
                        break;
                }
            }
    
            protected void DisplayResizeCursor( object sender, MouseEventArgs e )
            {
                var clickedShape = sender as Shape;
    
                switch( clickedShape.Name )
                {
                    case "ResizeN":
                    case "ResizeS":
                        this.Cursor = Cursors.SizeNS;
                        break;
                    case "ResizeE":
                    case "ResizeW":
                        this.Cursor = Cursors.SizeWE;
                        break;
                    case "ResizeNW":
                    case "ResizeSE":
                        this.Cursor = Cursors.SizeNWSE;
                        break;
                    case "ResizeNE":
                    case "ResizeSW":
                        this.Cursor = Cursors.SizeNESW;
                        break;
                    default:
                        break;
                }
            }
    
            protected void DragWindow( object sender, MouseButtonEventArgs e )
            {
                DragMove();
            }
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
  • 相关阅读:
    PyCharm 2024.1最新变化
    MySQL驱动包下载
    on java8之复用
    【线程池、有返回值的线程池、线程池监控】
    Linux15 --- 信号量
    RGB彩色模型理解与编程实例
    zlMediaKit 2 event-poller模块--reactor+管道回调执行异步任务队列+红黑树执行定时任务
    直播预告|OceanBase 社区版 4.0 全解析
    ES6数组的方法及伪数组转数组方法
    2022软件测试技能 Robotframework + SeleniumLibrary + Jenkins web关键字驱动自动化实战教程
  • 原文地址:https://blog.csdn.net/liusarzn/article/details/133928624