设置datagridview的ToolTipText字体大小
时间: 2023-11-01 10:07:55 浏览: 82
要设置DataGridView的ToolTipText字体大小,可以使用以下代码:
```
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridViewCell cell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
string cellValue = cell.Value == null ? "" : cell.Value.ToString();
if (cell.ToolTipText != cellValue)
{
cell.ToolTipText = cellValue;
ToolTip tt = new ToolTip();
tt.UseFading = true;
tt.UseAnimation = true;
tt.IsBalloon = true;
tt.ShowAlways = true;
tt.AutoPopDelay = 5000;
tt.InitialDelay = 1000;
tt.ReshowDelay = 500;
tt.OwnerDraw = true;
tt.Draw += new DrawToolTipEventHandler(tt_Draw);
tt.Popup += new PopupEventHandler(tt_Popup);
tt.SetToolTip(dataGridView1, cell.ToolTipText);
}
}
}
private void tt_Popup(object sender, PopupEventArgs e)
{
Font f = new Font("Arial", 12, FontStyle.Bold);
e.ToolTipSize = TextRenderer.MeasureText(e.ToolTipText, f);
e.ToolTipSize = new Size(e.ToolTipSize.Width + 20, e.ToolTipSize.Height + 10);
}
private void tt_Draw(object sender, DrawToolTipEventArgs e)
{
e.DrawBackground();
e.DrawBorder();
e.DrawText(TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
}
```
在这个代码示例中,我们使用了DataGridView的CellMouseEnter事件来设置ToolTipText的值。然后,我们创建了一个ToolTip对象,并设置其各种属性,例如使用动画、显示气球、自动弹出延迟等。
在Popup事件中,我们使用MeasureText方法来测量ToolTipText的大小,并将其大小增加了一些以适应字体大小。在Draw事件中,我们使用DrawBackground、DrawBorder和DrawText方法来绘制ToolTipText的外观。
要更改字体大小,只需更改Font对象的大小即可。例如,要将字体大小更改为14,只需将以下行更改为:
```
Font f = new Font("Arial", 14, FontStyle.Bold);
```
阅读全文
相关推荐


















