如何设置单词的背景色? [英] How to set the background color of a word?

查看:157
本文介绍了如何设置单词的背景色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在窗体中有一个pictureBox.在该图片框的中间,我放置了所选照片的​​名称. 现在,我要为所选名称的背景上色.

I have a pictureBox in a Form. In the middle of that pictureBox, I've placed the name of a chosen photo. Now, I want to color the background of that chosen name.

我该怎么办?

推荐答案

我不确定您的意思,但是PictureBox的内容是图像.如果只想显示文本,请使用标签.如果希望它具有特定的背景色,请将其BackColor属性设置为所需的颜色.

I'm not sure what you mean, but a PictureBox's content is an image. If you want to simply display text, use a Label. If you want it to have a specific background color, set its BackColor property to the color that you want.

示例:

private void Form1_Load(object sender, EventArgs e)
{
    var label = new Label {BackColor = Color.White};
    Controls.Add(label);
}

我允许自己重复使用上面的Sampath示例的一部分,以使其适应用户的评论.

I allowed myself to re-use part of Sampath's example above to adapt it to the user's comment.

void pictureBox1_Paint(object sender, PaintEventArgs e)
{
    using (var font = new Font("Arial", 14))
    {
        const string pictureName = "Picture.jpg";
        var textPosition = new Point(10, 10);
        //Drawing logic begins here.
        var size = e.Graphics.MeasureString(pictureName, font);
        var rect = new RectangleF(textPosition.X, textPosition.Y, size.Width, size.Height);
        //Filling a rectangle before drawing the string.
        e.Graphics.FillRectangle(Brushes.Red, rect);
        e.Graphics.DrawString(pictureName, font, Brushes.Green, textPosition);
    }
}

这篇关于如何设置单词的背景色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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