使用ToolStripControlHost将HTML代码加载到Windows窗体工具提示中 [英] Load HTML code into a Windows Forms Tooltip using ToolStripControlHost
本文介绍了使用ToolStripControlHost将HTML代码加载到Windows窗体工具提示中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Hi,
我将使用多色文本显示工具提示。
I am going to display tooltip with multicolor text.
我正在尝试在工具提示中加载html代码。
I am trying to load html code in tooltip.
我使用下面的代码在工具提示中加载html代码。
I am using below code to load html code in tooltip.
但我不知道如何在运行时更改工具提示文本并计算web浏览器大小。
例如,在此示例中单击button2时我想更改工具提示文本。
For example in this sample when clicking button2 I want to change the tooltip text.
public partial class Form1 : Form
{
// the shown webbrowser
private WebBrowser webBrowser1;
private WebBroswerToolTip2 toolTip;
// the webbrowser that get the size
private WebBrowser webBrowser2;
Size BodySize;
public Form1()
{
InitializeComponent();
webBrowser2 = new WebBrowser();
webBrowser2.DocumentCompleted += webBrowser2_DocumentCompleted;
this.Shown += Form1_Shown;
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
Rectangle Rect = this.webBrowser2.Document.Body.ScrollRectangle;
BodySize = new Size(Rect.Width, Rect.Height);
}
string documenttext = "<html><body>" +
// "<h1><b>This is de title of the tooltip text</b></h1>" +
"<p>Display sample text <u>here</u>.</p>" +
"</body></html>";
private void Form1_Shown(object sender, EventArgs e)
{
webBrowser1 = new WebBrowser();
webBrowser1.DocumentText = documenttext;
webBrowser1.ScrollBarsEnabled = false;
webBrowser1.Size = BodySize;
toolTip = new WebBroswerToolTip2(webBrowser1);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser2.DocumentText = documenttext;
webBrowser2.Update();
}
private void button1_MouseHover(object sender, EventArgs e)
{
Point point = new Point();
point.X = button1.Location.X + button1.Width / 2;
point.Y = button1.Location.Y + button1.Height;
//toolTip.Show(webBrowser1, point);
toolTip.Show(webBrowser1, MousePosition);
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser2.DocumentText = "<html><body>" +
"<p>ggjh</u>.</p>" +
"</body></html>";
webBrowser2.Update();
webBrowser1.DocumentText = "<html><body>" +
"<p>ggjh</u>.</p>" +
"</body></html>";
webBrowser1.Update();
}
private void button1_MouseLeave(object sender, EventArgs e)
{
toolTip.Dispose();
webBrowser1 = new WebBrowser();
webBrowser1.DocumentText = documenttext;
webBrowser1.ScrollBarsEnabled = false;
webBrowser1.Size = BodySize;
toolTip = new WebBroswerToolTip2(webBrowser1);
}
}
class WebBroswerToolTip2 : ToolStripDropDown
{
public Control ctl;
public WebBroswerToolTip2(Control aControl)
: base()
{
this.ctl = aControl;
Initialize();
}
public void Initialize()
{
this.AutoSize = false;
ToolStripControlHost host = new ToolStripControlHost(this.ctl);
this.Margin = Padding.Empty;
this.Padding = Padding.Empty;
host.Margin = Padding.Empty;
host.Padding = Padding.Empty;
host.AutoSize = false;
host.Size = ctl.Size;
this.Size = ctl.Size;
this.Items.Add(host);
}
}
推荐答案
您好sgrm123,
Hi sgrm123,
要更改工具提示的文本,您可以按照此代码段进行操作。
To change the tooltip's text, you can follow this code segment.
bool flag = false;
string newdocumenttext = "<html><body>" +
"<h1><font face=\"verdana\">A heading</font></h1>" +
"<p><font size=\"5\" face=\"arial\" color=\"red\">A paragraph.</font></p>" +
"</body></html>";
private void button1_MouseHover(object sender, EventArgs e)
{
if (flag)
{
webBrowser1 = new WebBrowser();
webBrowser1.DocumentText = newdocumenttext;
webBrowser1.ScrollBarsEnabled = false;
webBrowser1.Size = BodySize;
toolTip = new WebBroswerToolTip2(webBrowser1);
}
toolTip.Show(webBrowser1, MousePosition);
}
private void button2_Click(object sender, EventArgs e)
{
webBrowser2 = new WebBrowser();
webBrowser2.DocumentText = newdocumenttext;
webBrowser2.DocumentCompleted += webBrowser2_DocumentCompleted;
flag = true;
}
由于您使用的是webbrowser,您可以通过html字体标记设置颜色。
Since you are using webbrowser, you can set the color via the html font tag.
结果:
问候,
Kyle
这篇关于使用ToolStripControlHost将HTML代码加载到Windows窗体工具提示中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文