0

複数行のテキスト文字列 (例: "Stuff\nMore Stuff\nYet More Stuff") があり、それをビットマップと共にツールチップにペイントしたいと考えています。ビットマップをペイントしているので、OwnerDraw を true に設定する必要があります。また、Popup イベントも処理しているので、ツールチップのサイズをテキストとビットマップを保持するのに十分な大きさにすることができます。

e.DrawBackground と e.DrawBorder() を呼び出してから、ツールチップ領域の左側にビットマップをペイントしています。

テキストを左揃えにするために e.DrawText() に渡すことができるフラグのセットはありますが、ビットマップ上に描画されないようにオフセットすることはできますか? または、すべてのテキストもカスタム描画する必要がありますか (おそらく改行などで文字列を分割する必要があります)?

更新: 最終的なコードは次のようになります。

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);
}
4

2 に答える 2

2

描画する境界矩形を定義する(画像オフセットを自分で計算する)と、次のことができると思います。

     RectangleF rect = new RectangleF(100,100,100,100);
     e.Graphics.DrawString(myString, myFont, myBrush, rect);
于 2008-11-07T09:35:15.740 に答える
0

特定の幅wが与えられた場合に、所有者が描画した文字列sの高さを計算するには、次のコードを使用します。

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;
}

よろしく、タンバーグ

于 2008-11-07T09:55:01.613 に答える