首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 字形視窗製作(二):程式碼控制 Bitmap 透明繪製與滑鼠關閉

     編輯:關於C語言

核心程式碼 (純程式碼版)

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();
    }
}

關鍵步驟總結

  1. 準備含單一背景色 (如藍色) 的 BMP 圖片
  2. FormBorderStyle.None 移除邊框
  3. TransparencyKey = BackColor 設定視窗透明色
  4. Bitmap.MakeTransparent 將圖片指定顏色設為透明
  5. OnPaint 繪製圖片至視窗客戶區
  6. 透明 Label 置於視覺上「關閉鈕」位置處理點擊關閉

與第 1 部分差異

  • 第 1 部分:著重設計器生成程式碼 (InitializeComponent)
  • 第 2 部分:著重邏輯程式碼 (Load、OnPaint、事件處理)
  • 兩部分合併即為完整可執行專案
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved