//Add the following Using statements
using System.Windows.Media.Imaging;
using System.Windows.Controls;
public class addButton : Indicator
{
// Define a Chart object to refer to the chart on which the indicator resides
private Chart chartWindow;
// Define a Button
private System.Windows.Controls.Button myButton = null;
// Instantiate a BitmapImage to hold an image
BitmapImage myBitmapImage = new BitmapImage();
// Instantiate an ImageBrush to apply to the Button
ImageBrush backgroundImage = new ImageBrush();
private bool IsToolBarButtonAdded;
protected override void OnStateChange()
{
if (State == State.Configure)
{
// Assign an image on the filesystem to the BitmapImage.
// This example assumes that a jpg image named "ButtonBackground" resides in the install directory
myBitmapImage.BeginInit();
myBitmapImage.UriSource = new Uri(NinjaTrader.Core.Globals.InstallDir + "ButtonBackground.jpg");
myBitmapImage.EndInit();
// Assign the BitmapImage as the ImageSource of the ImageBrush
backgroundImage.ImageSource = myBitmapImage;
}
else if (State == State.Historical)
{
//Call the custom addButtonToToolbar method in State.Historical to ensure it is only done when applied to a chart
// -- not when loaded in the Indicators window
if (!IsToolBarButtonAdded) AddButtonToToolbar();
}
else if (State == State.Terminated)
{
//Call a custom method to dispose of any leftover objects in State.Terminated
DisposeCleanUp();
}
}
private void AddButtonToToolbar()
{
// Use this.Dispatcher to ensure code is executed on the proper thread
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
//Obtain the Chart on which the indicator is configured
chartWindow = Window.GetWindow(this.ChartControl.Parent) as Chart;
if (chartWindow == null)
{
Print("chartWindow == null");
return;
}
// Create a style to apply to the button
Style s = new Style();
s.TargetType = typeof(System.Windows.Controls.Button);
s.Setters.Add(new Setter(System.Windows.Controls.Button.FontSizeProperty, 11.0));
s.Setters.Add(new Setter(System.Windows.Controls.Button.BackgroundProperty, Brushes.Orange));
s.Setters.Add(new Setter(System.Windows.Controls.Button.ForegroundProperty, Brushes.Black));
s.Setters.Add(new Setter(System.Windows.Controls.Button.FontFamilyProperty, new FontFamily("Arial")));
s.Setters.Add(new Setter(System.Windows.Controls.Button.FontWeightProperty, FontWeights.Bold));
// Instantiate the Button
myButton = new System.Windows.Controls.Button();
//Set Button Style
myButton.Style = s;
// Set the Imagebrush as the Background for the Button
myButton.Background = backgroundImage;
myButton.Content = "Click Here";
myButton.IsEnabled = true;
myButton.HorizontalAlignment = HorizontalAlignment.Left;
// Add the Button to the Chart's Toolbar
chartWindow.MainMenu.Add(myButton);
//Prevent the Button From Displaying when WorkSpace Opens if it is not in an active tab
myButton.Visibility = Visibility.Collapsed;
foreach (TabItem tab in this.chartWindow.MainTabControl.Items)
{
if ((tab.Content as ChartTab).ChartControl == this.ChartControl
&& tab == this.chartWindow.MainTabControl.SelectedItem)
{
myButton.Visibility = Visibility.Visible;
}
}
IsToolBarButtonAdded = true;
}));
}
private void DisposeCleanUp()
{
//ChartWindow Null Check
if (chartWindow != null)
{
//Dispatcher used to Assure Executed on UI Thread
ChartControl.Dispatcher.InvokeAsync((Action)(() =>
{
//Button Null Check
if (myButton != null)
{
//Remove Button from Indicator's Chart ToolBar
chartWindow.MainMenu.Remove(myButton);
}
}));
}
}
}