C# Winform 纵向文字标签

正样用C#在Winform做出纵向文字标签

目的

在Winform上面建立一个竖直的(纵向的)TextLabel。
Winform文字标签都是水平方向的,网上资料都是画上去,不管清除。 当你修改显示内容的时候会重叠在一起。 你也不能用上次文字背景色再写一遍清除,由于windows平滑边缘效果,使得边缘影子擦不干净。

方法

  • 建立一个类,实例化之后,用 System.Drawing.Graphics的方法在form上面画文本。
  • 实例保留Graphics对象,就可以用Clear方法清除写过的文本。
  • 流程:
    实例化对象 》 写文本 》 擦除文本 》写更新的文本
    由于制图对象在实例化中已经创建,方便的流程是在每次写文本之前清除文本

代码

实例化、调用显示

VerticalLabel _myVerticalLabel; //声明对象
//Form 实例化
public MYForm()
{
	...
	//实例化纵向标签
	_myVerticalLabel= new VerticalLabel(this);
	...
}

void MyMethod()
{
	...
	_myVerticalLabel.ClearDrawing();
    _myVerticalLabel.DrawVerticalString("My words", PositionX, PositionY, Color.Black);
    ...
}

对象类定义

  • 技巧1,纵向标签对象实例化的时候将Winform对象传给标签对象,不然你画不到form上。
  • 技巧2,保留formGraphics,这样才能够清除上次的文本
  • 改进, 你也可以吧Clear合并到Draw里面,每次先清除一下
    class VerticalLabel
    {
        System.Drawing.Graphics formGraphics;

        public VerticalLabel(Form targetForm)
        {
            formGraphics = targetForm.CreateGraphics();
        }

        public void DrawVerticalString(string drawString, float x, float y, Color color, int size = 9, string font = "Tahoma")
        {
            System.Drawing.Font drawFont = new System.Drawing.Font(font, size);
            System.Drawing.SolidBrush drawBrush = new System.Drawing.SolidBrush(color);

            System.Drawing.StringFormat drawFormat = new System.Drawing.StringFormat();
            drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
            formGraphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
            drawFont.Dispose();
            drawBrush.Dispose();
        }

        public void ClearDrawing()
        {
            formGraphics.Clear(SystemColors.Control);
        }
    }

效果

在这里插入图片描述

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在C# Winform中实现文字滚动效果,可以使用Label控件和Timer控件。首先,创建一个Label控件用于显示滚动的文字,然后添加一个Timer控件用于控制滚动速度。在Timer的Tick事件中,通过改变Label控件的Left属性的值来实现文字的滚动效果。具体的实现步骤如下: 1. 创建一个Winform项目,命名为Ex01_31,窗体默认为Form1。 2. 在窗体上添加一个Label控件用于显示消息,添加一个Button控件用于控制消息的运动,添加一个Timer控件用于控制滚动速度。 3. 在代码中,编写以下主要程序代码: ```csharp private void timer1_Tick(object sender, EventArgs e) { label1.Left -= 2; if (label1.Right < 0) { label1.Left = this.Width; } } private void button1_Click(object sender, EventArgs e) { timer1.Enabled = true; //开始滚动 } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; //停止滚动 } ``` 这段代码中,timer1_Tick事件中的代码用于控制文字的滚动速度。每次Timer触发Tick事件时,将Label控件的Left属性减去2,实现文字向左滚动的效果。当Label控件的Right属性小于0时,将Label控件的Left属性设置为窗体的宽度,使文字重新回到初始位置。button1_Click事件用于启动滚动,button2_Click事件用于停止滚动。 这样,当单击【演示】按钮时,文字就会开始滚动。单击【暂停】按钮可以停止滚动。 参考资料: \[1\] 引用\[1\] \[2\] 引用\[2\] \[3\] 引用\[3\] #### 引用[.reference_title] - *1* *2* [C# winform-窗体中的滚动字幕【案例+源码】](https://blog.csdn.net/m0_65636467/article/details/127869709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [winform实现文字滚动](https://blog.csdn.net/qq_39569480/article/details/127571034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值