更改 Winforms DataGridView ToolTip 字体 [英] Changing Winforms DataGridView ToolTip font

查看:22
本文介绍了更改 Winforms DataGridView ToolTip 字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有 DataGridView,在它上面我在 MouseMove 下编写了一个自定义工具提示,就像这样...

I have DataGridView in my program on which I compose a custom ToolTip under MouseMove, like this...

Private Sub dgv_dokument_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgv_dokument.MouseMove

    Dim hit As DataGridView.HitTestInfo = dgv_dokument.HitTest(e.X, e.Y)

    If hit.Type = DataGridViewHitTestType.Cell Then
        If hit.ColumnIndex >= 0 AndAlso hit.RowIndex >= 0 Then
            Dim s As Integer = Convert.ToInt32(dgv_dokument.Item(co.GetColIndex(dgv_dokument, "myNumCol"), hit.RowIndex).Value)
            Dim ttText As String = ""
            If s > 0 Then
                Dim sb As New StringBuilder
                get_data(s, sb)
                ttText = sb.ToString
            End If

            dgv_dokument.Item(hit.ColumnIndex, hit.RowIndex).ToolTipText = ttText
            Exit Sub
        End If
    End If
End Sub

在 get_data(s, sb) 下,我用代表列"格式数据的字符串填充了 StringBuilder,就像这样...

Under get_data(s, sb) I filled StringBuilder with strings in mean of "column" formatted data, like this...

   sb.Append(code.ToString.PadLeft(5) + " ")
   sb.Append(name.Trim.PadRight(27) + " ")
   sb.Append(meas.Trim.PadRight(3) + " ")
   sb.Append(qty.ToString("N2").PadLeft(10) + " ")
   sb.Append(price.ToString("N2").PadLeft(12))
   sb.Append(Environment.NewLine)

除了我希望看到我的工具提示列对齐之外,一切都很好.这可以通过在 ToolTop 中使用比例字体来完成.

That all works good except that I would like to see my tooltip column-aligned. That may be done with using proportional font in ToolTop.

我能否以某种方式(以及如何)确定 DataGridView 工具提示的默认字体以外的字体?例如Courier New".

Can I somehow (and how) determine other than default font for just DataGridView's ToolTip's? For example "Courier New".

推荐答案

您需要OwnerDrawToolTip.

这是一个例子:

首先为ToolTip设置属性OwnerDraw = true.

然后编码它的 Draw 事件,可能是这样的:

Then code its Draw event, maybe like this:

private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
{
    e.DrawBackground();
    e.DrawBorder();
    using (Font f = new Font("Consolas", 8f))
        e.Graphics.DrawString(e.ToolTipText, f, SystemBrushes.ControlText, e.Bounds);
}

请注意,如果您的字体小于默认值(如我的字体),则会显示一些额外的可用空间,这非常好.

Note that if your Fonts get smaller than the default (as mine) some extra free space will show, which is pretty much OK.

但如果它更大Bounds 可能需要适应,这可能会变得有些棘手.您必须填充文本到末尾和底部有足够的空间,以强制使用足够的Bounds 大小.

But if it is larger the Bounds may need to adapt, which can get somewhat tricky. You would have to pad the text with enough room to the end and bottom to enforce a sufficient Bounds size.

为此,您必须测量较大Font 所需的空间并添加空格,直到Bounds 增长到足够大.无需删除它们,因为透支空间不会成为问题;但坚持使用原来的 e.Font 肯定要容易得多.

For this you would have to measure the space needed for the larger Font and add spaces until the Bounds have grown enough. No need to remove them as overdrawing spaces will not be a problem; but sticking with the original e.Font certainly is a lot easier.

仍然可能会发现需要添加几个空格:您的固定字体平均会占用更多空间,因此Bounds代码> 无论如何,提供的结果可能有点紧..

You still may find the need to add a few spaces: Your fixed font will, on average, take a little more space so the Bounds provided may turn out to be a little tight anyway..

更新

由于您使用的是 DataGridView,因此您需要考虑一些额外的事项:

Since you are using a DataGridView you need a few extras to consider:

  • DGV 旨在显示与自己的单元格相关的工具提示.但是他们没有可访问的抽奖活动;所以我们不能使用它们.所以我们关闭它们:dataGridView.ShowCellToolTips = false;

由于我们需要为每个单元格显示一个不同的单元格,因此我们需要向它们显示我们需要检测何时到达新单元格.DGV 对于每个单元格没有不同的文本,虽然单元格有自己的文本,但不会将其交给外部 ToolTip.所以我们可以编写 MouseMove 事件,可能是这样的:

Since we need to show a different one for each cell we need to show them we need to detect when we are over a new cell. The DGV doesn't have a different text for each cell, although the cells have theirs, but won't hand it to the external ToolTip. So we can code the MouseMove event, maybe like this:

private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
    if (e.ColumnIndex < 0 || e.RowIndex < 0 
     || dataGridView1[e.ColumnIndex, e.RowIndex].Value == null) return;

    // use your own function to set the text!
    string s = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
    // check for changes to prevent flicker!
    if (s == toolTip1.GetToolTip(dataGridView1)) return;

    toolTip1.SetToolTip(dataGridView1, s); 
}

请注意,关于用空格填充以使文本适合边界的说明仍然适用..

Note that the remarks about padding with spaces to make the text fit in the bounds still applies..

这篇关于更改 Winforms DataGridView ToolTip 字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆