0

I want to use a winform inside MT4, which I know is possible. Metatrader4 has its own c++ flavored language, which can import .DLL-s, but we can only use stdcall CallingConvention.

I am using C# in Visual Studio 2015 to create the .DLL

My approach here is this:

From Metatrader 4 Terminal, I call a function in the .DLL to start the winform, I also pass the window handle as use it as an ID to track what chart just called the .DLL

I am storing the panel objects using a key, value dictionary.

Below is the process which loads the form.

// key,value dictionary holding forms
private static Dictionary<int, TradePanelForm> panels = new Dictionary<int, TradePanelForm>();


// start panel assosicated by window handle
[DllExport("OpenPanel", CallingConvention.StdCall)]
public static void OpenPanel(int inMT4hwnd)
{
   if (!panels.ContainsKey(inMT4hwnd))
   {
     // add the panel object to the dictionary list
     panels.Add(inMT4hwnd, new TradePanelForm(inMT4hwnd)); 
   }

   Application.Run(panels[inMT4hwnd]);          
   // panels[inMT4hwnd].Show();           
}

If I use

Application.Run(panels[inMT4hwnd]);

the form will open, but it seems to be 'in focus' and Metatrader 4 Terminal seems to become unresponsive until I close the panel.

Form loaded with application run method

If I load the panel this way

panels[inMT4hwnd].Show();

the panel partially loads, with the buttons whited out, and the both the panel, and Metatrader 4 Terminal hangs.

Q1: What is the correct way to be doing this?

Q2: Is there some special winAPI stuff I need to dance around to make the form work properly within the metatrader process?

I've looked up threads, tasks, async stuff. Could somebody usher me towards the right direction here.

It would be nice if I could get the form to load 'inside' Metatrader's window as like a child window.

Community
  • 1
  • 1
Dale Woods
  • 784
  • 1
  • 13
  • 31
  • I don't know about MetaTrader, but I can tell you that your `Application.Run` approach is probably wrong, because `Application.Run` starts a new WinForms "application". `Run` will not return until the last application window is closed, so what happens is that the `OpenPanel` function is called, but doesn't return. That's why the caller is blocked => MetaTrader doesn't respond until you close the panel and thus make `Run` return. – Thorsten Dittmar Nov 21 '16 at 12:59
  • Thank you sir, you are very correct. How do I open a form with myForm.show() and stop it from close (in a testing environment). – Dale Woods Nov 21 '16 at 13:01
  • Well, as I said, I don't know jack about MetaTrader, but I'd look at some C++ samples and see what they do. – Thorsten Dittmar Nov 21 '16 at 13:03

0 Answers0