WPF Adorner 弹出式工具栏 例子
源于MSDN 一个问题。
问:如何做出类似word的文字选中后工具栏弹出和动画效果。
我用的是adorner,其实用popup也是可以的。
效果图:

中间黑色部分代表真正的工具栏。
xaml代码:
<Window x:Class="ADO_TOOL.MainWindow"
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:ADO_TOOL"
mc:Ignorable="d"
Title="MainWindow" Height="" Width=""> <Grid x:Name="adohost">
<RichTextBox LostKeyboardFocus="RTB_LostKeyboardFocus" VerticalAlignment="Center" x:Name="RTB" PreviewMouseLeftButtonUp="RichTextBox_PreviewMouseLeftButtonUp" >
<FlowDocument >
<Paragraph >
<Run Text="测试显示tool"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
</Grid>
</Window>
adoner代码类
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media; namespace ADO_TOOL
{
public class ado_Gird : Adorner
{ private VisualCollection collection; protected override int VisualChildrenCount => collection.Count; protected override Visual GetVisualChild(int index) => collection[index]; protected override Size MeasureOverride(Size constraint) => base.MeasureOverride(constraint); protected override Size ArrangeOverride(Size finalSize)
{ _gird.Arrange(new Rect(finalSize)); var f = host as FrameworkElement; _gird.Margin = new Thickness(left, -top-_gird.Height,,); return base.ArrangeOverride(finalSize); }
private Grid _gird; private UIElement host; private double left;
private double top; public ado_Gird(UIElement adornedElement,double left,double top) :this(adornedElement)
{
this.left= left;
this.top = top;
} //h1是外部grid-内部grid的平均高度
//h2是外部grid-内部grid的平均宽度
readonly int h1 = ,h2=;
public ado_Gird(UIElement adornedElement) : base(adornedElement)
{
collection = new VisualCollection(this);
host = adornedElement;
this._gird = new Grid(); _gird.Height = ; _gird.Width = ; _gird.HorizontalAlignment = HorizontalAlignment.Left; _gird.Background = new SolidColorBrush(Colors.Red); Grid g2 = new Grid(); g2.Height = ; g2.Width = ; g2.Background = new SolidColorBrush(Colors.Black); _gird.Children.Add(g2); _gird.MouseMove += _gird_MouseMove; _gird.MouseLeave += _gird_MouseLeave; _gird.Opacity = 0.5; collection.Add(_gird);
} private void _gird_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
_gird.Opacity = 0.1;
} private void _gird_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
var point = e.GetPosition(_gird);
var f = host as FrameworkElement;
if (_gird.Width - point.X <= h2)
{
_gird.Opacity = -((h2 -( _gird.Width - point.X)) / h2);
}
if(_gird.Height - point.Y <= h1)
{
_gird.Opacity = - ((h1 - (_gird.Height - point.Y)) / h1);
}
if (point.X>&&point.X<=h2)
{
_gird.Opacity = -((h2 - point.X) / h2);
}
if (point.Y > && point.Y <= h1)
{
_gird.Opacity = - (h1 - point.Y) / h1;
} }
}
}
xaml.cs页面代码:
AdornerLayer layer;
private void RichTextBox_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
var point = e.GetPosition(RTB);
if (layer != null)
{
var b = layer.GetAdorners(adohost);
if(b!=null)
if(b.Count()>)
layer.Remove(b[]);
}
layer = AdornerLayer.GetAdornerLayer(adohost);
var ado = new ado_Gird(adohost, point.X,RTB.ActualHeight);
layer.Add(ado);
} private void RTB_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{ if (layer != null)
{
var b = layer.GetAdorners(adohost);
if (b != null)
if (b.Count() > )
layer.Remove(b[]); ;
}
}
WPF Adorner 弹出式工具栏 例子的更多相关文章
- PropertyGrid—为复杂属性提供下拉式编辑框和弹出式编辑框
零.引言 PropertyGrid中我们经常看到一些下拉式的编辑方式(Color属性)和弹出式编辑框(字体),这些都是为一些复杂的属性提供的编辑方式,本文主要说明如何实现这样的编辑方式. 一.为属性提 ...
- ZH奶酪:Ionic中(弹出式窗口)的$ionicModal使用方法
Ionic中[弹出式窗口]有两种(如下图所示),$ionicModal和$ionicPopup; $ionicModal是完整的页面: $ionicPopup是(Dialog)对话框样式的,直接用Ja ...
- Operating System-Thread(5)弹出式线程&&使单线程代码多线程化会产生那些问题
本文主要内容 弹出式线程(Pop-up threads) 使单线程代码多线程化会产生那些问题 一.弹出式线程(Pop-up threads) 以在一个http到达之后一个Service的处理为例子来介 ...
- 【转】PyQt弹出式对话框的常用方法及标准按钮类型
pyQt之弹出式对话框(QMessageBox)的常用方法及标准按钮类型 一.控件说明 QMessageBox是一种通用的弹出式对话框,用于显示消息,允许用户通过单击不同的标准按钮对消息进行反馈,且每 ...
- web全栈开发之网站开发二(弹出式登录注册框前端实现-类腾讯)
这次给大家分享的是目前很多网站中流行的弹出式登录框,如下面的腾讯网登录界面,采用弹出式登录的好处是大大提升了网站的用户体验和交互性,用户不用重新跳转到指定的页面就能登录,非常方便 先来个演示地址 要实 ...
- web开发实战--弹出式富文本编辑器的实现思路和踩过的坑
前言: 和弟弟合作, 一起整了个智慧屋的小web站点, 里面包含了很多经典的智力和推理题. 其实该站点从技术层面来分析的话, 也算一个信息发布站点. 因此在该网站的后台运营中, 富文本的编辑器显得尤为 ...
- asp.net 弹出式日历控件 选择日期 Calendar控件
原文地址:asp.net 弹出式日历控件 选择日期 Calendar控件 作者:逸苡 html代码: <%@ Page Language="C#" CodeFile=&quo ...
- php弹出式登录窗口并获得登录后返回值
一款bootstrap样式结合php制作的弹出式登录窗口,输入用户名和密码后,ajax传参给后台,并获得登录后返回值. hwLayer+ajax弹出登录框 $(function() { $('#for ...
- 让小区运营再智能一点,EasyRadius正式向WayOs用户提供到期弹出式提示充值页面
其实一直没向用户提供到期弹出式页面,主要是给VIP群的用户一点优越感,随着这次EasyRadius的更新,海哥就免费向普通easyRadius用户提供这两个模板下载. 有些人会问,什么样的模板.有什么 ...
随机推荐
- 前端开发HTML5——函数
20.函数的调用方式(内容尚未完全,可自行百度这方面的知识) call().apply().bind() 这三个方法的作用都是改变执行环境中的this指针的指向 call()与apply() 这两 ...
- grep日志去重
grep --text ' ERROR '2017.06.08.log | grep '12345678' | grep -Eo 'telephone=.*{11},p'| sort | uniq | ...
- c# 第13节 迭代语句、while、do...while、for、foreach、goto
本节内容: 1:迭代语句是什么 2:迭代语句之while 3:迭代语句之 do...while 4:迭代语句之for 5:迭代语句之foreach: 6:跳转语句break.continue.retu ...
- 201871010112-梁丽珍《面向对象程序设计(java)》第十周学习总结
项目 内容 这个作业属于哪个课程 https://www.cnblogs.com/nwnu-daizh/ 这个作业的要求在哪里 https://www.cnblogs.com/nwnu-daizh/p ...
- socket_http
socket_http import socket from urllib.parse import urlparse import time def get_url(url): # 通过socket ...
- Ubuntu下apache2安装配置(内含数字证书配置)
Ubuntu下apache2安装配置(内含数字证书配置)安装命令:sudo apt-get updatesudo apt-get install apache2 配置1.查看apache2安装目录命令 ...
- http协议里定义的四种常见数据的post方法
原文 https://blog.csdn.net/charlene0824/article/details/51199292 关于http协议里定义的四种常见数据的post方法,分别是: applic ...
- openjdk11 stretch基础镜像无法找到对应openjdk dbg 包的问题
今天在构建一个jdk perf 工具基于openjdk 11 发现8 的dbg 一直可以查找到,但是11的就是没有 参考issue https://github.com/docker-library/ ...
- 用NABCD法提出靠谱的项目建议
在项目中提出靠谱的建议,一方面能提高项目成功的概率,另一方面锻炼自己能力提升自己在项目中的影响力,所以我们要尽可能抓住机会在项目中提建议.那要如何提出靠谱的建议呢?从需求出发,明确做法和好处,分析竞争 ...
- Linux网络基础设置
1.笔记 systenctl restart network:重启网卡网络服务 bash:刷新主机名称 netstat:查看网络状态 route -n:不做地址解析 mii-tool eno16777 ...