我想显示一个旋转90度的标签(所以我可以把它们放在表格的顶部作为标题)。是否有捷径可寻?如何在C#中旋转标签?
10
A
回答
22
您将需要编写自己的或使用自定义控件。
A The Code Project您可以从Customized Text - Orientated Controls in C# - Part I (Label Control)开始的文章。这包含额外的功能,所以你应该能够修剪它,如果你愿意。
这里是从它的一些代码,感兴趣:
/// <summary>
/// This is a lable, in which you can set the text in any direction/angle
/// </summary>
#region Orientation
//Orientation of the text
public enum Orientation
{
Circle,
Arc,
Rotate
}
public enum Direction
{
Clockwise,
AntiClockwise
}
#endregion
public class OrientedTextLabel : System.Windows.Forms.Label
{
#region Variables
private double rotationAngle;
private string text;
private Orientation textOrientation;
private Direction textDirection;
#endregion
#region Constructor
public OrientedTextLabel()
{
//Setting the initial condition.
rotationAngle = 0d;
textOrientation = Orientation.Rotate;
this.Size = new Size(105,12);
}
#endregion
#region Properties
[Description("Rotation Angle"),Category("Appearance")]
public double RotationAngle
{
get
{
return rotationAngle;
}
set
{
rotationAngle = value;
this.Invalidate();
}
}
[Description("Kind of Text Orientation"),Category("Appearance")]
public Orientation TextOrientation
{
get
{
return textOrientation;
}
set
{
textOrientation = value;
this.Invalidate();
}
}
[Description("Direction of the Text"),Category("Appearance")]
public Direction TextDirection
{
get
{
return textDirection;
}
set
{
textDirection = value;
this.Invalidate();
}
}
[Description("Display Text"),Category("Appearance")]
public override string Text
{
get
{
return text;
}
set
{
text = value;
this.Invalidate();
}
}
#endregion
#region Method
protected override void OnPaint(PaintEventArgs e)
{
Graphics graphics = e.Graphics;
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.Trimming = StringTrimming.None;
Brush textBrush = new SolidBrush(this.ForeColor);
//Getting the width and height of the text, which we are going to write
float width = graphics.MeasureString(text,this.Font).Width;
float height = graphics.MeasureString(text,this.Font).Height;
//The radius is set to 0.9 of the width or height, b'cos not to
//hide and part of the text at any stage
float radius = 0f;
if (ClientRectangle.Width<ClientRectangle.Height)
{
radius = ClientRectangle.Width *0.9f/2;
}
else
{
radius = ClientRectangle.Height *0.9f/2;
}
//Setting the text according to the selection
switch (textOrientation)
{
case Orientation.Arc:
{
//Arc angle must be get from the length of the text.
float arcAngle = (2*width/radius)/text.Length;
if(textDirection == Direction.Clockwise)
{
for (int i=0; i<text.Length; i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
(float)(radius*(1 - Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for (int i=0; i<text.Length; i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform((-90 - (float)rotationAngle - 180*arcAngle*i/(float)Math.PI));
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
break;
}
case Orientation.Circle:
{
if (textDirection == Direction.Clockwise)
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 - Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
else
{
for(int i=0;i<text.Length;i++)
{
graphics.TranslateTransform(
(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
(float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
graphics.RotateTransform(-90 - (float)rotationAngle - (360/text.Length)*i);
graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
graphics.ResetTransform();
}
}
break;
}
case Orientation.Rotate:
{
//For rotation, who about rotation?
double angle = (rotationAngle/180)*Math.PI;
graphics.TranslateTransform(
(ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
(ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
graphics.RotateTransform((float)rotationAngle);
graphics.DrawString(text,this.Font,textBrush,0,0);
graphics.ResetTransform();
break;
}
}
}
#endregion
}
6
您还可以看看Windows的ToolStrip控件。它有一个TextDirection选项,可以将其设置为Vertical90或Vertical270,这将使您的标签文本以适当的方向旋转。
概述:ToolStrip Class(MSDN)
短视频的使用:http://msdn.microsoft.com/en-us/vstudio/bb798042对于旋转看到3:05旋转特别。
相关问题
- 1. 如何在Vb net中旋转标签?
- 2. 如何在ggplot2中旋转轴标签?
- 3. C#图表旋转标签
- 4. 如何旋转标签
- 5. 在IB中旋转标签?
- 6. 旋转标签
- 7. 标签旋转
- 8. 旋转grid.xaxis中的标签
- 9. 在seaborn boxplot中旋转xtick标签?
- 10. 旋转的X标签
- 11. 旋转文本标签
- 12. ActionScript 3:旋转列标签
- 13. ggpairs旋转轴标签
- 14. 使UI标签旋转
- 15. IOS标签旋转手机
- 16. 如何在TH Table中使用CSS旋转()标签
- 17. 如何在JFreeChart中使用GroupedStackedBarRenderer时旋转组标签?
- 18. 如何旋转MapView,同时保持标签和标记(叠加层)不旋转?
- 19. 鼠标在c中旋转图像#
- 20. 如何在OpenGL C++中旋转旋转轴?
- 21. 旋转开关小标签在GGPLOT2 facet_grid
- 22. chartist js在x轴上旋转标签
- 23. 旋转饼图标签在dc.js饼图
- 24. R中的旋转轴标签
- 25. 我如何在C#中旋转图表
- 26. 如何在C#3D中旋转对象?
- 27. 如何在使用matplotlib直方图时旋转轴标签?
- 28. 如何在浮动圆柱轴上旋转刻度标签?
- 29. 如何在水平面上旋转x轴标签90度
- 30. 强制旋转目标-c
第二个链接似乎被打破。 – 2014-01-16 08:41:43