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

C# 字形視窗製作(一):設計器設定與 TransparencyKey 非矩形視窗原理

     編輯:關於C語言

核心原理

protected override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawImage(bit, new Point(0, 0));
}

設計器生成程式碼關鍵設定

private void InitializeComponent()
{
    this.label1 = new Label();
    this.SuspendLayout();
    
    // Label 設為透明背景,作為視覺上關閉鈕
    this.label1.BackColor = Color.Transparent;
    this.label1.Location = new Point(261, 56);
    this.label1.Size = new Size(17, 16);
    this.label1.Click += label1_Click;
    
    // 視窗核心設定
    this.AutoScaleDimensions = new SizeF(6F, 12F);
    this.AutoScaleMode = AutoScaleMode.Font;
    this.BackColor = SystemColors.Control;
    this.ClientSize = new Size(393, 317);
    this.Controls.Add(this.label1);
    this.FormBorderStyle = FormBorderStyle.None;  // 關鍵:無邊框
    this.Name = "Form1";
    this.StartPosition = FormStartPosition.CenterScreen;
    this.Text = "非矩形視窗";
    this.TransparencyKey = SystemColors.Control;  // 關鍵:透明色鍵
    this.Load += Form1_Load;
    this.ResumeLayout(false);
}

非矩形視窗三要素

要素設定作用
移除邊框FormBorderStyle.None無標題列、無邊框、無系統選單
透明色鍵TransparencyKey = BackColor指定顏色像素完全透明 (含點擊穿透)
自繪內容OnPaint 繪製 Bitmap呈現任意形狀視覺內容

Bitmap 準備規格

  • 格式:BMP (支援透明色索引)
  • 背景色:單一純色 (範例使用藍色 Color.Blue)
  • 前景:任意形狀圖案、文字、圖示
  • 載入時:bit.MakeTransparent(Color.Blue) 將背景色標記透明

點擊穿透特性

透明色區域不僅視覺透明,滑鼠事件會穿透至下層視窗。若需特定區域可點擊,請在該區域放置控件 (如範例中的 Label)。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved