datagridview使用tooltip控件
datagridview使用tooltip控件显示单元格内容且不闪
之前把显示tooltip显示的事件写在GrvMessage_CellMouseMove里面导致一直在闪后改在GrvMessage_CellMouseEnter里就不闪了
private int cellColumnIndex = -1, cellRowIndex = -1;
public ShowMassage()
{
InitializeComponent();
this.GrvMessage.ShowCellToolTips = false;
this.toolTip.AutomaticDelay = 0;
this.toolTip.OwnerDraw = true;
this.toolTip.ShowAlways = true;
this.toolTip.ToolTipTitle = " ";
this.toolTip.UseAnimation = true;
this.toolTip.UseFading = true;
}
/// <summary>
/// 鼠标移开是隐藏tooltip
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GrvMessage_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
{
this.toolTip.Hide(this.GrvMessage);
}
/// <summary>
/// 重绘tooltip的样式
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void toolTip_Draw_1(object sender, DrawToolTipEventArgs e)
{
e.Graphics.FillRectangle(Brushes.AliceBlue, e.Bounds);
e.Graphics.DrawRectangle(Pens.Chocolate, new Rectangle(0, 0, e.Bounds.Width - 1, e.Bounds.Height - 1));
e.Graphics.DrawString(this.toolTip.ToolTipTitle + e.ToolTipText, e.Font, Brushes.Red, e.Bounds);
}
/// <summary>
/// 鼠标移动到datagridview的单元格内显示数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void GrvMessage_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0 || e.ColumnIndex < 0)
{
return;
}
this.toolTip.Hide(this.GrvMessage);
this.cellColumnIndex = e.ColumnIndex;
this.cellRowIndex = e.RowIndex;
if (this.cellColumnIndex >= 0 && this.cellRowIndex >= 0)
{
Point mousePos = PointToClient(MousePosition);
string tip = this.GrvMessage[this.cellColumnIndex, this.cellRowIndex].Value.ToString();
this.toolTip.Show(tip, this.GrvMessage, mousePos);
}
}
for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
{
if (this.dataGridView1[0, i].Value != null)
{
this.dataGridView1[0, i].ToolTipText = this.dataGridView1[0, i].Value.ToString();
}
}