C#中给Label控件设置BackgroundImage属性
来源:互联网 发布:asp sql注入 编辑:程序博客网 时间:2024/05/29 01:55
在C#中,默认情况下是无法给Label设置BackgroundImage属性的,只有Image这个属性,但是在某些特殊的情况下我们又需要设置Label的BackgroundImage属性,因此我们必须对label控件加以改造。Label是继承自Control类的,而Control类是有BackgroundImage这个属性的,Label也有这个属性,只是在VS中我们无法看到而已,微软做了下处理,不希望我们在属性窗口中能够直接设置它。实际上它有很多属性在属性面板中没有显示而已。
因此我们可以对Label控件代码稍加改写即可,代码如下图所示,我们写个控件继承Label,重写它的2个方法即可。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace Tempus.Component
{
public partial class LabelEx2 : Label
{
public LabelEx2()
{
}
protected override void OnPaintBackground(PaintEventArgs e)
{
return;
}
protected override void OnPaint(PaintEventArgs e)
{
//判断BackGroundImage是否为空
if (this.BackgroundImage != null)
{
e.Graphics.DrawImage(this.BackgroundImage, new System.Drawing.Rectangle(0, 0, this.Width, this.Height),
this.Location.X, this.Location.Y, this.Width, this.Height,
System.Drawing.GraphicsUnit.Pixel);
}
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
SolidBrush drawBrush = new SolidBrush(this.ForeColor);
e.Graphics.DrawString(this.Text, this.Font, drawBrush, new System.Drawing.Rectangle(0, 0, this.Width, this.Height));
//base.OnPaint(e);
}
}
}
调用时设置这个Label控件的BackgroundImage属性即可,Demo代码如下:
lblWineInfo.BackgroundImage = Image.FromFile(strWineDetail1);
- C#中给Label控件设置BackgroundImage属性
- C#中给Label控件设置BackgroundImage属性
- C#中给LABEL控件设置BACKGROUNDIMAGE属性
- 设置GtkMenuItem控件中的Label控件属性
- C#控件属性设置
- c# WinForm中如何在代码中设置控件的padding 设置Label的字体
- c# WinForm中如何在代码中设置控件的padding 设置Label的字体
- 给用户控件设置属性
- AS3 中Label相关属性的设置
- iOS中label设置属性字符串
- 如何给一个Label设置不同的文字属性
- C#中线程中设置另一个线程的控件属性
- C#中Label设置为透明
- Label控件属性AssociatedControlID
- 如何动态设置label控件的字体颜色属性
- C#线程中使用委托方式设置控件属性
- c#中控件使用时属性设置记录集
- C# winform自定义Label控件使其能设置行距
- 找数问题和青蛙爬楼梯问题
- Zend_Db_Select
- string.Format() 字符格式化
- 你会做Web上的用户登录功能吗?
- FTP命令
- C#中给Label控件设置BackgroundImage属性
- camera 控制流程
- OGNL的简单使用
- myeclipse环境下快捷键ALT+/失效的解决办法
- linux升级内核,出现:mount:could not find filesystem‘/dev/root’的解决方法
- oracle锁表的问题
- redhat enterprise 5 下的boost::asio 库应用(4)
- windows 2008 修改密码规则
- java中遍历Map