如何在 C#/Windows 窗体中翻转/旋转标签? [英] How can I flip/rotate the label in C#/Windows Forms?

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

问题描述

如何在 C# Windows 窗体中翻转/旋转标签?

How can I flip/rotate the label in C# Windows Forms?

我将背景图片设置为我的标签.

I set the background image to my label.

在每个时间间隔,它都会向右移动三个像素.当它到达表单结束位置时,我需要将标签翻转并返回.

At every time interval it moves three pixels to the right side. When it reaches the form end position I need the label to be flipped and turned back.

我尝试了以下方法,但没有得到解决方案.

I have tried the following way, but I didn't get the solution.

private void timer1_Tick(object sender, EventArgs e){

    if (label2.Location.X < this.Width)
        label2.Location = new Point(label2.Location.X + incr, label2.Location.Y);
    else
    {
        incr = -2;
        label2.Location = new Point(label2.Location.X - 50, label2.Location.Y);
        label1.Image.RotateFlip();
    }
    this.Refresh();
}

推荐答案

创建一个类,newlabel,它可以按用户指定的任意角度旋转它的Text.

Create a class, newlabel, which can rotate its Text on any angle specified by the user.

extend label class& override paint method

您可以通过代码使用它,也可以简单地从工具箱中拖动它.

You can use it by code or simply dragging from the ToolBox.

using System.Drawing;

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);
    }
}

现在将要使用的自定义控件拖到表单中.

Now drag this custom control to be used into your form.

您必须设置以下属性.

newlbl.Text = "";           
newlbl.AutoSize = false;      
newlbl.NewText = "ravindra";     
newlbl.ForeColor = Color.Green;  
newlbl.RotateAngle = -90; 

只需更改 RotateAngle 属性即可根据需要更改角度.

Change angle as required by simply changing the RotateAngle property.

这篇关于如何在 C#/Windows 窗体中翻转/旋转标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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