WPF透明窗体不支持缩放解决方案
方案一
WPF中的无边框透明窗体,由于没有边并且透明,窗体无法进行缩放操作,今天来讲解如何解决这个问题。
先说一下思路,我们先手为该窗体添加4个边,4个角用于缩放操作,然后再为他们写事件,完成拖放操作。
Xaml文件
<Window x:Class="UniversalRobot.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:UniversalRobot"
mc:Ignorable="d"
Title="Window2" Height="300" Width="300" WindowStyle="None" AllowsTransparency="True">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="4"/>
<RowDefinition/>
<RowDefinition Height="4"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="4"/>
<ColumnDefinition/>
<ColumnDefinition Width="4"/>
</Grid.ColumnDefinitions>
<Rectangle Name="ResizeTopLeft" Fill="#01000000" Grid.Row="0" Grid.Column="0" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeTop" Fill="#01000000" Grid.Row="0" Grid.Column="1" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeTopRight" Fill="#01000000" Grid.Row="0" Grid.Column="2" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeLeft" Fill="#01000000" Grid.Row="1" Grid.Column="0" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeRight" Fill="#01000000" Grid.Row="1" Grid.Column="3" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeBottomLeft" Fill="#01000000" Grid.Row="3" Grid.Column="0" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeBottom" Fill="#01000000" Grid.Row="3" Grid.Column="1" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
<Rectangle Name="ResizeBottomRight" Fill="#01000000" Grid.Row="3" Grid.Column="2" MouseMove="ResizePressed" MouseDown="ResizePressed"/>
</Grid>
</Window>
后台代码
namespace UniversalRobot
{
/// <summary>
/// Window2.xaml 的交互逻辑
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent(); this.SourceInitialized += delegate (object sender, EventArgs e)
{
this._HwndSource = PresentationSource.FromVisual((Visual)sender) as HwndSource;
}; } private const int WM_SYSCOMMAND = 0x112;
private HwndSource _HwndSource;
private enum ResizeDirection
{
Left = 1,
Right = 2,
Top = 3,
TopLeft = 4,
TopRight = 5,
Bottom = 6,
BottomLeft = 7,
BottomRight = 8,
}
private Dictionary<ResizeDirection, Cursor> cursors = new Dictionary<ResizeDirection, Cursor>
{
{ResizeDirection.Top, Cursors.SizeNS},
{ResizeDirection.Bottom, Cursors.SizeNS},
{ResizeDirection.Left, Cursors.SizeWE},
{ResizeDirection.Right, Cursors.SizeWE},
{ResizeDirection.TopLeft, Cursors.SizeNWSE},
{ResizeDirection.BottomRight, Cursors.SizeNWSE},
{ResizeDirection.TopRight, Cursors.SizeNESW},
{ResizeDirection.BottomLeft, Cursors.SizeNESW} };
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);
}
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam); private void ResizeWindow(ResizeDirection direction)
{
SendMessage(_HwndSource.Handle, WM_SYSCOMMAND, (IntPtr)(61440 + direction), IntPtr.Zero);
} }
}
从代码可以看出,先注册4个边和4个角的MouseMove和MouseDown事件,鼠标移动到拖放内容上时,判断鼠标悬停在那个边上,改变鼠标指针变成相应对象,判断鼠标是否按下,如果按下了,则发送Win32消息,进行拖放操作,从代码中可以看出来最终的拖放还是使用Win32 api来实现,因为,如果完全用wpf的事件进行拖放的话,实在是太慢了。
方案二
进一步,把该功能封装到自己的类中。
WPF透明窗体不支持缩放解决方案的更多相关文章
- WPF透明窗体制作
原文:WPF透明窗体制作 窗体的样式: <Grid Width="{Binding Width, ElementName=w}" Height="{Binding ...
- wpf 透明窗体中使用webbrowser
wpf ,PNG图形半透明窗体 ,使用webbrowser控件 附件:http://files.cnblogs.com/xe2011/WpfApplication1_webbrowser_tran ...
- WPF 透明窗体
窗体属性中设置:Background="Transparent" AllowsTransparency="True" WindowStyle="Non ...
- WPF中窗体最大化问题处理
遇到的问题信息 问题:当WindowStyle=None时,窗口最大化,不显示任务栏 -- 即窗体是全屏效果. 解决中遇到的问题列表[主要涉及到任务栏发生改变后的一些问题处理]: 最大化时,任务栏被遮 ...
- WPF自适应窗体实现小结
WPF自适应窗体实现小结 这几天,因工作需要,要对一个小软件进行UI调整.主要内容就是让其能够实现自适应窗体(包括文字和图标),做成像WIN7下的Media Center一样的UI.自适应窗体,顾名思 ...
- 【转载】Layered Window(分层窗体,透明窗体)
本文转载自花间醉卧<Layered Window(分层窗体,透明窗体)> //为窗体添加WS_EX_LAYERED属性,该属性使窗体支持透明 ModifyStyleEx(0, WS_EX_ ...
- [小结][N种方法]实现WPF不规则窗体
原文:[小结][N种方法]实现WPF不规则窗体 WPF实现不规则窗体,方法很多很多多.... 本文总结DebugLZQ认为简洁高效的几种方法 实现WPF不规则窗体的几种常用的方法如下: 1.使用Ble ...
- C#在透明窗体WinForm上面画图(电子尺小工具的实现)
前几天要做一个微信调一调的外挂,里面用到了尺子测量距离,然后就自己下载了一个电子尺,最近要升级我的跳一跳外挂,然后就准备自己做一个电子尺,嵌入到我的外挂里面,在嵌入到我的外挂之前,我自己做了一个完整版 ...
- WPF 透明掩码 OpactiyMask
原文:WPF 透明掩码 OpactiyMask 在WPF中提供了Opacity属性使得元素的所有内容都是透明的.而OpacityMask属性可以使元素的特定区域变成透明. OpacityMask属性接 ...
随机推荐
- 好记性比如烂笔头--linux学习笔记7关于linux中的shell脚本编程
之前看的各种面试,貌似都有shell脚本编程,没了解之前感觉很复杂,现在了解了些,没想象中那么难. 逻辑主要是这样的 编写.sh的脚本文件,文件里面的代码,就是在命令行输入的可执行命令的加强版,所谓加 ...
- 跟我学算法-图像识别之图像分类(下)(GoogleNet网络, ResNet残差网络, ResNext网络, CNN设计准则)
1.GoogleNet 网络: Inception V1 - Inception V2 - Inception V3 - Inception V4 1. Inception v1 split - me ...
- Node.js 项目打包
Node项目基于Electron打包 Electron打包打包后项目.exe程序包含在文件夹中,基于Electron打包的基础上直接打包成exe程序 参考一 参考二 需求的软件环境: NSIS 2.4 ...
- LeetCode之二叉树作题java
100. Same Tree Total Accepted: 127501 Total Submissions: 294584 Difficulty: Easy Given two binary tr ...
- Python运维开发基础03-语法基础
上节作业回顾(讲解+温习60分钟) #!/usr/bin/env python3 # -*- coding:utf-8 -*- # author:Mr.chen #只用变量和字符串+循环实现" ...
- Selenium+TestNG+Maven 搭建
Java环境配置 Eclipse配置TestNG Eclipse配置Maven pom.xml文件相关配置,添加依赖selenium和TestNg的jar包 <!-- https://mvnre ...
- Spring Boot 响应jsp
添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...
- [Linux] Big-endian and Little-endian (大小端模式)
Big-endian Little-endian 大小端模式 https://en.wikipedia.org/wiki/Endianness 大端模式,是指数据的高字节保存在内存的低地址中,而数 ...
- [Training Video - 1] [Selenium Basics] [What is Selenium IDE,RC,Webdriver, TestNG, Junit And Ant]
Selenium IDE (Only support in Firefox): - Record and Run - UI interface - User extensions - Conversi ...
- [OS]windows 2012 server-Local users and groups-Backup Operators
怎样找到windows 2012 server上的Backup Operators Press the Windows + R keys to open the Run dialog, type lu ...