自定义鼠标修改窗口大小
来源:互联网 发布:星际淘宝网下载 编辑:程序博客网 时间:2024/05/01 18:58
在windows的开发中,常常有自定义窗口外观的需要。通常都是设置WindowStyle="None",然后在客户区模拟一个标题栏。然而在模拟标题栏的时候会发现一个问题:你已经把自定义的标题栏放在界面的最上方了,可是上边总还会有有一部分空白(如果给标题栏设置个深色背景,会很容易发现)。这是什么原因呢?因为窗口的ResizeMode并不为None,它在客户区的周围仍保留有一部分空间,使得可以通过鼠标拖拽修改窗口大小。这对于我来说是无法接受的,于是设置ResizeMode="None",那这个时候就需要自己来完成鼠标拖拽修改窗口大小的功能。
下面是XAML文件及其界面效果:
<Window x:Class="WindowStyleNone.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" WindowStyle="None" Background="Transparent"> <Border Width="Auto" Height="Auto" Name="windowFrame" BorderBrush="#395984" BorderThickness="1" CornerRadius="0,20,30,40" MouseMove="windowFrame_MouseMove" MouseLeftButtonUp="windowFrame_MouseLeftButtonUp" MouseLeftButtonDown="windowFrame_MouseLeftButtonDown" > <Border.Background> <SolidColorBrush Color="AliceBlue"></SolidColorBrush> </Border.Background> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto"></RowDefinition> <RowDefinition></RowDefinition> <RowDefinition Height="Auto"></RowDefinition> </Grid.RowDefinitions> <TextBlock Text="Title Bar" Name="TitleBar" Margin="2" Padding="5"></TextBlock> <Grid Grid.Row="1" Background="#B5CBEF"> <TextBlock VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="20" Text="Content Goes Here"></TextBlock> </Grid> <TextBlock Name="foot" Grid.Row="2" Text="Footer" Margin="1,10,1,1" Padding="5" HorizontalAlignment="Center"></TextBlock> </Grid> </Border></Window>
后台程序:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;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 WindowStyleNone{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { private Point CursorOriginPoint; private int Gap=4; private bool IsMouseDown = false; public MainWindow() { InitializeComponent(); } private void windowFrame_MouseMove(object sender, MouseEventArgs e) { Point pt = e.GetPosition(this); Point spt = this.PointToScreen(pt); if (!IsMouseDown) { this.Cursor = Cursors.Arrow; if (pt.X > this.Width - this.Gap) this.Cursor = Cursors.SizeWE; if (pt.Y > this.Height - this.Gap) this.Cursor = Cursors.SizeNS; } else { if (this.Cursor.Equals(Cursors.SizeWE)) this.Width += Point.Subtract(spt, CursorOriginPoint).X; if (this.Cursor.Equals(Cursors.SizeNS)) this.Height += Point.Subtract(spt, CursorOriginPoint).Y; } CursorOriginPoint = spt; } private void windowFrame_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { if (e.Source.Equals(this.TitleBar)) { this.DragMove(); } else { Border border = sender as Border; border.CaptureMouse(); this.IsMouseDown = true; this.CursorOriginPoint = this.PointToScreen(e.GetPosition(this)); this.foot.Text = "Down"; } } private void windowFrame_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { Border border = sender as Border; if(border.IsMouseCaptured) border.ReleaseMouseCapture(); this.IsMouseDown = false; this.Cursor = Cursors.Arrow; this.foot.Text = "Up"; } }}
0 0
- 自定义鼠标修改窗口大小
- 修改Linux窗口大小
- Android: 自定义窗口大小
- 自定义activity窗口大小
- 修改鼠标样式--自定义
- windows命令行窗口大小修改
- ueditor修改涂鸦窗口大小
- 修改QQ聊天窗口大小
- android自定义Activity窗口大小
- android 自定义Activity窗口大小
- 利用OpenCV鼠标控制窗口大小
- 利用OpenCV鼠标控制窗口大小
- Qt5 鼠标滚轮控制窗口大小
- 修改css定制自定义鼠标
- MFC改变自定义鼠标图标的大小
- 修改Gnome Terminal窗口的默认大小
- Access 修改窗口大小(亲自实践)
- VS2010中不能修改窗口大小问题
- 基于jQuery下拉两级联动select
- Mysql中创建根据第二个自增的id
- OCruntime交换方法用在处理iOS版本跨度问题的解决
- 图形处理(九)点云重建(下)法矢求取、有向距离场等值面提取
- HashMap的工作原理总结
- 自定义鼠标修改窗口大小
- RecyclerView的使用
- 图形处理(十)测地极坐标参数化
- 精华阅读第 5 期 | 移动开发精英俱乐部
- leetcode3. Longest Substring Without Repeating Characters
- 编译安装lamp
- ios实时播放PCM数据
- socket编程
- LINK:fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏