程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> WPF實現無邊框窗體拖拽右下角▲ 改變窗體大小 謝謝大家關注,wpf窗體

WPF實現無邊框窗體拖拽右下角▲ 改變窗體大小 謝謝大家關注,wpf窗體

編輯:關於.NET

WPF實現無邊框窗體拖拽右下角▲ 改變窗體大小 謝謝大家關注,wpf窗體


效果圖:(右下角拖拽改變窗體大小)

第一步:添加xaml代碼:

<Border Name="ResizeBottomRight" MouseMove="ResizePressed"
                    MouseDown="ResizePressed" Height="15" Width="15" 
                    HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,3,3" RenderTransformOrigin="0.5,0.5">
                    <Border.RenderTransform>
                        <TransformGroup>
                            <ScaleTransform/>
                            <SkewTransform/>
                            <RotateTransform Angle="88.182"/>
                            <TranslateTransform/>
                        </TransformGroup>
                    </Border.RenderTransform>
                    <Border.Background>
                        <DrawingBrush>
                            <DrawingBrush.Drawing>
                                <GeometryDrawing Geometry="M96,128 L896,928 96,128 z M352,384 L128,160 172.19048,160 352,160 352,384 z M608,640 L384,416 608,416 608,640 z M608,384 L384,384 384,160 608,160 608,384 z M864,896 L640,672 864,672 864,896 z M864,640 L640,640 640,416 864,416 864,640 z M864,384 L640,384 640,160 864,160 864,384 z" Brush="#888888"></GeometryDrawing>
                            </DrawingBrush.Drawing>
                        </DrawingBrush>
                    </Border.Background>
                </Border>

第二步:xaml.cs後台代碼 聲明

#region 初始化窗體可以縮放大小
        private const int WM_SYSCOMMAND = 0x112;
        private HwndSource _HwndSource;
        private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
        {
            {ResizeDirection.BottomRight, Cursors.SizeNWSE},
        };
        private enum ResizeDirection
        {
            BottomRight = 8,
        }

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
        #endregion
事件:
this.SourceInitialized += delegate(object sender, EventArgs e)//執行拖拽 { this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource; }; this.MouseMove += new MouseEventHandler(Window_MouseMove);//鼠標移入到邊緣收縮 this.Loaded += new RoutedEventHandler(FluidWrapPanelDemo_Loaded);
private void Window_MouseMove(object sender, MouseEventArgs e)
        {
            if (Mouse.LeftButton != MouseButtonState.Pressed)
            {
                FrameworkElement element = e.OriginalSource as FrameworkElement;
                if (element != null && !element.Name.Contains("Resize"))
                    this.Cursor = Cursors.Arrow;
            }

        }
        private void ResizePressed(object sender, MouseEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            ResizeDirection direction = (ResizeDirection)Enum.Parse(typeof(ResizeDirection), element.Name.Replace("Resize", ""));
            this.Cursor = cursors[direction];
            if (e.LeftButton == MouseButtonState.Pressed)
                ResizeWindow(direction);
        }
private void ResizeWindow(ResizeDirection direction)
        {
            SendMessage(_HwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
        }

下次更新(菜單縮放):效果圖

 

 

轉載請注明地址~謝謝  謝謝大家關注

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved