如何在PictureBox上透明的显示文字1(利用控件技术)
关键点有2点:
1、将此控件的背景颜色设为透明色,
2、将此控件和父控件关联,即和PictureBox控件相关联。
范例代码如下:(关键性代码已用黑体字表示)
范例功能:当鼠标在图片上按下时,显示“你好”,当鼠标抬起后,文字自动消失。
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![]()
9
namespace CCDTest
10
{
11
public partial class Form1 : Form
12
{
13
string filename;
14
Label lblResult;
15
public Form1()
16
{
17
InitializeComponent();
18
filename = Application.StartupPath + "\\cc1.bmp";
19
lblResult = new Label();
20
lblResult.Location = new Point(0,0);
21
lblResult.Parent = pbCCD;
22
lblResult.BackColor = Color.Transparent;
23
lblResult.Text = "你好!";
24
lblResult.Visible = false;
25
}
26![]()
27
private void Form1_Load(object sender, EventArgs e)
28
{
29
pbCCD.Load(filename);
30
}
31![]()
32
private void pbCCD_MouseDown(object sender, MouseEventArgs e)
33
{
34
lblResult.Visible = true;
35
36
lblResult.Location = e.Location;
37
}
38![]()
39
private void pbCCD_MouseUp(object sender, MouseEventArgs e)
40
{
41
lblResult.Visible = false;
42
}
43
}
44
}
45![]()
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace CCDTest10
{11
public partial class Form1 : Form12
{13
string filename;14
Label lblResult;15
public Form1()16
{17
InitializeComponent();18
filename = Application.StartupPath + "\\cc1.bmp";19
lblResult = new Label();20
lblResult.Location = new Point(0,0);21
lblResult.Parent = pbCCD;22
lblResult.BackColor = Color.Transparent;23
lblResult.Text = "你好!";24
lblResult.Visible = false;25
}26

27
private void Form1_Load(object sender, EventArgs e)28
{29
pbCCD.Load(filename);30
}31

32
private void pbCCD_MouseDown(object sender, MouseEventArgs e)33
{34
lblResult.Visible = true;35
36
lblResult.Location = e.Location;37
}38

39
private void pbCCD_MouseUp(object sender, MouseEventArgs e)40
{41
lblResult.Visible = false;42
}43
}44
}45



浙公网安备 33010602011771号