zoukankan      html  css  js  c++  java
  • WPF学习笔记“窗口”四:三种方式不规则窗口实现大小的改变

      一、通过Window.ResizeMode = CanResizeWithGrip属性,可以实现右下角来改变窗口的大小;

      二、使用.NET的平台调用特性(P/Invoke)发送改变窗口尺寸的Win32消息;

      三、当用户拖动一个侧边时,简单地跟踪鼠标位置,并通过设置窗口的Width属性,手动地改变窗口的尺寸;

        使用二、三种方式都需要一个方法来探测用户何时会将鼠标移动到窗口的边缘上,这时,鼠标指针应当变为可以改变尺寸的光标;

      采用第二种方法的代码示例:

       

    View Code
    <Window x:Class="WpfApplication1.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="350" Width="525"
            AllowsTransparency="True" Background="{x:Null}" WindowStyle="None"
            ResizeMode="CanResizeWithGrip"
            x:Name="WindowName">
        <Grid>
            <Border>
                <Border.CornerRadius>
                    <CornerRadius TopLeft="20" TopRight="20" BottomLeft="20" BottomRight="20"></CornerRadius>
                </Border.CornerRadius>
                <Border.Background>
                    <LinearGradientBrush>
                        <GradientStop Offset="0" Color="Red"></GradientStop>
                        <GradientStop Offset="1" Color="Black"></GradientStop>
                        <GradientStop Offset="0.5" Color="Azure"></GradientStop>
                    </LinearGradientBrush>
                </Border.Background>
                <Button Content="Button" Height="23" Name="button1" Width="75" Click="button1_Click" />
            </Border>
        </Grid>
    </Window>
    View Code
    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 WpfApplication1
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                this.SourceInitialized += new EventHandler(Window1_SourceInitialized); 
                InitializeComponent();
            }
            void Window1_SourceInitialized(object sender, EventArgs e)
            {
                HwndSource hwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
    
                hwndSource.AddHook(new HwndSourceHook(WndProc));
    
            }
           private const int WM_NCHITTEST = 0x0084;
            private readonly int agWidth = 12; //拐角宽度   
            private readonly int bThickness = 4; // 边框宽度   
            private Point mousePoint = new Point(); //鼠标坐标   
    
            private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                if(msg == WM_NCHITTEST)
                {
                    this.mousePoint.X = (lParam.ToInt32() & 0xFFFF);
                    this.mousePoint.Y = (lParam.ToInt32() >> 16);
    
    
                    // 窗口左上角   
                    if (this.mousePoint.Y - this.Top <= this.agWidth
                        && this.mousePoint.X - this.Left <= this.agWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOPLEFT);
                    }
                    // 窗口左下角       
                    else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.agWidth
                        && this.mousePoint.X - this.Left <= this.agWidth)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMLEFT);
                    }
                    // 窗口右上角   
                    else if (this.mousePoint.Y - this.Top <= this.agWidth
                        && this.ActualWidth + this.Left - this.mousePoint.X <= this.agWidth)
                    {
                        handled = true;
                        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)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOMRIGHT);
                    }
                    // 窗口左侧   
                    else if (this.mousePoint.X - this.Left <= this.bThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTLEFT);
                    }
                    // 窗口右侧   
                    else if (this.ActualWidth + this.Left - this.mousePoint.X <= this.bThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTRIGHT);
                    }
                    // 窗口上方   
                    else if (this.mousePoint.Y - this.Top <= this.bThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTTOP);
                    }
                    // 窗口下方   
                    else if (this.ActualHeight + this.Top - this.mousePoint.Y <= this.bThickness)
                    {
                        handled = true;
                        return new IntPtr((int)HitTest.HTBOTTOM);
                    }
                    //else // 窗口移动   
                    //{
                    //    handled = true;
                    //    return new IntPtr((int)HitTest.HTCAPTION);
                    //}
                }
                return IntPtr.Zero;   
    
            }
            public static Int32 GET_X_LPARAM(int lParam)
            {
    
                return (lParam & 0xffff);
    
            }
            public static Int32 GET_Y_LPARAM(int lParam)
            {
    
                return (lParam >> 16);
    
            }
            public enum HitTest
            {
    
                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,
    
            }
     private void button1_Click(object sender, RoutedEventArgs e)
            {
                this.Close();
            }
        }
    }

       第二种方法能够比较完美的解决问题;

       采用第三种方式的代码示例:

      

    View Code
    <Window x:Class="Test.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="400" Width="500"
            
            ResizeMode="CanResizeWithGrip"
            AllowsTransparency="True" WindowStyle="None" Background="{x:Null}"
            >
            <!--关键的三个属性设置-->
        
        <Border Width="Auto" Height="Auto">
            <!--为窗口的四个角进行自定义的操作-->
            <Border.CornerRadius>
                <CornerRadius TopLeft="20" TopRight="20" BottomLeft="0" BottomRight="0"></CornerRadius>
            </Border.CornerRadius>
            <!--为窗口的背景进行操作-->
            <Border.Background>
                <LinearGradientBrush>
                    <GradientStop Offset="0" Color="Red"></GradientStop>
                    <GradientStop Offset="1" Color="AntiqueWhite"></GradientStop>
                    <GradientStop Offset="0.5" Color="BlueViolet"></GradientStop>
                </LinearGradientBrush>
            </Border.Background>
            <Grid>
                <Label MouseDoubleClick="Label_MouseDoubleClick" MouseLeftButtonDown="Window_Move"></Label>
                <Button Content="Button" Grid.Row="2" Height="23" HorizontalAlignment="Left" Margin="237,15,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
    
                <Rectangle Grid.Row="0" Name="RightBorder" Cursor="SizeWE" Fill="Transparent" Width="5" VerticalAlignment="Stretch" HorizontalAlignment="Right"
                    MouseLeftButtonDown="Window_InitiateWiden"
                    MouseLeftButtonUp="Window_EndWiden"
                    MouseMove="Window_Widen"></Rectangle>
                <Rectangle Grid.Row="0" Name="BottomBorder" Cursor="SizeNS" Fill="Transparent" Height="5" VerticalAlignment="Bottom" HorizontalAlignment="Stretch"
                    MouseLeftButtonDown="Window_InitiateWiden"
                    MouseLeftButtonUp="Window_EndWiden"
                    MouseMove="Window_Widen"></Rectangle>
            </Grid>
        </Border>
        <!--<Window.TaskbarItemInfo>
            <TaskbarItemInfo x:Name="taskBarItem">
                <TaskbarItemInfo.ThumbButtonInfos>
                    <ThumbButtonInfo Description="palt" Click="button1_Cck"></ThumbButtonInfo>
                </TaskbarItemInfo.ThumbButtonInfos>
            </TaskbarItemInfo>
        </Window.TaskbarItemInfo>-->
    </Window>
    View Code
    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;
    
    namespace Test
    {
        /// <summary>
        /// MainWindow.xaml 的交互逻辑
        /// </summary>
        public partial class MainWindow : Window
        {
    
            public MainWindow()
            {
                InitializeComponent();
            }
    
            //实现窗口的移动
            private void Window_Move(object sender, MouseButtonEventArgs e)
            {
                this.DragMove();
            }
    
            bool IsWiden = false;
    
            private void Window_InitiateWiden(object sender, MouseButtonEventArgs e)
            {
                IsWiden = true;
            }
    
            private void Window_EndWiden(object sender, MouseButtonEventArgs e)
            {
                IsWiden = false;
    
                Rectangle rectangle = (Rectangle)sender;
                rectangle.ReleaseMouseCapture();
            }
    
            private void Window_Widen(object sender, MouseEventArgs e)
            {
                Rectangle rectangle = sender as Rectangle;
                if (IsWiden)
                {
                    rectangle.CaptureMouse();
                    double newWidth = 0.0;
                    switch (rectangle.Name)
                    { 
                        case "RightBorder":
                            newWidth = e.GetPosition(this).X + 2;
                            if (newWidth > 0)
                            {
                                this.Width = newWidth;
                            }
                            break;
                        case "BottomBorder":
                            newWidth = e.GetPosition(this).Y + 2;
                            if (newWidth > 0)
                            {
                                this.Height = newWidth;
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
        }
    }

      第三种方式只能从窗口的右边和底边改变窗口的大小;

  • 相关阅读:
    sys、os 模块
    sh 了解
    TCP协议的3次握手与4次挥手过程详解
    python argparse(参数解析)模块学习(二)
    python argparse(参数解析)模块学习(一)
    Day17--Python--面向对象--成员
    Day16--Python--初识面向对象
    Day14--Python--函数二,lambda,sorted,filter,map,递归,二分法
    Day013--Python--内置函数一
    Day12--Python--生成器,生成器函数,推导式,生成器表达式
  • 原文地址:https://www.cnblogs.com/gengyuanchao/p/2726859.html
Copyright © 2011-2022 走看看