Announcement

Collapse
No announcement yet.

Partner 728x90

Collapse

TextMetrics

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

    TextMetrics

    I am trying to get the width of a string (after layout).

    In NT7 I was using:
    graphics.MeasureString(convertDoublePrice(prevVal) ,txtFont).Width)

    So far in NT8 I have:
    SharpDX.DirectWrite.TextMetrics(convertDoublePrice (prevVal),txtFont).Width)

    which gives a compile error of:
    SharpDX.DirectWrite.TextMetrics is a 'type' which is not valid in the given context.

    Could someone please point in the correct direction. SharpDX is completely new to me.

    #2
    You can get the Metrics property of the Layout object that you create and get that width.

    Code:
    TextLayout.Metrics.Width

    Comment


      #3
      Hi Calonious

      Are you able to give an example?
      Just typing that code is not giving me anything (no intellisense after a period).

      I just want the width of a string in pixels so that I can then 'draw' the string accurately on my screen.

      It all seemed so much easier in NT7.

      Comment


        #4
        If you're using TextLayout for the string drawing you should have created this object already in your code.

        Code:
        // Create your TextFormat object first by using a SimpleFont object that you have created. This gets passed into TextLayout
        TextFormat dxTextFormat = SimpleFont.ToDirectWriteTextFormat();
        
        // Create the TextLayout object with your string that you want to pass through.
        TextLayout textLayout = new TextLayout(Core.Globals.DirectWriteFactory, "Your String Here", dxTextFormat, chartPanel.H, (float)SimpleFont.Size);
        
        // Now that the TextLayout is created we can access its properties
        double textWidth = textLayout.Metrics.Width;

        Comment


          #5
          TextFormat and TextLayout are giving compile errors:
          Type or namespace not found.

          Which namespace do I need to declare?

          Also, where do I create chartPanel?

          Thanks for the help... so far.
          Last edited by FatCanary; 05-09-2016, 02:30 PM.

          Comment


            #6
            Sorted the Using declarations - SharpDX.DirectWrite.

            Now I can see that chartPanel refers to maxWidth.
            Last edited by FatCanary; 05-09-2016, 02:48 PM.

            Comment


              #7
              Where are you making this call? Indicator or Strategy? and in what method?

              Comment


                #8
                Indicator.

                At the moment it's in OnStateChange(), but not sure this is the right place.

                This is my first attempt at converting an NT7 indicator. Trust me to pick a difficult one.

                I've edited my earlier post. I found the Using declartions and so can now see that chartPanel refers to maxWidth.

                Comment


                  #9
                  I would not be doing any text rendering in the OnStateChange();

                  If anything use OnRender() method for the indicator.
                  http://ninjatrader.com/support/helpG.../?onrender.htm

                  Comment


                    #10
                    Many thanks. Code moved to OnRender()

                    Could you possibly expand on TextFormat please - not sure what you mean by ....using a SimpleFont object that you have created.
                    How would I set the font of a string to, say, Arial size 12?
                    In NT7 I had:
                    txtFont = new Font("Arial", fontSize);

                    Comment


                      #11
                      I don't have an accessible sample at hand for this, currently. Would need to get home tonight and pull it up. If someone else has one and beats me to punch feel free to post up.

                      Comment


                        #12
                        No problem, Thanks for the response, really appreciate the help.

                        I'm struggling to find a decent help resource with regard to SharpDX.

                        Comment


                          #13
                          Ok got you a sample put together.

                          The code comments should break it down for you, but if you got anymore questions feel free to ask
                          Attached Files

                          Comment


                            #14
                            Originally posted by Calonious View Post
                            Ok got you a sample put together.

                            The code comments should break it down for you, but if you got anymore questions feel free to ask
                            Still looks much easier in NT7.

                            Comment


                              #15
                              Hi Calonious

                              Did you do that sample just for me?
                              If so, many, many thanks
                              If not, then just many thanks

                              I'm still not seeing where I would set the font (e.g. Arial) and the size.
                              Last edited by FatCanary; 05-10-2016, 04:18 PM.

                              Comment

                              Latest Posts

                              Collapse

                              Topics Statistics Last Post
                              Started by andrewtrades, Today, 04:57 PM
                              1 response
                              9 views
                              0 likes
                              Last Post NinjaTrader_Manfred  
                              Started by chbruno, Today, 04:10 PM
                              0 responses
                              6 views
                              0 likes
                              Last Post chbruno
                              by chbruno
                               
                              Started by josh18955, 03-25-2023, 11:16 AM
                              6 responses
                              436 views
                              0 likes
                              Last Post Delerium  
                              Started by FAQtrader, Today, 03:35 PM
                              0 responses
                              8 views
                              0 likes
                              Last Post FAQtrader  
                              Started by rocketman7, Today, 09:41 AM
                              5 responses
                              19 views
                              0 likes
                              Last Post NinjaTrader_Jesse  
                              Working...
                              X