当前位置: 首页 > 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.jsqmd.com/news/447/

相关文章:

  • 新建Vue3项目流程
  • G. Chimpanzini Bananini
  • 深入解析:HSA35NV001美光固态闪存NQ482NQ470
  • ERP和MES、WMS、CRM,到底怎么配合 - 智慧园区
  • YOLO实战应用 1YOLOv5 架构与模块
  • YOLO实战应用 2数据准备与增强
  • Day18稀疏数组
  • 底层
  • YOLO实战应用 3训练与优化策略
  • WPF 视图缩略图控件(支持缩放调节与拖拽定位)
  • 实用指南:Dify关联Ollama
  • ik中文分词器使用
  • 动态水印也能去除?ProPainter一键视频抠图整合包下载
  • SpringBoot整合RustFS:全方位优化文件上传性能
  • javaScript(WebAPI) - 教程
  • windows使用es-client插件
  • AI学习日记 - 实践
  • es中的端点
  • 解码C语言宏
  • es中的索引
  • es中的数据类型
  • 防御安全播客第214期:数据泄露与漏洞攻防实战
  • windows使用kibana
  • VIVADO的IP核 DDS快速采用——生成正弦波,线性调频波
  • 03作业
  • 软工作业个人项目
  • rapidxml中接口函数
  • YOLO进阶提升 6模型训练与测试
  • 深入解析:C语言---判断语句
  • vue基于Springboot框架网上电子书店商城好书推荐管理系统 - 教程