让文字竖着显示

NealXX 2009-08-07 09:48:05
在WinForm程序中,怎样才能让Label的Text竖着显示,或者说让Label控件顺时针旋转90°
...全文
567 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
SQL77 2009-08-07
  • 打赏
  • 举报
回复
程序中用D\nA\n...
NealXX 2009-08-07
  • 打赏
  • 举报
回复
差不多了 谢谢
markhao 2009-08-07
  • 打赏
  • 举报
回复
13L是专家级人物?
markhao 2009-08-07
  • 打赏
  • 举报
回复
咋运行13L代码报14个错...
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
13L好快...
风之影子 2009-08-07
  • 打赏
  • 举报
回复
13楼的是旋转的自绘。

应该满足你需求了吧!
markhao 2009-08-07
  • 打赏
  • 举报
回复
都旋转...哈哈.
风之影子 2009-08-07
  • 打赏
  • 举报
回复
你想要什么旋转呢?
lovvver 2009-08-07
  • 打赏
  • 举报
回复

// ****************************************************************************
// Copyright Swordfish Computing 2003 **
// **
// Filename: Swordfish\Utilities\VerticalLabel.cs **
// Authored by: John Stewien of Swordfish Computing **
// Date: October 2003 **
// **
// - Change Log - **
// ****************************************************************************
// $Id: $

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace xxx
{
/// <summary>
/// This class is a Label control with vertical text
/// </summary>
public class VerticalLabel : System.Windows.Forms.Label
{
/// <summary>
/// The text normally has it's top on the right hand side. When
/// this is true, the text is drawn with it's top on the left hand
/// side.
/// </summary>
private bool flip180 = true;

/// <summary>
/// Contructor.
/// </summary>
public VerticalLabel()
{
}

/// <summary>
/// Overrides the OnPaint Method. Draws vertical text on the control.
/// </summary>
/// <param name="e"></param>
protected override void OnPaint(PaintEventArgs e)
{
// Get the graphics area to paint to
Graphics g = e.Graphics;

// Create rectangles for the vertical label, the horizontal label,
// and the corner where they meet
RectangleF rectangle = new RectangleF(
(float)this.ClientRectangle.X,
(float)this.ClientRectangle.Y,
(float)this.ClientRectangle.Width,
(float)this.ClientRectangle.Height);

// Set up text format
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;
stringFormat.Trimming = StringTrimming.None;
stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

// Set up the string alignment according to the TextAlign property.
switch(this.TextAlign)
{
// Align the text along the bottom
case ContentAlignment.BottomCenter:
goto case ContentAlignment.BottomRight;
case ContentAlignment.BottomLeft:
goto case ContentAlignment.BottomRight;
case ContentAlignment.BottomRight:
stringFormat.Alignment = flip180 ? StringAlignment.Near : StringAlignment.Far;
break;

// Align the text in the middle
case ContentAlignment.MiddleCenter:
goto case ContentAlignment.MiddleRight;
case ContentAlignment.MiddleLeft:
goto case ContentAlignment.MiddleRight;
case ContentAlignment.MiddleRight:
stringFormat.Alignment = StringAlignment.Center;
break;

// Align the text along the top
case ContentAlignment.TopCenter:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopLeft:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopRight:
stringFormat.Alignment = flip180 ? StringAlignment.Far : StringAlignment.Near;
break;
}

// Create brush for writing text
Brush textBrush = new SolidBrush(this.ForeColor);

// Get the width of the wrapped text

// Set the stringFormat for measuring the first character
CharacterRange[] characterRanges ={ new CharacterRange(0, Text.Length)};
stringFormat.SetMeasurableCharacterRanges(characterRanges);
Region[] stringRegions = new Region[1];
stringRegions = g.MeasureCharacterRanges(Text,Font,rectangle,stringFormat);
float textWidth = stringRegions[0].GetBounds(g).Right +2f;

// Calculate the xOffset required to align the text in accordance
// with the TextAlign property.
float xOffset = 0f;
switch(this.TextAlign)
{
// Align the text in the center
case ContentAlignment.BottomCenter:
goto case ContentAlignment.TopCenter;
case ContentAlignment.MiddleCenter:
goto case ContentAlignment.TopCenter;
case ContentAlignment.TopCenter:
xOffset = (rectangle.Width - textWidth)/2f;
break;

// Align the text on the left
case ContentAlignment.BottomLeft:
goto case ContentAlignment.TopLeft;
case ContentAlignment.MiddleLeft:
goto case ContentAlignment.TopLeft;
case ContentAlignment.TopLeft:
xOffset = flip180 ? rectangle.Width - textWidth : 0f;
break;

// Align the text on the right
case ContentAlignment.BottomRight:
goto case ContentAlignment.TopRight;
case ContentAlignment.MiddleRight:
goto case ContentAlignment.TopRight;
case ContentAlignment.TopRight:
xOffset = flip180 ? 0f : rectangle.Width - textWidth;
break;
}

// Store the current matrix
Matrix storedState = g.Transform;

// If we need to flip 180, then rotate 180, and then translate
// back onto the control area
if (flip180)
{
// Flip the view matrix 180 degrees
g.RotateTransform(180f);

// Translate to bring the text back to the rectangle.
// Remember that the translation is flipped 180 degrees
g.TranslateTransform(-rectangle.Width, -rectangle.Height);
}

// Add in the offset to align the text according to the TextAlign property
g.TranslateTransform(xOffset,0f);

// Draw the y axis label
g.DrawString(
this.Text,
this.Font,
textBrush,
rectangle,
stringFormat);

// Restore the view Transform
g.Transform = storedState;
}

/// <summary>
/// Gets/Sets whether the text should be flipped 180 degrees or not
/// </summary>
[Description("Whether the text should be flipped 180 degrees or not"),Category("Appearance")]
public bool Flip180
{
get
{
return flip180;
}
set
{
flip180 = value;
this.Invalidate();
}
}
}
}

markhao 2009-08-07
  • 打赏
  • 举报
回复
有没有思路?
NealXX 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 dancingbit 的回复:]
在Paint事件中,使用DrawString绘制文本。
[/Quote]
这个可行 能不能具体说下 谢谢
NealXX 2009-08-07
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 flyerwing 的回复:]
把宽度调到只有容纳一个字,高度加大。
[/Quote]
字没旋转。。


NealXX 2009-08-07
  • 打赏
  • 举报
回复
谢谢 俺去试试
flyerwing 2009-08-07
  • 打赏
  • 举报
回复
把宽度调到只有容纳一个字,高度加大。
NealXX 2009-08-07
  • 打赏
  • 举报
回复
不行的话 看看能不能自己画, 自己画太麻烦了
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
在Paint事件中,使用DrawString绘制文本。
pengalwin 2009-08-07
  • 打赏
  • 举报
回复
我也想实现这玩意
柳晛 2009-08-07
  • 打赏
  • 举报
回复
不知道label能不能重写。
NealXX 2009-08-07
  • 打赏
  • 举报
回复
字没旋转。。
dancingbit 2009-08-07
  • 打赏
  • 举报
回复
把宽度调到只有容纳一个字,高度加大。
加载更多回复(1)

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧