How To use a winows Forms in cAlgo ....

Created at 01 Nov 2014, 16:58
How’s your experience with the cTrader Platform?
Your feedback is crucial to cTrader's development. Please take a few seconds to share your opinion and help us improve your trading experience. Thanks!
MehranFX's avatar

MehranFX

Joined 01.11.2014

How To use a winows Forms in cAlgo ....
01 Nov 2014, 16:58


Hi Dear:

I have Developed an cBot and want to load a winows Form as a specific perpouse, the code i used is here:

           Rectangle workingArea = Screen.PrimaryScreen.WorkingArea;
            Form fm = new Form();
            fm.ClientSize = new Size(200, 200);
            int left = workingArea.Width - fm.Width;
            int top = workingArea.Height - fm.Height;
            fm.Location = new Point(left, top);
            fm.ShowInTaskbar = false;
            fm.ShowIcon = false;
            fm.MinimizeBox = false;
            fm.MaximizeBox = false;
            fm.FormBorderStyle = FormBorderStyle.FixedToolWindow;
            fm.Text = "Test";            
            fm.Show();

every things is OK but the generated window is Hanging ! like picture below :

Can Someone  Help Me How I Can Solve this Issue?

With Regards....


@MehranFX
Replies

Elogos
01 Nov 2014, 20:09

Might be do with the thread the form was created in.

I'll have a look when I get a bit of time.


@Elogos

Elogos
01 Nov 2014, 20:36

RE:

Elogos said:

Might be do with the thread the form was created in.

I'll have a look when I get a bit of time.

Yep, it is created in the cTrader main thread and Show() isn't returning.

I have some developers notes somewhere from a Spotware developer on creating a new WPF Window. I'll see if I can dig them up somewhere.


@Elogos

MehranFX
01 Nov 2014, 21:25

RE: RE:

Elogos said:

Elogos said:

Might be do with the thread the form was created in.

I'll have a look when I get a bit of time.

Yep, it is created in the cTrader main thread and Show() isn't returning.

I have some developers notes somewhere from a Spotware developer on creating a new WPF Window. I'll see if I can dig them up somewhere.

Thanks Elogos , I'm waiting for you!

with regards...


@MehranFX

Elogos
02 Nov 2014, 00:00

ShowDialog() doesn't produce the same effect.


@Elogos

Elogos
02 Nov 2014, 00:11

RE: RE:

Elogos said:

I have some developers notes somewhere from a Spotware developer on creating a new WPF Window. I'll see if I can dig them up somewhere.

I have looked for the notes, but it seems to be hidden somewhere in an old Archive.

I'll keep searching, but Perhaps I hope that the same person will repost here.


@Elogos

Elogos
02 Nov 2014, 00:14

        public class AlertWindowWrapper : MarshalByRefObject
        {
            private static Thread _windowThread;
            private static readonly AutoResetEvent LoadedEvent = new AutoResetEvent(false);
            private static readonly object SyncObject = new object();
 
            private static object _window;
            private static AlertWindowModel _alertWindowModel;
 
            public void ShowAlert(string message)
            {
                lock (SyncObject)
                {
                    if (_window == null)
                        CreateWindow();
 
                    _alertWindowModel.Message = message;
                    var items = new List<AlertItem>(_alertWindowModel.Items);
                    items.Insert(0, new AlertItem(DateTime.Now, message));
                    _alertWindowModel.Items = items;
                }
            }
 
            private void CreateWindow()
            {
                _windowThread = new Thread(() =>
                {
                    try
                    {
                        _window = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.Window");
                        SubscribeToEvent(_window, "Loaded", "Window_Loaded");
                        SubscribeToEvent(_window, "Closing", "Window_Closing");
                        ReflectionHelper.SetProperty(_window, "ShowInTaskbar", true);
                        ReflectionHelper.SetProperty(_window, "ShowActivated", true);
                        ReflectionHelper.SetProperty(_window, "Width", 525);
                        ReflectionHelper.SetProperty(_window, "Height", 400);
                        ReflectionHelper.SetProperty(_window, "WindowStartupLocation", ReflectionHelper.GetStaticValue(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.WindowStartupLocation", "CenterScreen"));
                        ReflectionHelper.SetProperty(_window, "WindowStyle", ReflectionHelper.GetStaticValue(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.WindowStyle", "ToolWindow"));
                        ReflectionHelper.SetProperty(_window, "Title", "Alert");
                        var rootGrid = WpfReflectionHelper.CreateGrid();
                        WpfReflectionHelper.SetMargin(rootGrid, 5);
                        WpfReflectionHelper.AddAutoRowDefinition(rootGrid);
                        WpfReflectionHelper.AddStarRowDefinition(rootGrid);
 
                        var messageGrid = WpfReflectionHelper.CreateGrid();
                        WpfReflectionHelper.AddAutoColumnDefinition(messageGrid);
                        WpfReflectionHelper.AddStarColumnDefinition(messageGrid);
 
                        var image = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.Controls.Image");
                        WpfReflectionHelper.SetMargin(image, 20);
                        var bitmapImage = Base64ToImage("iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAS/0lEQVR42u2ae6xlV33fP+uxn2efc+49996ZOw/PeLDnYWzjUY2NQTxK3cQlGLVBlKKmIqVBTZES2qZBNEFJIaFt1KZRUJRWqBUNtFJRTAKBPkJJIhVDoDEkwY9gbDxjz/vOfZ999tnPtVb/2Gfm3jtzZwDb+E5FtrS079065+y1vuv7+/5+6/f7KXboyr75T1/7E2+U73/gjvLN9+xn+PlH61M7MQ+1Ey+tV/7TjwX7HvycN/rGvcvnn73n/LnsXZHT337yonnspZ6L3AkAmvN/8ivmzGdFtnSeleURdx0fiK89W/3KTsxlRwBYOn1m/9P/57/x+GNPcOb0mHv+5j+hN+vt34m56J146Vrq89sP/QXSOJ55DtZ+41OcWjD8wDAgnjvI1x93nFvWEAd85tNf54Fjlh8YBnT7fV55BF7/hj6dJOa/f+40/R78zp/9gDDAuZ3Z7e0u8VK85GM/Fd401Q+Oah3e7Af+rpfddvu/fObRpzh6bEAYR5w8sYKzFXESfsCY8mKe588O0/GTD/7i8pn/LwH4wI+o2W5s39pNeFOau9c2Rs32e5r9eyPmdyf4nkcQBGjPYy1rqGrDeJxTlhV1VWNMgzMWgV2yTfWlNG3+1/qQ333fJ1m6oQH49Xd0Do/z4oNxh7c9/KjxswLe/qaEO26fpdefJogitA6Ikz5BZwoVxkgvxNmGOhtSjlPG6Qqry0ssXFjg5MlVzp6vMRYij8pXfGo05oO/9D94+oYD4Jff7L86z6sv3HV8V2f/LfO8472P8vP/sM/9b7iF/uw8nd40fhgjpYeMpxFhDJ4PygdnoS7BWOx4jfHqOdYunuHsqWc4+fQZFpcc5xchy6Ebk5UNP/SrX+ArN4wI/sIPS5mOqo/fdCDpPPCm4/h+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BZCeLR9TL619tLfnnNOW2v1xPVKNvrhxKYGISuEMM65RkrZCCEaoAZqKWUF1EKIummaevLcFEXRKKVMWZa2LEtb17UTQrhDhw65FyUf8D353TQVm6pAwvM8IYTAOdee7dpgQlyvY2ziLZwQwk3uGGMwxrQsEQIhBN1u1/GX119eL/j6f13TfTYXJjiZAAAAAElFTkSuQmCC");
                        ReflectionHelper.SetProperty(image, "Source", bitmapImage);
                        WpfReflectionHelper.AddChild(messageGrid, image);
 
                        var messageTextBlock = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.Controls.TextBlock");
 
                        WpfReflectionHelper.SetGridColumn(messageTextBlock, 1);
                        ReflectionHelper.SetProperty(messageTextBlock, "HorizontalAlignment", ReflectionHelper.GetEnumValue(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.HorizontalAlignment", "Left"));
                        ReflectionHelper.SetProperty(messageTextBlock, "TextWrapping", ReflectionHelper.GetEnumValue(WpfReflectionHelper.PresentationCoreAssembly, "System.Windows.TextWrapping", "Wrap"));
                        ReflectionHelper.SetProperty(messageTextBlock, "VerticalAlignment", ReflectionHelper.GetEnumValue(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.VerticalAlignment", "Center"));
                        ReflectionHelper.SetProperty(messageTextBlock, "FontSize", 18);
                        WpfReflectionHelper.SetBinding(messageTextBlock, "Message", "System.Windows.Controls.TextBlock", "TextProperty");
                        WpfReflectionHelper.AddChild(messageGrid, messageTextBlock);
 
                        var allAlertsGrid = WpfReflectionHelper.CreateGrid();
                        WpfReflectionHelper.SetGridRow(allAlertsGrid, 1);
                        WpfReflectionHelper.AddStarRowDefinition(allAlertsGrid);
                        WpfReflectionHelper.AddAutoRowDefinition(allAlertsGrid);
                        var dataGrid = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.Controls.DataGrid");
                        WpfReflectionHelper.SetBinding(dataGrid, "Items", "System.Windows.Controls.ItemsControl", "ItemsSourceProperty");
                        WpfReflectionHelper.AddChild(allAlertsGrid, dataGrid);
                        var closeButton = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationFrameworkAssembly, "System.Windows.Controls.Button");
                        SubscribeToEvent(closeButton, "Click", "ButtonOnClick");
                        WpfReflectionHelper.SetGridRow(closeButton, 1);
                        ReflectionHelper.SetProperty(closeButton, "Width", 100);
                        WpfReflectionHelper.SetMargin(closeButton, 0, 5, 0, 0);
                        ReflectionHelper.SetProperty(closeButton, "Content", "Close");
                        WpfReflectionHelper.AddChild(allAlertsGrid, closeButton);
 
                        WpfReflectionHelper.AddChild(rootGrid, messageGrid);
                        WpfReflectionHelper.AddChild(rootGrid, allAlertsGrid);
 
                        ReflectionHelper.SetProperty(_window, "Content", rootGrid);
                        _alertWindowModel = new AlertWindowModel();
                        ReflectionHelper.SetProperty(_window, "DataContext", _alertWindowModel);
 
                        ReflectionHelper.InvokeMethod(_window, "ShowDialog");
                    } catch (Exception exception)
                    {
                    }
                }) 
                {
                    IsBackground = false,
                    Name = "Alert Window thread"
                };
 
                _windowThread.TrySetApartmentState(ApartmentState.STA);
                _windowThread.Start();
                LoadedEvent.WaitOne();
            }
 
            private static object Base64ToImage(string stringValue)
            {
 
                var binaryData = Convert.FromBase64String(stringValue);
 
                var bitmapImage = ReflectionHelper.CreateInstance(WpfReflectionHelper.PresentationCoreAssembly, "System.Windows.Media.Imaging.BitmapImage");
 
                ReflectionHelper.InvokeMethod(bitmapImage, "BeginInit");
                ReflectionHelper.SetProperty(bitmapImage, "StreamSource", new MemoryStream(binaryData));
                ReflectionHelper.InvokeMethod(bitmapImage, "EndInit");
 
                return bitmapImage;
            }
 
            public void ButtonOnClick(object sender, object routedEventArgs)
            {
                ReflectionHelper.InvokeMethod(_window, "Close");
            }
 
            public void Window_Loaded(object s, object e)
            {
                LoadedEvent.Set();
            }
 
            public void Window_Closing(object sender, CancelEventArgs e)
            {
                _window = null;
                _alertWindowModel = null;
            }
 
            private void SubscribeToEvent(object @object, string eventName, string handlerName)
            {
                var eventInfo = @object.GetType().GetEvent(eventName);
                var methodInfo = GetType().GetMethod(handlerName);
                var handler = Delegate.CreateDelegate(eventInfo.EventHandlerType, this, methodInfo);
                eventInfo.AddEventHandler(@object, handler);
            }
        }
 
        public class AlertWindowModel : INotifyPropertyChanged
        {
            private string _message;
            private IEnumerable<AlertItem> _items = new List<AlertItem>();
 
            public string Message
            {
                get { return _message; }
                set
                {
                    if (_message == value)
                        return;
 
                    _message = value;
                    OnPropertyChanged("Message");
                }
            }
 
            public IEnumerable<AlertItem> Items
            {
                get { return _items; }
                set
                {
                    if (_items == value)
                        return;
 
                    _items = value;
                    OnPropertyChanged("Items");
                }
            }
 
 
            public event PropertyChangedEventHandler PropertyChanged;
 
            protected virtual void OnPropertyChanged(string propertyName)
            {
                var handler = PropertyChanged;
                if (handler != null)
                    handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
 
        public class AlertItem
        {
            public DateTime Time { get; private set; }
            public string Message { get; private set; }
 
            public AlertItem(DateTime time, string message)
            {
                Time = time;
                Message = message;
            }
        }
 
        public static class WpfReflectionHelper
        {
            public static Assembly PresentationFrameworkAssembly = Assembly.Load("PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
            public static Assembly PresentationCoreAssembly = Assembly.Load("PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35");
 
            public static void SetMargin(object rootGrid, params object[] parameters)
            {
                var thickness = ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.Thickness", parameters);
                ReflectionHelper.SetProperty(rootGrid, "Margin", thickness);
            }
 
            public static object CreateGrid()
            {
                return ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.Controls.Grid");
            }
 
            public static void AddChild(object grid, object child)
            {
                var rootGridChildren = ReflectionHelper.GetPropertyValue(grid, "Children");
                ReflectionHelper.InvokeMethod(rootGridChildren, "Add", child);
            }
 
            private static readonly object AutoGridLegth = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, "System.Windows.GridLength", "Auto");
 
            public static void AddAutoRowDefinition(object grid)
            {
                var rowDefinition = CreateRowDefinition();
                ReflectionHelper.SetProperty(rowDefinition, "Height", AutoGridLegth);
                AddRowDefinition(grid, rowDefinition);
            }
 
            public static void AddStarRowDefinition(object grid)
            {
                var rowDefinition = CreateRowDefinition();
                var starUnitType = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, "System.Windows.GridUnitType", "Star");
                var gridLength = ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.GridLength", new[] 
                {
                    1,
                    starUnitType
                });
                ReflectionHelper.SetProperty(rowDefinition, "Height", gridLength);
                AddRowDefinition(grid, rowDefinition);
            }
 
            private static object CreateRowDefinition()
            {
                var rowDefinition = ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.Controls.RowDefinition");
                return rowDefinition;
            }
 
            public static void AddRowDefinition(object grid, object rowDefinition)
            {
                var rowDefinitions = ReflectionHelper.GetPropertyValue(grid, "RowDefinitions");
                ReflectionHelper.InvokeMethod(rowDefinitions, "Add", rowDefinition);
            }
 
            public static void AddAutoColumnDefinition(object grid)
            {
                var ColumnDefinition = CreateColumnDefinition();
                ReflectionHelper.SetProperty(ColumnDefinition, "Width", AutoGridLegth);
                AddColumnDefinition(grid, ColumnDefinition);
            }
 
            public static void AddStarColumnDefinition(object grid)
            {
                var ColumnDefinition = CreateColumnDefinition();
                var starUnitType = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, "System.Windows.GridUnitType", "Star");
                var gridLength = ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.GridLength", new[] 
                {
                    1,
                    starUnitType
                });
                ReflectionHelper.SetProperty(ColumnDefinition, "Width", gridLength);
                AddColumnDefinition(grid, ColumnDefinition);
            }
 
            private static object CreateColumnDefinition()
            {
                var ColumnDefinition = ReflectionHelper.CreateInstance(PresentationFrameworkAssembly, "System.Windows.Controls.ColumnDefinition");
                return ColumnDefinition;
            }
 
            public static void AddColumnDefinition(object grid, object ColumnDefinition)
            {
                var ColumnDefinitions = ReflectionHelper.GetPropertyValue(grid, "ColumnDefinitions");
                ReflectionHelper.InvokeMethod(ColumnDefinitions, "Add", ColumnDefinition);
            }
 
            public static void SetGridColumn(object element, int column)
            {
                var columnProperty = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, "System.Windows.Controls.Grid", "ColumnProperty");
                ReflectionHelper.InvokeMethod(element, "SetValue", columnProperty, column);
            }
 
            public static void SetGridRow(object element, int row)
            {
                var columnProperty = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, "System.Windows.Controls.Grid", "RowProperty");
                ReflectionHelper.InvokeMethod(element, "SetValue", columnProperty, row);
            }
 
            public static void SetBinding(object element, string path, string propertyTypeName, string propertyName)
            {
                var property = ReflectionHelper.GetStaticValue(PresentationFrameworkAssembly, propertyTypeName, propertyName);
                ReflectionHelper.InvokeMethod(element, "SetBinding", property, path);
            }
        }
 
        public static class ReflectionHelper
        {
            public static object GetEnumValue(Assembly assembly, string typeName, string value)
            {
                var type = assembly.GetType(typeName);
                return Enum.Parse(type, value);
            }
 
            public static void InvokeMethod(object instance, string methodName, params object[] parameters)
            {
                instance.GetType().InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, parameters);
            }
 
            public static object InvokeStaticMethod(Assembly assembly, string typeName, string methodName, params object[] parameters)
            {
                return assembly.GetType(typeName).InvokeMember(methodName, BindingFlags.InvokeMethod, null, null, parameters);
            }
 
            public static object GetStaticValue(Assembly assembly, string typeName, string propertyName)
            {
                var type = assembly.GetType(typeName);
                var propertyInfo = type.GetProperty(propertyName);
                if (propertyInfo != null)
                    return propertyInfo.GetValue(null, null);
 
                var fieldInfo = type.GetField(propertyName);
                return fieldInfo.GetValue(null);
            }
 
            public static void SetProperty(object instance, string propertyName, Assembly assembly, string typeName, params object[] parameters)
            {
                var propertyInfo = instance.GetType().GetProperty(propertyName);
                var propertyValue = CreateInstance(assembly, typeName, parameters);
                propertyInfo.SetValue(instance, propertyValue, null);
            }
 
            public static void SetProperty(object instance, string propertyName, object value)
            {
                var propertyInfo = instance.GetType().GetProperty(propertyName);
                propertyInfo.SetValue(instance, value, null);
            }
 
            public static object GetPropertyValue(object instance, string propertyName)
            {
                var propertyInfo = instance.GetType().GetProperty(propertyName);
                return propertyInfo.GetValue(instance, null);
            }
 
            public static object CreateInstance(Assembly assembly, string typeName)
            {
                return CreateInstance(assembly, typeName, new object[0]);
            }
 
            public static object CreateInstance(Assembly assembly, string typeName, object[] parameters)
            {
                var pointType = assembly.GetType(typeName);
                var propertyValue = Activator.CreateInstance(pointType, parameters);
                return propertyValue;
            }
        }
        //}
 
        private int _lastError;
 
        Mq4Double IsTesting()
        {
            return IsBacktesting;
        }
 
 

 


@Elogos

Elogos
02 Nov 2014, 00:22

Got it!

CreateWindow method in this code from the MT4 to cAlgo library.

The dialog box that is available in MT4 can be replicated using an WPF window.

That code is how they did it.

 

I have adapted it in the past before for my use for an old Stochastic alert dashboard thing that I did which since has slipped out of usage. I will keep digging to find the code, but I think that above should suffice.

 

It can be found here fully:

/forum/indicator-support/3283

 


@Elogos

MehranFX
02 Nov 2014, 06:40 ( Updated at: 23 Jan 2024, 13:16 )

RE:

Elogos said:

Got it!

CreateWindow method in this code from the MT4 to cAlgo library.

The dialog box that is available in MT4 can be replicated using an WPF window.

That code is how they did it.

 

I have adapted it in the past before for my use for an old Stochastic alert dashboard thing that I did which since has slipped out of usage. I will keep digging to find the code, but I think that above should suffice.

 

It can be found here fully:

/forum/indicator-support/3283

 

[Elogos !]

You Are Great Man! Thanks a lot ! I will work on it.

Cheers ....


@MehranFX

Elogos
05 Nov 2014, 01:06

Did you have any success?


@Elogos