当前位置: 首页 > news >正文

WPF 字符竖向排列的排版格式(直排)表明控件

WPF 字符竖向排列的排版格式(直排)显示控件

自定义控件,继承自Label,重写OnRender方法,使用DrawingContext手动绘制文本。自动换列。

<local:VerticalTextBlock Width="280"Height="200"HorizontalAlignment="Left"Background="#555C8B00"Content="床前明月光疑是地上霜举头望明月低头思故乡"FontFamily="宋体"FontSize="40"FontWeight="Bold"Foreground="Black"IsReverse="False" />

C#代码:

public class VerticalTextBlock
: Label
{
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
int charsPerColumn = (int)(ActualWidth / FontSize);
int charsPerRow = (int)(ActualHeight / FontSize);
if (charsPerColumn == 0) charsPerColumn = 1;
for (int i = 0; i < Content.ToString().Length; i++)
{
var formattedText = new FormattedText(
Content.ToString()[i].ToString(),
CultureInfo.CurrentUICulture,
FlowDirection,
new Typeface(FontFamily, FontStyle = FontStyle, FontWeight = FontWeight, FontStretch = FontStretch),
FontSize,
Foreground, 1.25);
int column = 0;
int row = 0;
if (charsPerRow != 0)
{
column = i / charsPerRow;
row = i % charsPerRow;
}
if (!IsReverse)
{
column = charsPerColumn - 1 - column;
}
drawingContext.DrawText(formattedText, new Point(column * FontSize, row * FontSize));
}
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
InvalidateVisual();
}
public bool IsReverse
{
get {
return (bool)GetValue(IsReverseProperty);
}
set {
SetValue(IsReverseProperty, value);
}
}
public static readonly DependencyProperty IsReverseProperty =
DependencyProperty.Register("IsReverse", typeof(bool), typeof(VerticalTextBlock), new PropertyMetadata(false, OnIsReverseChanged));
private static void OnIsReverseChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as VerticalTextBlock;
if (control != null)
{
control.InvalidateVisual();
}
}
}

xaml代码:

<Style TargetType="local:VerticalTextBlock"><Setter Property="Template"><Setter.Value><ControlTemplate TargetType="local:VerticalTextBlock"><Border Padding="{TemplateBinding Padding}"Background="{TemplateBinding Background}"BorderBrush="{TemplateBinding BorderBrush}"BorderThickness="{TemplateBinding BorderThickness}" /></ControlTemplate></Setter.Value></Setter>
</Style>
http://www.zskr.cn/news/8490.html

相关文章:

  • 深入解析:HSA35NV001美光固态闪存NQ482NQ470
  • YOLO实战应用 1YOLOv5 架构与模块
  • SpringBoot整合RustFS:全方位优化文件上传性能
  • windows使用es-client插件
  • AI学习日记 - 实践
  • es中的索引
  • VIVADO的IP核 DDS快速采用——生成正弦波,线性调频波
  • 深入解析:C语言---判断语句
  • YOLO进阶提升 4训练准备与数据处理
  • YOLO进阶提升 5标注与配置
  • 【学术会议前沿信息|科研必备】IEEE/EI/Scopus三检护航!人工智能+自动化控制+人文社科+遥感+地理信息+视觉领域国际会议征稿启动,硕博生速来! - 教程
  • YOLO进阶提升 3YOLOv4 改进
  • 深入解析:数据库入门实战版
  • C# Avalonia 15- Animation- AnimationPlayerTest
  • JSONArray集合根据某个字段查询对象
  • 完整教程:Qt开发经验 --- qmake执行系统命令(15)
  • 13. LangChain4j + 加入检索增加生成 RAG(知识库) - Rainbow
  • CentOS 7 源码版 PhpMyAdmin 安装指南(适配 Nginx+PHP-FPM 环境) - 教程
  • AI智能体服务优秀的平台架构设计
  • 深入解析:YARN架构解析:深入理解Hadoop资源管理核心
  • JBoltAI:破解Java企业级AI应用落地难题的利器
  • Day04 C:\Users\Lenovo\Desktop\note\code\JavaSE\Basic\src\com\David\operator Demo01-08+Doc
  • springboot创建请求处理 - 指南
  • Mapper.xml与数据库进行映射的sql语言注意事项
  • 深入解析:人工智能学习:什么是LSTM模型
  • RabbitMQ 幂等性, 顺序性 和 消息积压 - 详解
  • resultMap和自定义映射结果形式(ResultMapManage)以及ResultMap Vs ResultType
  • 嵌入式设备不能正常上网问题
  • 2、论文固定模板(背景过度结尾)
  • gin: 静态文件