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 | 呈現任意形狀視覺內容 |
Color.Blue)bit.MakeTransparent(Color.Blue) 將背景色標記透明透明色區域不僅視覺透明,滑鼠事件會穿透至下層視窗。若需特定區域可點擊,請在該區域放置控件 (如範例中的 Label)。