在pictureBox的Paint事件中寫下如下代碼
private void pictureBox1_Paint(object sender, PaintEventArgs e)
或者在載入頁面的時(shí)候在LOAD中寫入下面代碼:
pictureBox1.SendToBack();
關(guān)鍵點(diǎn)有2點(diǎn):
1、將此控件的背景顏色設(shè)為透明色,
2、將此控件和父控件關(guān)聯(lián),即和PictureBox控件相關(guān)聯(lián)。
范例代碼如下:(關(guān)鍵性代碼已用黑體字表示)
范例功能:當(dāng)鼠標(biāo)在圖片上按下時(shí),顯示“你好”,當(dāng)鼠標(biāo)抬起后,文字自動(dòng)消失。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace CCDTest
{
public partial class Form1 : Form
{
string filename;
Label lblResult;
public Form1()
{
InitializeComponent();
filename = Application.StartupPath + "\\cc1.bmp";
lblResult = new Label();
lblResult.Location = new Point(0,0);
lblResult.Parent = pbCCD; //將label和picturebox(只是我命名為pbCCD而已)關(guān)聯(lián)!
lblResult.BackColor = Color.Transparent; //設(shè)置label的背景色為透明色。
lblResult.Text = "你好!";
lblResult.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
pbCCD.Load(filename);
}
private void pbCCD_MouseDown(object sender, MouseEventArgs e)
{
lblResult.Visible = true;
lblResult.Location = e.Location;
}
private void pbCCD_MouseUp(object sender, MouseEventArgs e)
{
lblResult.Visible = false;
}
}
利用c#的GDI+技術(shù),PictureBox.CreateGraphics()繪圖,利用g.DrawString寫文字。 利用this.Invalidate()刷新Form窗體,或者利用PictureBox.Invalidate()刷新PictureBox.。 范例代碼如下: using System; namespace CCDTest private void Form1_Load(object sender, EventArgs e) private void pbCCD_MouseDown(object sender, MouseEventArgs e) using (Graphics g = pbCCD.CreateGraphics()) private void pbCCD_MouseUp(object sender, MouseEventArgs e) |
聯(lián)系客服