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

C# WinForm 自訂 ToolStripButton 背景與邊框樣式

     編輯:關於C#

需求

預設 ToolStripButton 滑鼠懸停背景為藍色,需自訂背景顏色與邊框樣式(例:滑鼠進入變黑色)。

解決方案:重寫 Paint 事件

ToolStripButton tsb = (ToolStripButton)sender;
Rectangle rectButton = tsb.Bounds;
Point p = toolStrip1.PointToClient(Control.MousePosition);
if (rectButton.Contains(p)) {
  e.Graphics.Clear(SystemColors.ControlText); // 背景設為黑色
  if (tsb.Image != null) {
    e.Graphics.DrawImage(tsb.Image,
      new Point((e.ClipRectangle.Width - tsb.Image.Width) / 2,
                (e.ClipRectangle.Height - tsb.Image.Height) / 2));
  }
}

在 Paint 事件中判斷滑鼠是否在按鈕區域,手動繪製背景與圖片置中。

原始碼下載

ToolStripButton 自訂背景範例原始碼

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