- ago
Also is it possible to have multiple colors in the same line when showing a line of text using DrawHeaderText()?
0
915
Solved
3 Replies

Reply

Bookmark

Sort
Glitch8
 ( 10.62% )
- ago
#1
It's possible to draw a line with multiple colors, but we don't have it baked into our framework (yet at least.) You have to rely on features of the .NET framework.

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using System.Drawing; namespace WealthScript1 { public class MyStrategy : UserStrategyBase { //Initialize public override void Initialize(BarHistory bars)       {          Bitmap bmp = new Bitmap(200, 50);          font = new Font("Arial", 10, FontStyle.Regular);          using(g = Graphics.FromImage(bmp))          {             DrawColoredText("Taste", Color.Red);             DrawColoredText("the", Color.Green);             DrawColoredText("Rainbow", Color.Blue);          }          DrawImageAt(bmp, 4, 20); } //Execute public override void Execute(BarHistory bars, int idx) { }       //private members       private Graphics g;       private Font font;       private float x = 0;       private void DrawColoredText(String txt, Color c)       {          g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;          g.DrawString(txt, font, new SolidBrush(c), x, 0);          x += g.MeasureString(txt, font).Width;       } } }


2
Best Answer
- ago
#2
Excellent!
Thanks @Glitch for the sample code!

This will work for my current use case. Once I get some time, I will actually develop it into a mini framework to allow adding rows of texts and appending to the rows and also allowing different font-size in the same row.
0
- ago
#3
Thanks for the code. I tried simplifying it, but C# insists on making DrawColoredText a class--unfortunately--which only seems to complicate it. Any help in simplifying the code would be welcomed.

The implementation below might want a second constructor. I declared the implementation "public", but it's a nested class within MyStrategy, so it's really "private" to that strategy. But if you move it into a library, you'll want it "public".

How do I automatically call a destructor when the DrawTextAt() method is called? (Or add an IDisposable method to use with a using statement?)

CODE:
using WealthLab.Backtest; using System; using WealthLab.Core; using System.Drawing; namespace WealthScript1 {    public class MyStrategy : UserStrategyBase    {       //Initialize       public override void Initialize(BarHistory bars)       {          DrawColoredText coloredText = new DrawColoredText(this as UserStrategyBase);          coloredText.AppendText("Taste", Color.Red);          coloredText.AppendText("the", Color.Green);          coloredText.AppendText("Rainbow", Color.Blue);          coloredText.DrawTextAt(2, 20);       }       //Execute       public override void Execute(BarHistory bars, int idx)       {       }       public class DrawColoredText       {          internal UserStrategyBase usb;          Bitmap bmp = new Bitmap(200, 50);          Font font;          Graphics graphic;          float textPosPointer = 0F;          public DrawColoredText(UserStrategyBase usbRef, int fontSize = 10, string fontName = "Arial")          {             usb = usbRef; //UserStrategyBase object reference             graphic = Graphics.FromImage(this.bmp);             font = new Font(fontName, fontSize, FontStyle.Regular);          }          public void AppendText(String txt, Color c)          {             graphic.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;             graphic.DrawString(txt, font, new SolidBrush(c), textPosPointer, 0);             textPosPointer += graphic.MeasureString(txt, font).Width;          }          public void DrawTextAt(double x, double y, string paneTag = "Price")          {             usb.DrawImageAt(bmp, x, y, paneTag);          }       }    } }
0

Reply

Bookmark

Sort