2011-05-06 102 views
0

如果文本是多行的,我將創建一個自定義ToolTip,該文本將粗體顯示第一行文本。我還使用VisualStyleRenderer正確繪製具有樣式的工具提示。但是,當我繪製文本(即使使用TextFormatFlags.VerticalCenter設置)時,它會在框的頂部處繪製。我打算將邊界框撞上2個像素(它固定在Windows 7上),但我並不是100%確定它是如何便攜到另一個操作系統的。有誰知道如何正確繪製文字垂直居中?將文本垂直對齊VisualStyleElement.ToolTip.Standard.Normal的VisualStyleRenderer

編輯:爲了清楚,我知道這段代碼不粗體的第一行。我試圖第一個複製一個標準的工具提示,然後事後做粗體。

public class BoldedFirstLineToolTip : ToolTip 
{ 
    public BoldedFirstLineToolTip() 
    { 
     this.OwnerDraw = true; 
     this.Draw += new DrawToolTipEventHandler(OnDraw); 
    } 

    private void OnDraw(object sender, DrawToolTipEventArgs e) 
    { 
     // Try to draw using the visual style renderer. 
     if (VisualStyleRenderer.IsElementDefined(VisualStyleElement.ToolTip.Standard.Normal)) 
     { 
      var renderer = new VisualStyleRenderer(VisualStyleElement.ToolTip.Standard.Normal); 
      renderer.DrawBackground(e.Graphics, e.Bounds); 

      var b = e.Bounds; 
      // b.Y + 2 // This works when using e.Graphics.DrawString. 
      renderer.DrawText(e.Graphics, b, e.ToolTipText, false /*drawDisabled*/, 
       TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter); 
     } 
     else 
     { 
      // Fall back to non-visual style drawing. 
      e.DrawBackground(); 
      e.DrawBorder(); 
      e.DrawText(); 
     } 
    } 
} 

回答

0

我決定只使用填充修正。我在下面提供了我的完整解決方案。我在XP和Windows 7上都進行了測試。

public class BoldedFirstLineToolTip : ToolTip 
{ 
    public BoldedFirstLineToolTip() 
    { 
     this.OwnerDraw = true; 
     this.Draw += new DrawToolTipEventHandler(OnDraw); 
    } 

    private void OnDraw(object sender, DrawToolTipEventArgs e) 
    { 
     // Try to draw using the visual style renderer. 
     if (VisualStyleRenderer.IsSupported && VisualStyleRenderer.IsElementDefined(VisualStyleElement.ToolTip.Standard.Normal)) 
     { 
      var bounds = e.Bounds; 
      var renderer = new VisualStyleRenderer(VisualStyleElement.ToolTip.Standard.Normal); 
      renderer.DrawBackground(e.Graphics, bounds); 

      var color = renderer.GetColor(ColorProperty.TextColor); 

      var text = e.ToolTipText; 

      using (var textBrush = new SolidBrush(renderer.GetColor(ColorProperty.TextColor))) 
      using (var font = e.Font) 
      { 
       // Fix the positioning of the bounds for the text rectangle. 
       var rendererBounds = new Rectangle(e.Bounds.X + 6, e.Bounds.Y + 2, e.Bounds.Width - 6 * 2, e.Bounds.Height - 2 * 2); 

       if (!text.Contains('\n')) 
       { 
        renderer.DrawText(e.Graphics, rendererBounds, text); 
       } 
       else 
       { 
        var lines = text.Split('\n').Select(l => l.Trim()); 
        var first = lines.First(); 
        var otherLines = Environment.NewLine + String.Join(Environment.NewLine, lines.Skip(1).ToArray()); 

        // Draw the first line. 
        using (var boldFont = new Font(font, FontStyle.Bold)) 
        { 
         e.Graphics.DrawString(first, boldFont, textBrush, rendererBounds.X - 1, rendererBounds.Y - 1); 
        } 

        renderer.DrawText(e.Graphics, rendererBounds, otherLines, false /*drawDisabled*/, TextFormatFlags.Left); 
       } 

      } 
     } 
     else 
     { 
      // Fall back to non-visual style drawing. 
      e.DrawBackground(); 
      e.DrawBorder(); 
      using (var sf = new StringFormat()) 
      { 
       sf.LineAlignment = StringAlignment.Center; 
       e.Graphics.DrawString(e.ToolTipText, SystemFonts.DialogFont, Brushes.Black, e.Bounds, sf); 
      } 
     } 
    } 
}