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

8

本文介绍了如何在 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 窗体中翻转/旋转标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14