如何在打字时让文本框旋转 90(垂直)?
How to make textbox rotated 90 (vertical) during typing?
我需要制作一个像 excel 这样的垂直文本框。打字光标显示为垂直,如果我输入一些文本,文本将从上到下扩展。文本旋转 90 度。(不是溢出线)
如何在 WinForm 中创建垂直文本框?
你的问题让我很好奇!所以我试图实现一些东西。事实证明在 winforms
…
中做起来有点复杂
这是我的镜头,您需要对其进行改进,但它确实有效。这个想法是画一个白色背景来隐藏 TextBox
绘制的文本,然后垂直绘制它。
结果:
请注意,您可以使用另一种方法绘制文本来改进结果 — this code for example。
代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class VerticalTextBox : TextBox
{
private bool bFlip = true;
public VerticalTextBox()
{
Multiline = true;
SetStyle(ControlStyles.UserPaint, true);
TextChanged += OnTextChanged;
}
private void OnTextChanged(object sender, EventArgs eventArgs)
{
Invalidate(); // repaint all
}
protected override void OnPaintBackground(PaintEventArgs e)
{
var g = e.Graphics;
g.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
var stringFormat = new StringFormat {
Alignment = StringAlignment.Center,
Trimming = StringTrimming.None,
FormatFlags = StringFormatFlags.DirectionVertical
};
Brush textBrush = new SolidBrush(ForeColor);
var storedState = g.Transform;
if (bFlip)
{
g.RotateTransform(180f);
g.TranslateTransform(-ClientRectangle.Width,-ClientRectangle.Height);
}
g.DrawString(
Text,
Font,
textBrush,
ClientRectangle,
stringFormat);
g.Transform = storedState;
}
[Description("When this parameter is true the VLabel flips at 180 degrees."),Category("Appearance")]
public bool Flip180
{
get => bFlip;
set
{
bFlip = value;
Invalidate();
}
}
}
参考资料
VerticalLabel
SetStyle告诉TextBox调用Paint事件。
更好(?)的选择
为了获得更好的效果,在winforms中,推荐大家学习一下FastColoredTextBox并使用
迁移到 WPF,apparently is easy。
我需要制作一个像 excel 这样的垂直文本框。打字光标显示为垂直,如果我输入一些文本,文本将从上到下扩展。文本旋转 90 度。(不是溢出线)
如何在 WinForm 中创建垂直文本框?
你的问题让我很好奇!所以我试图实现一些东西。事实证明在 winforms
…
这是我的镜头,您需要对其进行改进,但它确实有效。这个想法是画一个白色背景来隐藏 TextBox
绘制的文本,然后垂直绘制它。
结果:
请注意,您可以使用另一种方法绘制文本来改进结果 — this code for example。
代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
public class VerticalTextBox : TextBox
{
private bool bFlip = true;
public VerticalTextBox()
{
Multiline = true;
SetStyle(ControlStyles.UserPaint, true);
TextChanged += OnTextChanged;
}
private void OnTextChanged(object sender, EventArgs eventArgs)
{
Invalidate(); // repaint all
}
protected override void OnPaintBackground(PaintEventArgs e)
{
var g = e.Graphics;
g.FillRectangle(new SolidBrush(BackColor), ClientRectangle);
var stringFormat = new StringFormat {
Alignment = StringAlignment.Center,
Trimming = StringTrimming.None,
FormatFlags = StringFormatFlags.DirectionVertical
};
Brush textBrush = new SolidBrush(ForeColor);
var storedState = g.Transform;
if (bFlip)
{
g.RotateTransform(180f);
g.TranslateTransform(-ClientRectangle.Width,-ClientRectangle.Height);
}
g.DrawString(
Text,
Font,
textBrush,
ClientRectangle,
stringFormat);
g.Transform = storedState;
}
[Description("When this parameter is true the VLabel flips at 180 degrees."),Category("Appearance")]
public bool Flip180
{
get => bFlip;
set
{
bFlip = value;
Invalidate();
}
}
}
参考资料
VerticalLabel
SetStyle告诉TextBox调用Paint事件。
更好(?)的选择
为了获得更好的效果,在winforms中,推荐大家学习一下FastColoredTextBox并使用
迁移到 WPF,apparently is easy。