나는 90도 회전 된 레이블을 보여주고 싶다. 그래서 표제의 맨 위에 표제로 묶을 수있다. 이 작업을 수행하는 쉬운 방법이 있습니까?C#에서 레이블을 회전하는 방법은 무엇입니까?
10
A
답변
22
직접 작성하거나 사용자 지정 컨트롤을 사용해야합니다.
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
또한 윈도우의 ToolStrip 컨트롤에 좀 걸릴 수 있습니다 : 여기
그리고
는 관심의 그것에서 일부 코드입니다. 그것은 Vertical90 또는 Vertical270으로 설정 될 수있는 TextDirection을위한 옵션을 가지고 있으며 이것은 적절한 방향으로 라벨 텍스트를 회전시킬 것입니다.개요 : ToolStrip Class (MSDN)의 사용에
짧은 비디오 : http://msdn.microsoft.com/en-us/vstudio/bb798042 회전은 회전 3시 5분을 특별히 참조하십시오.
관련 문제
- 1. C에서 이미지와 함께 picturebox를 회전하는 방법은 무엇입니까?
- 2. MapView를 회전하는 방법은 무엇입니까?
- 3. UIImageView를 증분 회전하는 방법은 무엇입니까?
- 4. 속도계에서 이미지를 회전하는 방법은 무엇입니까?
- 5. GWT에서 위젯을 회전하는 방법은 무엇입니까?
- 6. Android에서 이미지를 회전하는 방법은 무엇입니까?
- 7. numpy 배열을 회전하는 방법은 무엇입니까?
- 8. wx.StaticText의 텍스트를 회전하는 방법은 무엇입니까?
- 9. TabBar로 앱을 회전하는 방법은 무엇입니까?
- 10. 보기로 정보 버튼을 회전하는 방법은 무엇입니까?
- 11. Tkinter Canvas에서 파이썬으로 폴리곤을 회전하는 방법은 무엇입니까?
- 12. 아파치 POI에서 HSSFCell의 텍스트를 회전하는 방법은 무엇입니까?
- 13. 사각형에 비해 사각형의 UIImage를 회전하는 방법은 무엇입니까?
- 14. OpenGL에서 특정 객체를 회전하는 방법은 무엇입니까?
- 15. 고정 점으로 UIIMageVIew를 회전하는 방법은 무엇입니까?
- 16. div HTML 레이어를 회전하는 방법은 무엇입니까?
- 17. 캔버스에 그려지는 이미지를 회전하는 방법은 무엇입니까?
- 18. System.Drawing.Image x degrees를 회전하는 방법은 무엇입니까?
- 19. 네트워크 인터페이스에서 IP를 회전하는 방법은 무엇입니까?
- 20. PHP를 사용하여 PDF 문서를 회전하는 방법은 무엇입니까?
- 21. jQuery Slider로도 (도) 이미지를 회전하는 방법은 무엇입니까?
- 22. CSS3에서 원점을 기준으로 회전하는 방법은 무엇입니까?
- 23. JFreeChart에서 GroupedStackedBarRenderer를 사용할 때 그룹 라벨을 회전하는 방법은 무엇입니까?
- 24. c에서 비밀번호를 마스크하는 방법은 무엇입니까? C에서
- 25. 테이블 안의 레이블을 숨기고 표시하는 방법은 무엇입니까?
- 26. UITableView 위에 로딩 레이블을 표시하는 방법은 무엇입니까?
- 27. Zweckform 4737 레이블을 인쇄하는 방법은 무엇입니까?
- 28. 레이블을 업데이트하여 새 텍스트를 표시하는 방법은 무엇입니까?
- 29. 일부 창에 일부 레이블을 배치하는 방법은 무엇입니까?
- 30. 이름없는 드라이브의 레이블을 표시하는 방법은 무엇입니까?
두 번째 링크가 손상된 것 같습니다. –
@PeterMortensen을 사용할 수 있습니다. –
+1은 매우 쉽습니다. – adam