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