方案一

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透明窗体不支持缩放解决方案的更多相关文章

  1. WPF透明窗体制作

    原文:WPF透明窗体制作 窗体的样式: <Grid Width="{Binding Width, ElementName=w}" Height="{Binding ...

  2. wpf 透明窗体中使用webbrowser

    wpf ,PNG图形半透明窗体 ,使用webbrowser控件   附件:http://files.cnblogs.com/xe2011/WpfApplication1_webbrowser_tran ...

  3. WPF 透明窗体

    窗体属性中设置:Background="Transparent" AllowsTransparency="True" WindowStyle="Non ...

  4. WPF中窗体最大化问题处理

    遇到的问题信息 问题:当WindowStyle=None时,窗口最大化,不显示任务栏 -- 即窗体是全屏效果. 解决中遇到的问题列表[主要涉及到任务栏发生改变后的一些问题处理]: 最大化时,任务栏被遮 ...

  5. WPF自适应窗体实现小结

    WPF自适应窗体实现小结 这几天,因工作需要,要对一个小软件进行UI调整.主要内容就是让其能够实现自适应窗体(包括文字和图标),做成像WIN7下的Media Center一样的UI.自适应窗体,顾名思 ...

  6. 【转载】Layered Window(分层窗体,透明窗体)

    本文转载自花间醉卧<Layered Window(分层窗体,透明窗体)> //为窗体添加WS_EX_LAYERED属性,该属性使窗体支持透明 ModifyStyleEx(0, WS_EX_ ...

  7. [小结][N种方法]实现WPF不规则窗体

    原文:[小结][N种方法]实现WPF不规则窗体 WPF实现不规则窗体,方法很多很多多.... 本文总结DebugLZQ认为简洁高效的几种方法 实现WPF不规则窗体的几种常用的方法如下: 1.使用Ble ...

  8. C#在透明窗体WinForm上面画图(电子尺小工具的实现)

    前几天要做一个微信调一调的外挂,里面用到了尺子测量距离,然后就自己下载了一个电子尺,最近要升级我的跳一跳外挂,然后就准备自己做一个电子尺,嵌入到我的外挂里面,在嵌入到我的外挂之前,我自己做了一个完整版 ...

  9. WPF 透明掩码 OpactiyMask

    原文:WPF 透明掩码 OpactiyMask 在WPF中提供了Opacity属性使得元素的所有内容都是透明的.而OpacityMask属性可以使元素的特定区域变成透明. OpacityMask属性接 ...

随机推荐

  1. Oracle 和 MySQL的区别(不完整)

    1. Oracle对单双引号要求的很死,一般不准用双引号,不然会报错.MySQL 单双引号都可以. 2. 事务提交方式:Oracle 默认手动提交,MySQL 默认自动提交. 3. 分页:MySQL有 ...

  2. 像素(PX)转其它长度单位(mm、cm...)

    今天想把px转成mm为单位,因像素跟其它单位比值的大小会跟屏幕设置的分辨率大小而不定,因此不能以固定的数值去计算. 解决方法是 页面上放一个高度为1mm的隐藏块 <div id="di ...

  3. Windows环境下为PHP5.6安装redis扩展和memcached扩展

    一.php安装redis扩展   1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本       2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...

  4. 1.Ehcache(01)——简介、基本操作

    转自:https://blog.csdn.net/w1014074794/article/details/51086228 Ehcache简介 目录 1       CacheManager 1.1  ...

  5. IrisBlur - 虹膜模糊

    [IrisBlur - 虹膜模糊] IrisBlur模拟人眼的虹膜,本质上是一个控制程度更高的FieldBlur,在FieldBlur的基础上暴露了更多的参数. Choose Filter >  ...

  6. java 数字金额转换中文金额

    public static String digitUppercase(double n){ String fraction[] = {"角", "分"}; S ...

  7. 在Linux下访问Windows共享文件夹

    说明 以下操作以Ubuntu为例,大家可以参考. 我在Ubuntu 14.04和16.04都试过了. Windows共享文件夹 如果局域网内有一台Windows主机,将指定文件夹设为共享,就可以在局域 ...

  8. 访问localhost的phpmyadmin出现访问被拒绝

    原因是:没有配置MySQL数据库密码. 1.打开D:\wamp\apps\phpmyadmin x.xx找到config.inc.php. 修改并保存: $cfg['Servers'][$i]['us ...

  9. redis 工具

    搜索关键词:redis 工具 推荐:http://redisdesktop.com/download 点评:使用顺手,顺畅. ★★★★★ 五星好评 备注:redis key 可以采用某种format ...

  10. swift学习之UIButton

    // //  ViewController.swift //  button // //  Created by su on 15/12/7. //  Copyright © 2015年 tian. ...