Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Custom Drawing for an AddOn...

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

    Custom Drawing for an AddOn...

    Hi there, have looked through threads didn't see anything quite like my question so...

    I have created AddOn which outputs and updates Text to a TextBox control. Works great.

    The TextBox however is limited to solid Fore/Back colors and would like to write my own control which lets me color the Text, as well as other drawing primitives like Rectangles and such around blocks of text. Basically an owner drawn control but hosted in the AddOn.

    In the C++ world have done this using a Button which is OwnerDrawn then do anything want rendering it in the OnPaint() method.

    Has NinjaTrader created any code samples which show owner drawn controls in an AddOn? They could be charts or even just a line of text.

    I'm new to NT and .Net programming still old school from MFC days in Windows. But learning...

    Thanks for any guidance!

    #2
    Hello,

    Thank you for the post.

    In this case, this wouldn't necessarily be something we would provide a sample of as this is a C#/Xaml concept.

    In the case just colors were needed, you could set the existing Background/Foreground properties for the TextBox. For custom drawing or otherwise a custom Control, you would need to create a UserControl in C# or C#+Xaml and then use that control instead.

    This isn't something that is easily explained in a forum post and is well documented in other locations. It sounds like you have some experience in creating controls in C++, it is a really similar process although the processes are different as Windows Forms is no longer used and WPF is now used. In general, you can accomplish the same goals of overriding the properties of a control to do custom things.

    Because of the nature of how NinjaTrader compiles, you would need to create a new UserControl in either all C# with no xaml, or create a new project in visual studio and compile the new control into an assembly which could be referenced in NinjaScript. NinjaTrader will not specifically compile the xaml so either all code would be needed or an assembly with your control compiled would need to be used.

    I located the following resource using google which may be helpful in learning how to create a custom control in WPF:


    WPF - Custom Controls - WPF applications allows to create custom controls which makes it very easy to create feature-rich and customizable controls. Custom controls are used when all the built-in controls provided by Microsoft are not fulfilling your criteria or you don’t want to pay for third-party controls.


    For extending existing controls which may be what you want to do:
    Christian Moser's WPF Tutorial. An elaborate tutorial about the Windows Presentation Foundation with hunderts of samples.

    I would like an additional WPF control that would add an int property to the TextBox class. I have tried Project > Add New Item > Custom Control (WPF). That gave me a new cs file for the new Contro...

    My attempt to create a CustomControl like a TextBox with variable caption. I try to learn create CustomControl and my exceptation is that my CustomControl (I called it TextBoxCustomControl) has ev...


    Please also keep in mind that making custom controls will likely break the skin for that control meaning it would likely look strange or just like a default WPF control. In that situation, you may need to additionally apply custom styles or internal styles to the object to make it look similar to the existing platform controls.


    I look forward to being of further assistance.
    JesseNinjaTrader Customer Service

    Comment


      #3
      Great thanks for the reply and links to articles Jesse! I have been hunting around but these are excellent direction. I might be able to simply subclass from the TextBox and extend the painting dynamics. Otherwise full custom control will work.

      Interestingly this is all going back to Terminal Emulation days what am needing to do.... These got really involved prior to bitmap graphics taking off... Wonder if anyone out there still remembers how that worked! Has been over twenty years now..

      Comment


        #4
        Hi again Jesse!

        Well I stumbled around for a bit but now have my own user drawn control which created and am rendering correctly external to NT. Have now tried to add this into NT itself as a control but not quite sure what need to do to bring it into the Borg Collective. Have tried adding "using" statement and put the correct entry in the xaml but think NT needs to know more where to find this new content.

        Are there some config files need to tweak with NT that will let me do this???

        Thanks again!

        Comment


          #5
          Hello,

          Thank you for the reply.

          I take it that you have created this control in an external C# project currently, is that correct? If so, are you compiling a DLL and need to reference that DLL or are you copying/pasting code from that project into a NinjaScript file?

          If you have an external project that creates a DLL, you would need to reference that DLL to be able to use "using" statements. In the case you had not referenced the DLL yet, this is likely why you can't use the "using" statements.
          To add a reference, add the DLL to the \Documents\NinjaTrader 8\bin\Custom directory and then in the NinjaScript editor, right click and choose References and then add a reference to the file copied to the \Documents\NinjaTrader 8\bin\Custom folder.

          After doing this, complete a compile for the editor to pick up the new code.


          I look forward to being of further assistance.

          If you are working on an external project, I would highly suggest looking at the addon project we provide in the help guide, this demonstrates a "suggested" way to create DLLs for NinjaScript that may contain an Addon or in your case a Control.

          The section that lists" Below is a complete Visual Studio project with this setup in place".

          I look forward to being of further assistance.
          JesseNinjaTrader Customer Service

          Comment


            #6
            Thanks again for the helping hand! Yes this is as a DLL and once it was put into the right directory and reference added all the compilation and link occurred correctly - easy!

            The issue now is something with the XAML and/or my logic picking up the control at runtime. Am getting a null pointer return. What have done is replaced a TextBox named "outputBox" with a JCBUserControl:UserControl1 but it does not hook up correctly....

            Not that I want you to fully debug my code, but as this is new stuff to me perhaps a seasoned eye can save me a day trying to track this down with web searches??

            the Hookup code for the old and the new is:

            // Find Output Box - old way gets me the TextBox
            outputBox = LogicalTreeHelper.FindLogicalNode(pageContent, "outputBox") as TextBox;

            // Find my UserControl1 it returns NULL
            JCBOutput = LogicalTreeHelper.FindLogicalNode(pageContent, "JCBControl") as JCBUserControl.UserControl1;

            while the XAML which is not working is this (note red):

            <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:t="clr-namespace:NinjaTrader.Gui.Tools;assembly=NinjaTrad er.Gui"
            xmlns:system="clr-namespace:System;assembly=mscorlib"
            xmlns:AccountPerformance="clr-namespace:NinjaTrader.Gui.AccountPerformance;assem bly=NinjaTrader.Gui"
            xmlns:AccountData="clr-namespace:NinjaTrader.Gui.AccountData;assembly=Nin jaTrader.Gui"
            xmlns:AtmStrategy="clr-namespace:NinjaTrader.Gui.NinjaScript.AtmStrategy; assembly=NinjaTrader.Gui" >
            xmlns:JCBUserControl="clr-namespace:JCBUserControl;assembly=JCBUserControl"
            <Grid Background="Transparent">
            <JCBUserControl:UserControl1 x:Name="JCBControl" Grid.Column="1" />
            </Grid>
            </Page>

            and what worked with the simple TextBox is this:

            <Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:t="clr-namespace:NinjaTrader.Gui.Tools;assembly=NinjaTrad er.Gui"
            xmlns:system="clr-namespace:System;assembly=mscorlib"
            xmlns:AccountPerformance="clr-namespace:NinjaTrader.Gui.AccountPerformance;assem bly=NinjaTrader.Gui"
            xmlns:AccountData="clr-namespace:NinjaTrader.Gui.AccountData;assembly=Nin jaTrader.Gui"
            xmlns:AtmStrategy="clr-namespace:NinjaTrader.Gui.NinjaScript.AtmStrategy; assembly=NinjaTrader.Gui" >
            <Grid Background="Transparent">
            <TextBox x:Name="outputBox" Margin="{Binding Source={StaticResource MarginBase}}" Grid.Column="1" IsReadOnly="True" TextWrapping="Wrap" VerticalScrollBarVisibility="Disabled"
            FontFamily="Courier New" FontSize="11" FontStyle="Normal" FontWeight="Normal" Background="Black"/>

            </Grid>
            </Page>

            Thanks for any further help Jesse you have been very helpful so far! I'll figure out this .NET stuff yet.... (programming windows since '92 but stopped at MFC...)

            Comment


              #7
              Hello,

              Thank you for the reply.

              That looks like the correct snytax, out of context it would be difficult to say.

              Rather than debugging what you have tried, I have created a sample of a custom user control that inherits from TextBox.

              This is a little outside of what we would normally provide for a sample but because of the odd loading we use for the addon I feel this is the easier solution.

              I have included all the necessary parts, the visual studio solution to build the assembly with the control, and an Addon that uses the control.

              I added a custom dependency property to the control to demonstrate the Render override in the control also which is set in the xaml.

              The same steps would apply, you would first need to build the DLL in visual studio and copy it to the bin/custom folder and reference it.

              After doing this, you should be able to import the other zip with the addon

              Finally, you would need to extract the xaml file into the bin/custom/addons folder as well.



              I look forward to being of further assistance.
              Attached Files
              JesseNinjaTrader Customer Service

              Comment


                #8
                hey thanks very much Jesse! that is above and beyond the call of duty IMHO! your company has been very helpful indeed.

                i will check it out after market closes today should be quite helpful. thx again!

                -Jim

                [Update: your sample worked great. something weird was going on with my xaml that still can't explain but starting from yours was eventually able to get it to parse mine. it appears the parser was getting confused somehow. so actually when got past that my UserControl became active and worked like it did in my stub test program without any changes. whatever the xaml logic was doing seems pretty fuzzy with bizarre behavior. so things are running now can now augment my user control! thanks again!]
                Last edited by JimB17; 05-24-2017, 12:18 PM.

                Comment

                Latest Posts

                Collapse

                Topics Statistics Last Post
                Started by DanielSanMartin, Yesterday, 02:37 PM
                2 responses
                12 views
                0 likes
                Last Post DanielSanMartin  
                Started by DJ888, 04-16-2024, 06:09 PM
                4 responses
                12 views
                0 likes
                Last Post DJ888
                by DJ888
                 
                Started by terofs, Today, 04:18 PM
                0 responses
                11 views
                0 likes
                Last Post terofs
                by terofs
                 
                Started by nandhumca, Today, 03:41 PM
                0 responses
                7 views
                0 likes
                Last Post nandhumca  
                Started by The_Sec, Today, 03:37 PM
                0 responses
                3 views
                0 likes
                Last Post The_Sec
                by The_Sec
                 
                Working...
                X