我想顯示一個旋轉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. 在GWT中旋轉標籤
- 7. 如何翻轉/旋轉C#/ Windows窗體中的標籤?
- 8. 如何在C#中旋轉GMap.NET標記
- 9. 旋轉標籤
- 10. 旋轉grid.xaxis中的標籤
- 11. 旋轉軸標籤在jqplot
- 12. 如何旋轉dygraph x標籤
- 13. 在seaborn boxplot中旋轉xtick標籤?
- 14. 如何在谷歌圖表中旋轉標籤?
- 15. 如何在ms圖表中旋轉特定標籤?
- 16. 如何在JFreeChart中使用GroupedStackedBarRenderer時旋轉組標籤?
- 17. 如何在TH Table中使用CSS旋轉()標籤
- 18. 如何在此jqplot腳本中旋轉x軸標籤
- 19. 如何在nvd3圖表中旋轉到Y軸標籤
- 20. 如何在R的twoord.plot中旋轉Y軸標籤?
- 21. Xamarin Forms XAML標籤旋轉
- 22. IOS標籤旋轉手機
- 23. 使UI標籤旋轉
- 24. 旋轉面板和標籤
- 25. ggpairs旋轉軸標籤
- 26. 旋轉的X標籤
- 27. 旋轉文本標籤
- 28. Javafx旋轉標籤問題
- 29. ActionScript 3:旋轉列標籤
- 30. 如何旋轉MapView,同時保持標籤和標記(疊加層)不旋轉?
第二個鏈接似乎被打破。 – 2014-01-16 08:41:43