[英]passing font size to user control tool tip in Winform C# project
問題:我有一個Winform C#項目,當從主機項目傳遞時,它會更改用戶控制工具提示的工具提示文本。 我需要將字體大小作為變量傳遞,但不知道在哪里進行更改。 我在網上嘗試了大量的解決方案,並堅持這個實現。 任何這方面的幫助都將非常感激。
到目前為止我所擁有的:
我有一個C#用戶控件項目,我在我的UC_ToolTipButton項目中設置了一個屬性來設置工具提示文本以更改為用戶指定的值,其中項目的內容如下:
UC_ToolTipButton.cs
using System.Windows.Forms;
namespace UC_ToolTipButton
{
public partial class UC_ToolTipButton : UserControl
{
public string TT_Message
{
get{
return ToolTip_Message.GetToolTip(btnTT);
}
set{
ToolTip_Message.SetToolTip(btnTT, value);
}
}
public UC_ToolTipButton()
{
InitializeComponent();
}
}
}
在我的設計器文件中,我放置了一個按鈕(btnTT),我在其上放了一個工具提示(ToolTip_Message)。
當我編譯此用戶控件表單項目時,它工作正常並創建一個DLL文件。
在項目TryButtonTooltip中導入此文件后,我在其中包含文件TryTooltipForm.cs,其中包含以下內容
using System.Windows.Forms;
namespace TryButtonToolTip
{
public partial class TryToolTipForm : Form
{
public TryToolTipForm()
{
InitializeComponent();
uC_TTMessage.TT_Message = @"Hi";
}
}
}
設置OwnerDraw
的ToolTip
,以true
,在ToolTip
的Draw
活動設置所需的字體,然后在Popup
的事件的措施,並設置您的大小ToolTip
,如在例子中說明這里 。
例如像這樣(未經測試):
public partial class UC_ToolTipButton : UserControl
{
public string TT_FontFamily { get; set; }
public float TT_FontSize { get; set; }
public string TT_Message
{
get
{
return ToolTip_Message.GetToolTip(btnTT);
}
set
{
ToolTip_Message.SetToolTip(btnTT, value);
}
}
public UC_ToolTipButton()
{
InitializeComponent();
TT_FontFamily = "Tahoma";
TT_FontSize = 10;
ToolTip_Message.OwnerDraw = true;
ToolTip_Message.Draw += new DrawToolTipEventHandler(TT_Draw);
ToolTip_Message.Popup += new PopupEventHandler(TT_Popup);
}
private void TT_Popup(object sender, PopupEventArgs e)
{
using (Font f = new Font(TT_FontFamily, TT_FontSize))
{
e.ToolTipSize = TextRenderer.MeasureText(ToolTip_Message.GetToolTip(e.AssociatedControl), f);
}
}
private void TT_Draw(System.Object sender,
System.Windows.Forms.DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
using (StringFormat sf = new StringFormat())
{
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
using (Font f = new Font(TT_FontFamily, TT_FontSize))
{
e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ActiveCaptionText, e.Bounds, sf);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.