核心程式碼 (純程式碼版)
public partial class Form1 : Form
{
Bitmap bit;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bit = new Bitmap("WordImage.bmp");
bit.MakeTransparent(Color.Blue); // 指定透明色
}
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(bit, new Point(0, 0));
}
private void label1_Click(object sender, EventArgs e)
{
this.Close();
}
}
關鍵步驟總結
- 準備含單一背景色 (如藍色) 的 BMP 圖片
FormBorderStyle.None 移除邊框
TransparencyKey = BackColor 設定視窗透明色
Bitmap.MakeTransparent 將圖片指定顏色設為透明
OnPaint 繪製圖片至視窗客戶區
- 透明 Label 置於視覺上「關閉鈕」位置處理點擊關閉
與第 1 部分差異
- 第 1 部分:著重設計器生成程式碼 (
InitializeComponent)
- 第 2 部分:著重邏輯程式碼 (
Load、OnPaint、事件處理)
- 兩部分合併即為完整可執行專案