先定义tooltip的操作类
public static class TooltipToolV2
{
/// <summary>
/// 为控件提供Tooltip
/// </summary>
/// <param name="control">控件</param>
/// <param name="tip">ToolTip</param>
/// <param name="message">提示消息</param>
public static void ShowTooltip(this Control control, ToolTip tip, string message)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x, _y);
tip.Active = true;
}
/// <summary>
/// 为控件提供Tooltip
/// </summary>
/// <param name="control">控件</param>
/// <param name="tip">ToolTip</param>
/// <param name="message">提示消息</param>
/// <param name="durationTime">保持提示的持续时间</param>
public static void ShowTooltip(this Control control, ToolTip tip, string message, int durationTime)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x, _y, durationTime);
tip.Active = true;
}
public static void HidenTooltip(this Control control, ToolTip tip) {
tip.Hide(control);
}
/// <summary>
/// 为控件提供Tooltip
/// </summary>
/// <param name="control">控件</param>
/// <param name="tip">ToolTip</param>
/// <param name="message">提示消息</param>
/// <param name="xoffset">水平偏移量</param>
/// <param name="yoffset">垂直偏移量</param>
public static void ShowTooltip(this Control control, ToolTip tip, string message, int xoffset, int yoffset)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x + xoffset, _y + yoffset);
tip.Active = true;
}
/// <summary>
/// 为控件提供Tooltip
/// </summary>
/// <param name="control">控件</param>
/// <param name="tip">ToolTip</param>
/// <param name="message">提示消息</param>
/// <param name="xoffset">水平偏移量</param>
/// <param name="yoffset">垂直偏移量</param>
/// <param name="durationTime">保持提示的持续时间</param>
public static void ShowTooltip(this Control control, ToolTip tip, string message, int xoffset, int yoffset, int durationTime)
{
Point _mousePoint = Control.MousePosition;
int _x = control.PointToClient(_mousePoint).X;
int _y = control.PointToClient(_mousePoint).Y;
tip.Show(message, control, _x + xoffset, _y + yoffset, durationTime);
tip.Active = true;
}
然后在需要自定义tooltip的页面,引入tooltip,定义初始化方法
string schedulerToolTip = "";
private void toolTipSet(string tipText)
{
schedulerToolTip = tipText;
toolTip1.OwnerDraw = true;
toolTip1.Popup -= ToolTip_Popup;
toolTip1.Popup += ToolTip_Popup;
toolTip1.Draw -= ToolTip_Draw;
toolTip1.Draw += ToolTip_Draw;
}
void ToolTip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = TextRenderer.MeasureText(schedulerToolTip, new Font("微软雅黑", 14.0f));
}
void ToolTip_Draw(object sender, DrawToolTipEventArgs e)
{
using (e.Graphics)
{
Font f = new Font("微软雅黑", 14.0f);
e.DrawBackground();
e.DrawBorder();
e.Graphics.DrawString(e.ToolTipText, f, Brushes.Black, new Point(2, 2));
}
}
在需要操作的方法里面调用
private void dgv_CurTechnic_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
var tempText= (dgv_CurTechnic.Rows[e.RowIndex].Cells[e.ColumnIndex].Value ?? string.Empty).ToString();
string resultString = "";
int i = 0;
int size = 12;
// 此处为了换行,适当参考
for (; i< tempText.Length / size; i++)
resultString += tempText.Substring(i* size, size) + Environment.NewLine;
resultString += tempText.Substring(i * size, resulttxt.Length - i * size);
toolTipSet(resultString);
dgv_CurTechnic.ShowTooltip(toolTip1, resultString, 55000);
}
private void dgv_CurTechnic_Leave(object sender, EventArgs e)
{
dgv_CurTechnic.HidenTooltip(toolTip1);
}