User Tools

Site Tools


Action disabled: source
blog:2025-05-28-001



2025-05-28 C# WinForm Label 加上有圖的 tooltips

  • 在 C# WinForms 中,為 Label 控制項添加帶有圖像的工具提示(ToolTip)並不直接支持,但可以通過使用 ToolTip 控制項來實現。以下是如何創建一個具有圖像的工具提示的步驟:

步驟 1:添加 ToolTip 控制項

  • 在 WinForms 應用程序中,打開設計視圖。
  • 從工具箱中拖動 ToolTip 控制項到你的表單上。這將在設計器中添加一個隱形的 ToolTip 控制項。

步驟 2:設置 Label 和 ToolTip

  • 接下來,需要設置 Label 和 ToolTip 的屬性。以下是一個示例代碼,演示如何在 Label 上顯示帶有圖像的工具提示。
  • 示例代碼
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MainForm : Form
    {
        private Label myLabel;
        private ToolTip toolTip;
    
        public MainForm()
        {
            // 初始化組件
            InitializeComponent();
        }
    
        private void InitializeComponent()
        {
            this.myLabel = new Label();
            this.toolTip = new ToolTip();
    
            // 設置 Label
            this.myLabel.Text = "Hover over me!";
            this.myLabel.Location = new Point(50, 50);
            this.myLabel.AutoSize = true;
    
            // 設置 ToolTip
            this.toolTip.ToolTipTitle = "Information";
            this.toolTip.ToolTipIcon = ToolTipIcon.Info;
    
            // 設置 ToolTip 的內容
            this.toolTip.SetToolTip(this.myLabel, "This is a tooltip with an image!");
    
            // 設置 ToolTip 的圖像
            this.toolTip.Popup += new PopupEventHandler(toolTip_Popup);
    
            // 添加 Label 到窗體
            this.Controls.Add(this.myLabel);
    
            // 設定窗體的其他屬性
            this.Text = "ToolTip Example";
            this.Size = new Size(300, 200);
        }
    
        private void toolTip_Popup(object sender, PopupEventArgs e)
        {
            // 獲取 ToolTip 的圖形
            ToolTip tt = sender as ToolTip;
            if (tt != null)
            {
                // 創建一個圖像
                Bitmap bmp = new Bitmap(16, 16);
                using (Graphics g = Graphics.FromImage(bmp))
                {
                    g.Clear(Color.Transparent);
                    g.FillEllipse(Brushes.Red, 0, 0, 16, 16); // 繪製紅色圓形
                }
    
                // 在工具提示中繪製圖像
                e.ToolTipSize = new Size(200, 100); // 設定工具提示的大小
                e.ToolTip = tt;
                e.ToolTip.Draw += (s, args) =>
                {
                    args.Graphics.DrawImage(bmp, new Point(0, 0));
                    args.Graphics.DrawString(tt.GetToolTip(myLabel), tt.Font, Brushes.Black, new Point(20, 0));
                };
            }
        }
    
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }
    }

說明

  • ToolTip 控制項:
    • 使用 ToolTip 控制項來顯示工具提示。
  • Popup 事件:
    • 設置 Popup 事件來自定義工具提示的顯示方式。在這裡,我們在工具提示中繪製一個紅色圓形圖像。
  • 繪製圖像:
    • 使用 Graphics 對象來繪製圖像和文本。這樣可以在工具提示中顯示圖像。

注意事項

  • 這個示例中的圖像是簡單的紅色圓形,你可以根據需要替換為其他圖像。
  • 確保圖像的大小和位置適合工具提示的顯示。
  • 工具提示的內容和圖像可以根據具體需求進行調整。
  • 這樣就可以在 C# WinForms 的 Label 控制項上添加帶有圖像的工具提示了!

TAGS

  • 27 person(s) visited this page until now.

blog/2025-05-28-001.txt · Last modified: 2025/05/28 13:34 by jethro