Label的透明问题(Up有分啊!!!)
我想在PictureBox上面放几个Label,让Label的背景色相对于PictureBox中的图象透明
问题:
Label的背景色不能设为透明
请问各位有什么解决的办法吗?(最好是高效一点的)
分不够可以加!谢谢!
问题点数:98、回复次数:48Top
1 楼net_lover(【孟子E章】)回复于 2004-09-01 17:03:10 得分 0
Label.BackColor=Color.Transparent;Top
2 楼net_lover(【孟子E章】)回复于 2004-09-01 17:05:38 得分 0
PictureBox1.SendToBack()
Label1.BackColor = Color.Transparent
Label1.Parent = PictureBox1
Label1.BringToFront()
Top
3 楼net_lover(【孟子E章】)回复于 2004-09-01 17:06:39 得分 18
PictureBox1.SendToBack();
Label1.BackColor = Color.Transparent;
Label1.Parent = PictureBox1;
Label1.BringToFront();
Top
4 楼net_lover(【孟子E章】)回复于 2004-09-01 17:10:44 得分 0
Or
Label1.BackColor = System.Drawing.Color.FromArgb(65, 204, 212, 230);Top
5 楼limeigui0725(你猜猜)回复于 2004-09-01 17:21:00 得分 10
PictureBox1.SendToBack();
Label1.BackColor = Color.Transparent;
Label1.Parent = PictureBox1;
Label1.BringToFront();Top
6 楼thinkc(【抗火、抗冰冻、抗闪电、抗毒、物理攻击无效】)回复于 2004-09-01 17:24:33 得分 2
呵呵,红星真耀眼呀Top
7 楼jimh(Jimmy)回复于 2004-09-01 17:29:41 得分 2
刚试过,Label.BackColor=Color.Transparent;是不行的,只有使用API了Top
8 楼show00(星星)回复于 2004-09-01 17:59:45 得分 0
行是行的通,但是效率太底了点吧,我如果有20个Label在上面,明显看的出刷新过程!!
有没有效率高点的方法啊
无论如何还是先谢谢各位的帮助!!Top
9 楼lhx1977(清水无鱼)回复于 2004-09-01 18:07:00 得分 3
Label1.BackColor = Color.Transparent
Label1.Parent = PictureBox1Top
10 楼RoadE()回复于 2004-09-01 18:14:13 得分 0
uP!Top
11 楼777kit(型死)回复于 2004-09-01 18:24:52 得分 3
Label1.BackColor = Color.Transparent
Label1.Parent = PictureBox1
Top
12 楼show00(星星)回复于 2004-09-01 18:35:49 得分 0
实在不行我就TextOut(),不知道行不行的通?Top
13 楼yanransoft()回复于 2004-09-01 18:36:16 得分 2
markTop
14 楼triout(笨牛)回复于 2004-09-01 18:40:02 得分 20
http://community.csdn.net/Expert/topic/3297/3297645.xml?temp=.4277307Top
15 楼triout(笨牛)回复于 2004-09-01 18:40:23 得分 0
这个问题是因为你把透明理解错了。
透明背景颜色(Transparent)说的不是把控件的背景设置为无色,只有背景颜色为无色时,控件的下层控件显示的颜色(或图片)才会成为当前控件的背景颜色的(让人感觉是透明的)。
在WINDOWS应用中,透明背景色指的是把控件的背景颜色设置为父控件的背景色。
因此,如果你想把控件的背景颜色设置为透明后,其背景色为其下层控件的显示的颜色,原则上是不可以的,但按照你的想法可以:
添加一个Panel控件,设置Panel控件的背景色或背景图片,把你说的LABEL或其他控件添加到这个PANEL中,作为这个PANEL的子控件运行。
Top
16 楼nnfzj(学习思归)回复于 2004-09-01 18:40:24 得分 5
Label.BackColor=Color.Transparent;
已经可以了啊!
也可以在属性里面改啊,效果是一样的。
怎么能不行呢?Top
17 楼buggavin(逸飞)回复于 2004-09-01 18:40:59 得分 2
没见过,帮你顶!Top
18 楼triout(笨牛)回复于 2004-09-01 18:42:27 得分 0
1——
增加一个PANEL
2——
设置PANEL的背景色/背景图片
3——
把LABEL添加为PANEL的子控件
4——
设置LABEL的背景色为透明。Top
19 楼show00(星星)回复于 2004-09-01 18:53:15 得分 0
谢谢triout(笨牛)
我等下回去试下看,谢谢你讲的原理,我知道必须用这一句Label1.Parent = PictureBox1
用Panel和PictureBox有区别吗?这样做的效率为什么不高,当Label多了的时候,重绘窗体很明显感觉到Label是在一个一个的被设置透明(即是我说的“很明显感觉到刷新”,我的机子比较差的!)
大家都可以试一下,用20个Label来实验一下!就知道了!
Top
20 楼triout(笨牛)回复于 2004-09-01 18:56:38 得分 0
PictureBox我不知道是否可以添加子控件,没有实验过。
可能你这种想法比较有新意,或者我年纪大点吧,喜欢墨守成规,通常都使用Panel作为容器控件的,所以从来不敢(因为不会)去想你的说法,把Label作为PictureBox的子控件。
呵呵,你实验后告诉我是否可以把Label作为PicturerBox的子控件吧,我就不实验了。Top
21 楼triout(笨牛)回复于 2004-09-01 18:58:08 得分 0
另外,孟子的代码
Label1.Parent = PictureBox1;
应该是可以吧,不过,我感觉,干吗不为PictureBox增加一个子控件Label呢?
PictureBox.Controls.Add(Label)Top
22 楼show00(星星)回复于 2004-09-01 19:10:03 得分 0
不要这样说啊,我刚用C#不久,很多东西都不太懂,我主要是因为要用PictureBox来处理图象,所以才选的这样做,我也没考虑其他的,Label是可以作为PictureBox的子控件的,而且两个效果差不多,我先那样说是真的以为他们在处理上有差别,可能会影响到效率,并没有其他意思。我只是想让程序在效率上有所提高,让它看上去运行快点!Anyway,谢谢你的帮助!!Top
23 楼triout(笨牛)回复于 2004-09-01 19:15:01 得分 0
呵呵,你多心了。
说实在的,在软件开发这行要的就是新意,敢想敢做才行,失败了也是有收获的。只是这些跟年纪有关,一来年轻,想象力丰富,二来年轻,有机会呀。Top
24 楼show00(星星)回复于 2004-09-01 19:16:05 得分 0
好象用PictureBox要快一点,Panel要慢一点,但不知道原因。
希望有知道的能为小弟解释一下!Top
25 楼triout(笨牛)回复于 2004-09-01 19:16:23 得分 0
不过,我看了孟子在1楼的回复就晕了,如果我仔细看了2楼的回复,也许我就不会再回复了。
呵呵。Top
26 楼laidycn(禅意生活)回复于 2004-09-01 19:17:16 得分 2
.............星真多!!Top
27 楼triout(笨牛)回复于 2004-09-01 19:17:26 得分 0
因为PANEL是把IMAGE作为背景的,而背景的处理本来就花时间,而PICTUREBOX是直接处理的,当然要快点。Top
28 楼Samen168(Code to coding)回复于 2004-09-01 19:21:22 得分 3
呵呵,要继承先 然后设置
this.SetStyle(ControlStyles.SupportsTransparentBackColor,true);
this.BackColor=Color.Transparent;Top
29 楼show00(星星)回复于 2004-09-01 19:21:32 得分 0
谢谢triout(笨牛)的解释
还是哪个问题,我想提高效率,真的就没有其他办法可以实现了吗?
期待……Top
30 楼hxhbluestar(贺星河)回复于 2004-09-01 19:40:33 得分 3
办法当然有
假如图片为pictureBox1,Label 为 label1
首先,在Form_Load()事件中加入这样一条:
//允许设置透明色
this.SetStyle( ControlStyles.SupportsTransparentBackColor,true);
//然后,再写以下语句
this.label1.BackColor = Color.Transparent;
this.pictureBox1.Controls.Add( this.label1);
this.label1.Location = new Point(0,0);//人为的将label1定位,可以由你自己设定
关键是要写this.SetStyle( ControlStyles.SupportsTransparentBackColor,true);
这一句
希望问题得到解决!
Top
31 楼triout(笨牛)回复于 2004-09-01 19:45:12 得分 0
解决这个问题的办法肯定是有的,比如你自己写一个控件等,但目前的办法是最简单的。Top
32 楼Samen168(Code to coding)回复于 2004-09-01 19:56:02 得分 3
hxhbluestar(贺星河) ( ) 信誉:97
关键是要写this.SetStyle( ControlStyles.SupportsTransparentBackColor,true);
这一句
你吹啊,的Form里写一百句Lable也不会透明Top
33 楼ilqtj(飞天)回复于 2004-09-01 20:13:58 得分 3
你们这些人误人子弟。
所谓透明是指对父窗体的透明。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace WindowsApplication10
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.IContainer components;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label Label1;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.button1 = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Red;
this.pictureBox1.Location = new System.Drawing.Point(48, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(360, 264);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(440, 168);
this.button1.Name = "button1";
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// label2
//
this.label2.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.label2.Location = new System.Drawing.Point(416, 64);
this.label2.Name = "label2";
this.label2.TabIndex = 2;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(536, 325);
this.Controls.Add(this.label2);
this.Controls.Add(this.button1);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
Label1=new Label();
Label1.Size = new System.Drawing.Size(300, 200);
Label1.Text="测试";
Label1.Font=new Font("宋体",20);
Label1.BorderStyle=System.Windows.Forms.BorderStyle.FixedSingle;
this.Label1.Location=new Point(0,0);
Label1.BackColor=Color.Transparent;
this.pictureBox1.Controls.Add(this.Label1);
}
private void button1_Click(object sender, System.EventArgs e)
{
}
}
}
Top
34 楼triout(笨牛)回复于 2004-09-01 20:17:51 得分 0
呵呵,楼上的,看了你的代码,感觉是“高明”许多哟。Top
35 楼show00(星星)回复于 2004-09-01 20:57:05 得分 0
感谢大家踊跃发言,但说来说去都还是这一种办法……
虽然实现是很简单,但关键是解决不了速度的问题,如果有其它办法,复杂点都可以,只要能解决速度和效率的问题
另外:
To :hxhbluestar(贺星河) ( )
其实不要this.SetStyle( ControlStyles.SupportsTransparentBackColor,true);
这句也可以
Anyway,再次谢谢大家,希望大家继续帮我想办法!!!!谢谢!!!Top
36 楼ilqtj(飞天)回复于 2004-09-02 08:27:45 得分 0
不好意思,没有看仔细下面的回答。
感觉不好办(原理咱不懂),无论是label还是自己写的控件,效率上没有什么提高。
你可以看看这个http://dev.csdn.net/article/28/28203.shtm;
虽然是用户控件,效率依然很低。
楼主是否可以考虑用vb、vc实现的原理来用vb、vc来做?
Top
37 楼wangdequan1024(紫夜)回复于 2004-09-02 08:38:10 得分 2
upTop
38 楼wnlovezxm(大垃圾人)回复于 2004-09-02 08:43:25 得分 3
不就想在图片上面写两个字吗,用什么lable啊,用画笔直接写就完了!想写哪写哪!Top
39 楼wnlovezxm(大垃圾人)回复于 2004-09-02 08:44:01 得分 0
要不浪费了pictruebox了!Top
40 楼cjzlxy(享受孤独)回复于 2004-09-02 08:46:43 得分 3
要想达到效果就不要去考虑什么效率了.没什么严重的问题.
Top
41 楼triout(笨牛)回复于 2004-09-02 08:47:20 得分 0
使用.NET就不要太在意运行效率的问题。因为.NET在效率上已经是不敢恭维的了,但比起现在计算机的能力来说,比起开发速度来说,我想牺牲一点效率问题是值得的,这就是.NET的魅力了。如果一定要考虑到效率(就这使用了PICTUREBOX作为PARENT后引起的效率降低的问题你都很在意的话),那么我建议你不要使用.NET来开发了。
诚然,一个好的产品应该是高效率的和健壮的,但鱼和熊掌难以得兼,奈何?所以你不应该在这方面强求或者吹毛求疵了,一来这个效率问题应该是专门搞研究的人或MS的研发人员考虑的,二来,我们只是使用,只要客户认可就行,作为一线产品的研发人员,能在底层方面有所建树当然是好的,但我们的精力却不在这儿,否则,一个产品一个月的时间,却在这方面花了15天,值得吗?
我的建议:本讨论就此打住!Top
42 楼haiwangstar(南河三(信为万事之本))回复于 2004-09-02 08:48:22 得分 5
楼主不能说一说要实现什么功能吗?
一定要用在PictureBox上放透明Label的方法实现,换个角度,也许有更好的方法。Top
43 楼hxhbluestar(贺星河)回复于 2004-09-02 08:51:10 得分 0
其实孟子一开始就把问题解决了,大家在无谓的继续讨论!!!Top
44 楼eflea(磊磊)回复于 2004-09-02 08:51:39 得分 2
upTop
45 楼1979xt(搞.NET三年多,现在有点迷惘.)回复于 2004-09-02 08:54:00 得分 2
是啊,再讨论分都不够分了^-^Top
46 楼show00(星星)回复于 2004-09-02 10:38:43 得分 0
谢谢各位,那么就到此结束吧!再次谢谢各位参与!!Top
47 楼yangqi0226(非洲)回复于 2004-11-09 19:10:35 得分 0
MARKTop
48 楼laohuchiren(桌子凳子)回复于 2005-06-17 13:22:20 得分 0
Label1.BackColor = Color.Transparent
Label1.Parent = PictureBox1
Top




