Page 1 of 1

embedding the matrix into the application form

Posted: 25 Oct 2011, 18:20
by Nourah
Hello everybody…

I have a c# application connected with BCI2000 by UDP channel, at the beginning of each run I open the bci2000 from the code by opening the .bat file.with this commends

Code: Select all

cd C:/BCI2000/prog
start operator.exe --OnConnect "-LOAD PARAMETERFILE C:\BCI2000\parms\examples\P3Speller_CopySpelling.prm; SETCONFIG" 
start SignalGenerator.exe 127.0.0.1
start P3SignalProcessing.exe 127.0.0.1

after that i need to make th P3 Speller matrix appear in the same application form.

Code: Select all

private void button1_Click_1(object sender, EventArgs e)
        {
            Process p = null;
            IntPtr appWin = IntPtr.Zero; ;
            try
            {
                Process.Start("C:\\BCI2000\\batch\\ttrryy.bat");
                // Start the process
             
                p = System.Diagnostics.Process.Start(@"C:\\BCI2000\\prog\\P3Speller.exe");
               
                // Wait for process to be created and enter idle condition 

                p.WaitForInputIdle();

                // Get the main handle               

                appWin = p.MainWindowHandle;
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error");
            }
            // Put it into this form

            SetParent(appWin, this.panel1.Handle);

            // Remove border and whatnot

          //  SetWindowLong(appWin, GWL_STYLE, WS_VISIBLE);

            // Move the window to overlay it on this window

            MoveWindow(appWin, 0, 0, this .panel1.Width, this.panel1.Height, true);
           
        }

       .................
       ....................
       ..................
       ................
          
when i replace the "P3Speller.exe" with any other application (for example "notepad.exe" )the code able to open and embed the application to the form but in case of P3Speller.exe it open it out of the panel

i think the problem because P3Speller.exe open many windows not only one...
so i tried to access the sub-windows by searching for their names to use them but i could not get anything from the source code BCI2000src\src\core\Application\P3Speller

does any one have any idea how to get around this ?

thank you in advance


Thank you ………….

Re: embedding the matrix into the application form

Posted: 28 Oct 2011, 11:52
by mellinger
Hi,

that's an interesting way to include foreign program windows into one's own application.

To find out about BCI2000 windows, there is a helpful tool called Spy++ that comes with VisualStudio. It gives an overview of all windows currently open, and allows to identify windows by clicking/highlighting.
Using Spy++, I can tell that the P3Speller actually has a main window, which is consistent with the fact that your code does not report an error.

You should check the return value of SetParent() and MoveWindow(), and call GetLastError() in case one of them fails.

Regards,
Juergen

Re: embedding the matrix into the application form

Posted: 30 Oct 2011, 02:50
by Nourah
thank you so much for your informative reply,

i add the loop, it works only with P3Speller_CopySpelling.prm because the main window is the matrix but it did not work with menus because it has write panel as main window

Code: Select all

  p.WaitForInputIdle();

                // Get the main handle
              while (p.MainWindowHandle == IntPtr.Zero)
                {
                    //process = Process.GetProcessById(process.Id);
                    p.Refresh();
                }

                appWin = p.MainWindowHandle;
thank you again

regards,

Re: embedding the matrix into the application form

Posted: 01 Nov 2011, 11:56
by mellinger
Hi,

the process' MainWindowHandle property just contains whatever top-level is found first for the process in question. In order to find the right window, you might use the EnumWindows() API function, and use the window's title to filter out the right handle.

Regards,
Juergen