WPF 无边框窗体 缩放窗体

XAML代码:

<Window x:Class="WindowStyle.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" MinHeight="400" MinWidth="500" AllowsTransparency="True" WindowStyle="None">
    <Grid>
        <DockPanel Background="Transparent">
            <!--标题栏-->
            <Grid DockPanel.Dock="Top" Height="30" Background="LightGray" MouseLeftButtonDown="TitleBar_MouseLeftButtonDown">
                <DockPanel>
                    <Grid  DockPanel.Dock="Top" >
                        <DockPanel>
                            <!--图标-->
                            <Grid DockPanel.Dock="Left">
                                <Image Source="images/logo.png" Margin="5"/>
                            </Grid>
                            <!--关闭按钮-->
                            <Grid DockPanel.Dock="Right" Margin="6"> 
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="1*"/>
                                    <ColumnDefinition Width="1*"/>
                                    <ColumnDefinition Width="1*"/>
                                </Grid.ColumnDefinitions>
                                <Button x:Name="buttonMin" Click="buttonMin_Click" Background="Transparent" HorizontalAlignment="Right" BorderThickness="0"
										Margin="0" Grid.Column="0">
                                    <Image Source="images/Close_up_24px_1181430_easyicon.net.ico"/>
                                </Button>
                                <Button x:Name="buttonMax" Click="buttonMax_Click" Background="Transparent" HorizontalAlignment="Right" BorderThickness="0"
										Margin="0" Grid.Column="1">
                                    <Image Source="images/cursor_drag_arrow_silver_24px_1098825_easyicon.net.ico"/>
                                </Button>
                                <Button x:Name="buttonClose" Click="buttonClose_Click" Background="Transparent" HorizontalAlignment="Right" BorderThickness="0"
										Margin="0" Grid.Column="2">
                                    <Image Source="images/close_24px_1181428_easyicon.net.ico"/>
                                </Button>
                            </Grid>
                            <!--标题-->
                            <Grid>
                                <Label Content="设置" VerticalAlignment="Center" HorizontalAlignment="Center" FontSize="17" FontFamily="微软雅黑 Light" Height="30" />
                            </Grid>
                        </DockPanel>
                    </Grid>
                </DockPanel>
            </Grid>
            <!--状态栏-->
            <Grid x:Name="GridStatusBar" DockPanel.Dock="Bottom" Height="20" Background="#d9d9d9">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="400*"/>
                    <ColumnDefinition Width="20"/>
                </Grid.ColumnDefinitions>
                <Grid Grid.Column="0">
                    <TextBlock Margin="1,3,0,1"  x:Name="TextBlockOperationStatus" />
                </Grid>
                <Grid Grid.Column="1" >
                    <Image Name="ResizeBottomRight" Source="images/右下角icon.png" Stretch="None" VerticalAlignment="Bottom" HorizontalAlignment="Right" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
                </Grid>
            </Grid>
            <!--窗体部分-->
            <Grid DockPanel.Dock="Top">
            </Grid>
        </DockPanel>
    </Grid>
</Window>

后台代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
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.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WindowStyle
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.SourceInitialized += delegate(object sender, EventArgs e)
            {
                this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
            };
            this.MouseMove += new MouseEventHandler(Window_MouseMove);
        }

        private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }

        private void buttonClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }
        //定义窗体启动时的大小
        Rect rcnormal;
        private void buttonMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.Width < SystemParameters.WorkArea.Width)
            {

                rcnormal = new Rect(this.Left, this.Top, this.Width, this.Height);
                this.Left = SystemParameters.WorkArea.Left;
                this.Top = SystemParameters.WorkArea.Top;
                this.Width = SystemParameters.WorkArea.Width;
                this.Height = SystemParameters.WorkArea.Height;
            }
            else
            {
                this.Left = rcnormal.Left;
                this.Top = rcnormal.Top;
                this.Width = rcnormal.Width;
                this.Height = rcnormal.Height;
            }
        }

        private void buttonMin_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        #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);
        
        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);
        }
        #endregion
    }
}
运行效果


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值