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

How can I flip/rotate the label in C#/Windows Forms?(如何在 C#/Windows 窗体中翻转/旋转标签?)
本文介绍了如何在 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 窗体中翻转/旋转标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)