【问题标题】:Draw and Clear String over a Control C#在控件 C# 上绘制和清除字符串
【发布时间】:2011-06-30 09:23:03
【问题描述】:

在 Google 上进行了一些搜索后,我找到了一些示例,但没有一个提供了我需要的内容。

我需要在ButtonClick 上的 WinForm 中将字符串 (WriteString()) 写入控件,并且我需要更新该绘图,因为我正在尝试将日期写入控件,即系统日期。

因此,每次用户单击该按钮时,DateTime.Now.ToString(); 都应该被绘制到控件中。

最佳

【问题讨论】:

    标签: c# winforms controls drawstring


    【解决方案1】:

    draw a string on label

    这个网址一定能帮到你

    那里写的代码

    void Label_OnPaint(object sender, PaintEventArgs e) {
      base.OnPaint(e);
      Label lbl = sender as Label;
      if (lbl != null) {
        string Text = lbl.Text;
        e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
        if (myShowShadow) { // draw the shadow first!
          e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(myShadowColor), myShadowOffset, StringFormat.GenericDefault);
        }
        e.Graphics.DrawString(Text, lbl.Font, new SolidBrush(lbl.ForeColor), 0, 0, StringFormat.GenericDefault);
      }
    }
    

    【讨论】:

    • 好的,它可以与 Label 一起使用,也可以与我的 Control 一起使用,但更新的唯一方法是 Control.Hide();比 Control.Show();
    【解决方案2】:

    您应该考虑为此使用 winforms LabelTimer

    【讨论】:

    • 是的,但是如果我在视频播放器控件中使用标签,则标签显示的是“背景”,我需要它透明(背景)。
    • 您可以尝试将标签 background 颜色设置为 Transparent 但这可能会或可能不会起作用,具体取决于您渲染视频的方式
    【解决方案3】:

    或者您可以修改控件的 OnPaint 方法以覆盖控件的绘制方式。 Graphics 对象中有一个方法可以让您编写字符串 g.DrawString

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 2013-05-11
    相关资源
    最近更新 更多