如何通过在C#窗体中保持自动调整大小来旋转标签 [英] How to rotate a label by keeping autosize in C# windows forms

查看:115
本文介绍了如何通过在C#窗体中保持自动调整大小来旋转标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想通过按钮点击旋转标签

我发现这个代码

这里是一个newLabel类

  class  newLabel:System.Windows.Forms.Label 
{
public int RotateAngle { get ; set ; }
public string NewText { get ; set ; }
受保护 覆盖 void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
Brush b = new SolidBrush( .ForeColor);
e.Graphics.TranslateTransform( this .Width / 2 this .Height / 2 );
e.Graphics.RotateTransform( this .RotateAngle);
e.Graphics.DrawString( this .NewText, this .Font,b,0f, 0F);
base .OnPaint(e);
}
}



和标签的使用

 newlbl.AutoSize =  false ; 
newlbl.NewText = Heba< b;
newlbl.ForeColor = Color.Green;



我的问题是

 newlbl.AutoSize =  false ; 



我想要使autoSize = true但是在制作时标签会从表格中消失/>
我无法调整标签Font的大小,因为它没有完全显示,因为它有一个固定的高度和宽度

任何帮助?



我尝试了什么:



我尝试了上面提交的代码

解决方案

使用AutoSize时,它采用水平方向 - 并在此基础上自动调整大小。然后使用大小和水平文本绘制控件文本。但是,由于你已经将旋转/变换应用于它将绘制的图形对象,它会删除你的文本,并自己绘制它 - 以一个角度,这意味着它在实际控件的范围内不再可见。



如果你想要一个自动调整旋转控件,你将不得不完全转储Label并根据UserControl创建你自己的替代品,并通过MeasureString自己处理它。请注意,控件的大小必须随着旋转而改变,因为控件是矩形的:

 ____ 
| 1234 |
----

大约25像素高,75宽:

<前lang =text> _
| 4 |
| 3 |
| 2 |
| 1 |
-

大约75像素高,大约30像素(特别是字符旋转90度)


创建一个class

 newLable 

可以在你指定的任何角度旋转它的文字。



你可以使用它代码或只是从ToolBox拖动





 使用 System.Drawing; 

class newLabel:System.Windows.Forms.Label
{
public int RotateAngle { get ; set ; } // 旋转文字
public string NewText { get ; set ; } // 绘制文字
protected 覆盖 void OnPaint(System.Windows.Forms.PaintEventArgs e)
{
刷b = SolidBrush( .ForeColor);
e.Graphics.TranslateTransform( this .Width / 2 this .Height / 2 );
e.Graphics.RotateTransform( this .RotateAngle);
e.Graphics.DrawString( this .NewText, this .Font,b,0f, 0F);
base .OnPaint(e);
}
}





现在这个自定义控件用于你的表格。



你必须在下面设置属性



  1 。 nwlbl.Text =  ;  //  可以通过NewText属性更改 
2 。 nwlbl.AutoSize = false ; // 根据您的文字进行调整
3 。 nwlbl.NewText = Hello; // 您要显示的内容
4 。 nwlbl.ForeColor = Color.Red; // 要显示的颜色
5 。 nwlbl.RotateAngle = -90; // 旋转角度< / pre>


Hi,
I want to rotate a label by button click
I found this code
here is a newLabel class

class newLabel : System.Windows.Forms.Label
    {
        public int RotateAngle { get; set; }
        public string NewText { get; set; }
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            Brush b = new SolidBrush(this.ForeColor);
            e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
            e.Graphics.RotateTransform(this.RotateAngle);
            e.Graphics.DrawString(this.NewText, this.Font, b, 0f, 0f);
            base.OnPaint(e);
        }
    }


and that's use of label

newlbl.AutoSize = false;
newlbl.NewText = "Heba<b";
newlbl.ForeColor = Color.Green;


my problem is

newlbl.AutoSize = false;


I want To make autoSize = true but when make it the label disappear from a Form
and I can't resize label Font because it doesn't appear completely because it has a fixed height and width
any help ?

What I have tried:

I tried the code I submitted above

解决方案

When you use AutoSize, it assumes a horizontal orientation - and autosizes on that basis. It then Paints the control text using the size and horizontal text. But since you have applied a rotate / transform to the graphics object it will draw with, it erases your text, and draws it's own - at an angle which means it is no longer visible within the bounds of the actual control.

If you want an autosize, rotatable control, you are going to have to dump Label completely and create your own alternative based on a UserControl, and handle it all yourself, via MeasureString. Do note that the size of the control will have to change as the rotation does, as the control is rectangular:

 ____
|1234|
 ----

is around 25 pixel high, by 75 wide:

 _
|4|
|3|
|2|
|1|
 - 

Is probably about 75 pixels high, by 30 or so wide (especially is the characters are rotated though 90 degrees as well)


Create a class

newLable

which can rotate it's Text on any angle specified by you.

You can use it by code or simply dragging from ToolBox


using System.Drawing;

class newLabel:System.Windows.Forms.Label
{
    public int RotateAngle { get; set; }  // to rotate your text
    public string NewText { get; set; }   // to draw text
    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
        Brush b =new SolidBrush(this.ForeColor);           
        e.Graphics.TranslateTransform(this.Width / 2, this.Height / 2);
        e.Graphics.RotateTransform(this.RotateAngle);
        e.Graphics.DrawString(this.NewText, this.Font,b , 0f, 0f);
        base.OnPaint(e);
    }
}



Now this custom control is used into your form.

You have to set below properties

1. nwlbl.Text = "";             //which can be changed by NewText property
2. nwlbl.AutoSize = false;      // adjust according to your text
3. nwlbl.NewText = "Hello";     // whatever you want to display
4. nwlbl.ForeColor = Color.Red;  // color to display
5. nwlbl.RotateAngle = -90;     //angle to rotate</pre>


这篇关于如何通过在C#窗体中保持自动调整大小来旋转标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆