문제

나는 멀티 라인 텍스트 문자열 (예 : "stuff n more stuff nyet 더 많은 것들")을 가지고 있으며, 비트 맵과 함께 툴팁에 페인트를 칠하고 싶습니다. 비트 맵을 그리기 때문에 소유자 드로우를 True로 설정해야합니다. 또한 팝업 이벤트를 처리하고 있으므로 텍스트와 비트 맵을 보유 할 수있을 정도로 툴팁을 크기로 크기를 크기로 크기를 만들 수 있습니다.

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);
}
도움이 되었습니까?

해결책

나는 당신이 그릴 때 경계 사각형을 정의하면 (이미지 오프셋 계산) 당신은 다음과 같이 할 수 있다고 가정합니다.

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

다른 팁

특정 너비 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;
}

안부, 탐버그

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top