Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

How to draw text above some level correctly

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    How to draw text above some level correctly

    Hello, I need to draw some horizontal line and then the text above it in OnRender method

    for drawing line I use
    Code:
    RenderTarget.DrawLine(new Vector2(x1, y), new Vector2(x2, y), lineBrush, lineWidth);
    Then I use this code to draw text above the line

    Code:
    SimpleFont wpfFont = chartControl.Properties.LabelFont ?? new SimpleFont();
    using (var textFormat = wpfFont.ToDirectWriteTextFormat())
    {
       using (TextLayout textLayout = new TextLayout(
          Globals.DirectWriteFactory,
          MyText,
          textFormat, 1, textFormat.FontSize))
        {
           RenderTarget.DrawTextLayout(new Vector2(x1, y), textLayout, textBrush, DrawTextOptions.NoSnap);
         }
    }
    The text appears under the line because my y coordinate = top of the text rectangle by default. How can I tell to DrawTextLayout routine that my y coordinate is a bottom of the text rectangle?

    #2
    Hello rfsettling,

    Thanks for your inquiry.

    The solution would be to measure the font height from the TextLayout and then to use that value to offset the DrawTextLayout()'s y-coordinate.

    I've attached some sample code and a working example that uses it.

    Code:
    private int CheckFontHeight(SimpleFont font)
    {
    	int Height = 0;
    	
    	SharpDX.DirectWrite.TextFormat textFormat = font.ToDirectWriteTextFormat();
    	SharpDX.DirectWrite.TextLayout textLayout =
    		new SharpDX.DirectWrite.TextLayout(NinjaTrader.Core.Globals.DirectWriteFactory,
    		"", textFormat, ChartPanel.X + ChartPanel.W,
    		textFormat.FontSize);
    
      	Height =  (int)textLayout.Metrics.Height;
    
    	textLayout.Dispose();
    	textFormat.Dispose();
    	
    	return Height;
    }
    If you have any additional questions, please don't hesitate to write back.
    Attached Files

    Comment


      #3
      Hello, Jim

      I hoped there is a vertical alignment setting which I just could not find. Having vertical alignment would make the life easier...

      Anyway, thanks!

      Comment


        #4
        thank Jim, this gave me an idea

        Comment

        Latest Posts

        Collapse

        Topics Statistics Last Post
        Started by charlesugo_1, 05-26-2026, 05:03 PM
        0 responses
        72 views
        0 likes
        Last Post charlesugo_1  
        Started by DannyP96, 05-18-2026, 02:38 PM
        1 response
        152 views
        0 likes
        Last Post NinjaTrader_ChelseaB  
        Started by CarlTrading, 05-11-2026, 05:56 AM
        0 responses
        162 views
        0 likes
        Last Post CarlTrading  
        Started by CarlTrading, 05-10-2026, 08:12 PM
        0 responses
        100 views
        0 likes
        Last Post CarlTrading  
        Started by Hwop38, 05-04-2026, 07:02 PM
        0 responses
        288 views
        0 likes
        Last Post Hwop38
        by Hwop38
         
        Working...
        X