C#/ .NET中OwnerDraw工具提示上的文本对齐方式

时间:2021-12-21 20:05:42

I have a multiline text string (e.g. "Stuff\nMore Stuff\nYet More Stuff"), and I want to paint it, along with a bitmap into a tooltip. Since I am painting the bitmap, I need to set OwnerDraw to true, which I am doing. I am also handling the Popup event, so I can size the tooltip to be large enough to hold the text and the bitmap.

我有一个多行文本字符串(例如“Stuff \ nMore Stuff \ nYet More Stuff”),我想将它与位图一起绘制到工具提示中。由于我正在绘制位图,我需要将OwnerDraw设置为true,我正在这样做。我也在处理Popup事件,所以我可以将工具提示的大小设置得足够大以容纳文本和位图。

I am calling e.DrawBackground and e.DrawBorder(), and then painting my bitmap on the left side of the tooltip area.

我正在调用e.DrawBackground和e.DrawBorder(),然后在工具提示区域的左侧绘制我的位图。

Is there a set of flags I can pass to e.DrawText() in order to left-align the text, but to offset it so that it doesn't get painted over my bitmap? Or do I need to custom draw all the text as well (which will probably involve splitting the string on newlines, etc)?

是否有一组标志我可以传递给e.DrawText()以便左对齐文本,但要偏移它以便它不会被绘制在我的位图上?或者我是否需要自定义绘制所有文本(这可能涉及将字符串拆分为换行符等)?

UPDATED: The final code looks like this:

更新:最终代码如下所示:

private void _ItemTip_Draw(object sender, DrawToolTipEventArgs e)
{
  e.DrawBackground();
  e.DrawBorder();

  // Reserve a square of size e.Bounds.Height x e.Bounds.Height
  // for the image. Keep a margin around it so that it looks good.
  int margin = 2;
  Image i = _ItemTip.Tag as Image;  
  if (i != null)
  {
    int side = e.Bounds.Height - 2 * margin;  
    e.Graphics.DrawImage(i, new Rectangle(margin, margin, side, side));
  }

  // Construct bounding rectangle for text (don't want to paint it over the image).
  int textOffset = e.Bounds.Height + 2 * margin; 
  RectangleF rText = e.Bounds;
  rText.Offset(textOffset, 0);
  rText.Width -= textOffset;

  e.Graphics.DrawString(e.ToolTipText, e.Font, Brushes.Black, rText);
}

2 个解决方案

#1


2  

I assume that if you define the bounding rectangle to draw in (calculating the image offset yourself) you could just:

我假设如果您定义要绘制的边界矩形(自己计算图像偏移量),您可以:

     RectangleF rect = new RectangleF(100,100,100,100);
     e.Graphics.DrawString(myString, myFont, myBrush, rect);

#2


0  

to calculate the Height of an owner drawn string s given a certain width w, we use the following code:

为了计算给定一定宽度w的所有者绘制的字符串的高度,我们使用以下代码:

double MeasureStringHeight (Graphics g, string s, Font f, int w) {
    double result = 0;
    int n = s.Length;
    int i = 0;
    while (i < n) {
        StringBuilder line = new StringBuilder();
        int iLineStart = i;
        int iSpace = -1;
        SizeF sLine = new SizeF(0, 0);
        while ((i < n) && (sLine.Width <= w)) {
            char ch = s[i];
            if ((ch == ' ') || (ch == '-')) {
                iSpace = i;
            }
            line.Append(ch);
            sLine = g.MeasureString(line.ToString(), f);
            i++;
        }
        if (sLine.Width > w) {
            if (iSpace >= 0) {
                i = iSpace + 1;
            } else {
                i--;
            }
            // Assert(w > largest ch in line)
        }
        result += sLine.Height;
    }
    return result;
}

Regards, tamberg

#1


2  

I assume that if you define the bounding rectangle to draw in (calculating the image offset yourself) you could just:

我假设如果您定义要绘制的边界矩形(自己计算图像偏移量),您可以:

     RectangleF rect = new RectangleF(100,100,100,100);
     e.Graphics.DrawString(myString, myFont, myBrush, rect);

#2


0  

to calculate the Height of an owner drawn string s given a certain width w, we use the following code:

为了计算给定一定宽度w的所有者绘制的字符串的高度,我们使用以下代码:

double MeasureStringHeight (Graphics g, string s, Font f, int w) {
    double result = 0;
    int n = s.Length;
    int i = 0;
    while (i < n) {
        StringBuilder line = new StringBuilder();
        int iLineStart = i;
        int iSpace = -1;
        SizeF sLine = new SizeF(0, 0);
        while ((i < n) && (sLine.Width <= w)) {
            char ch = s[i];
            if ((ch == ' ') || (ch == '-')) {
                iSpace = i;
            }
            line.Append(ch);
            sLine = g.MeasureString(line.ToString(), f);
            i++;
        }
        if (sLine.Width > w) {
            if (iSpace >= 0) {
                i = iSpace + 1;
            } else {
                i--;
            }
            // Assert(w > largest ch in line)
        }
        result += sLine.Height;
    }
    return result;
}

Regards, tamberg