Gamestudio Links
Zorro Links
Newest Posts
AlpacaZorroPlugin v1.3.0 Released
by kzhao. 05/22/24 13:41
Free Live Data for Zorro with Paper Trading?
by AbrahamR. 05/18/24 13:28
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
0 registered members (), 984 guests, and 5 spiders.
Key: Admin, Global Mod, Mod
Newest Members
LucasJoshua, Baklazhan, Hanky27, firatv, wandaluciaia
19053 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Hosting Acknex/LiteC in a Windows Forms Control #114261
02/28/07 14:34
02/28/07 14:34
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline OP
Serious User
MichaelGale  Offline OP
Serious User
M

Joined: Aug 2005
Posts: 1,230
A while ago I showed you how to access Acknex with C# and C++.

Today I will show you, how you can host your Acknex or Lite-C Application inside a Windows Forms Application.

First at all we need some function from the user32.dll, here are three of them:

Code:
  
internal static class Win32
{
[DllImport("user32.dll", SetLastError = true)]
public static extern long SetParent(IntPtr hWndChild, IntPtr hWndNewParent);

[DllImport("user32.dll", EntryPoint = "SetWindowLongA", SetLastError = true)]
public static extern long SetWindowLong(IntPtr hwnd, int nIndex, long dwNewLong);

[DllImport("user32.dll", SetLastError = true)]
public static extern bool MoveWindow(IntPtr hwnd, int x, int y, int cx, int cy, bool repaint);

public const int GWL_STYLE = (-16);
public const int WS_VISIBLE = 0x10000000;
}



SetParent is required to change the MainWindowHandle of Acknex.
SetWindowLong is required to change the Style of the window.
and MoveWindow is required to move and resize the window.

The next step is to create a new WinForms Control, though a Panel on a Form would be enough as well.

Now we have to start the process and to change its window handle.

First at all we create a new process and start it. Then we wait until our new process enters an idle state and copy the window handle of the process to a IntPtr handle in our class.

The last step is to change the window handle, we call our SetParent function from our user32.dll interface and change it. MoveWindow resizes the window and moves it to a 0,0 position so it is fully visible.

Code:
 
if (!mCreated)
{
mCreated = true;

mAppWnd = IntPtr.Zero;

Process p = null;

try
{
p = Process.Start("acknex.exe", "work/mandelbrot_pure.c");

p.WaitForInputIdle();

mAppWnd = p.MainWindowHandle;

Win32.SetParent(mAppWnd, this.Handle);

Win32.SetWindowLong(mAppWnd, deWin32.GWL_STYLE, deWin32.WS_VISIBLE);

Win32.MoveWindow(mAppWnd, 0, 0, this.Width, this.Height, true);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message, "Our Application", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}



And here a screenshot:

Screenshot

This works with basically every other application, too.

Michael

edit: I recommend you to add Thread.Sleep(TimeSpan.FromSeconds(1)) after p.WaitForInputIdle() to avoid an engine crash.

Last edited by MichaelFriedrich; 02/28/07 15:04.
Re: Hosting Acknex/LiteC in a Windows Forms Contro [Re: MichaelGale] #114262
02/28/07 17:12
02/28/07 17:12
Joined: Aug 2003
Posts: 7,439
Red Dwarf
Michael_Schwarz Offline
Senior Expert
Michael_Schwarz  Offline
Senior Expert

Joined: Aug 2003
Posts: 7,439
Red Dwarf
Looks nice


"Sometimes JCL reminds me of Notch, but more competent" ~ Kiyaku
Re: Hosting Acknex/LiteC in a Windows Forms Contro [Re: Michael_Schwarz] #114263
03/01/07 16:38
03/01/07 16:38
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline OP
Serious User
MichaelGale  Offline OP
Serious User
M

Joined: Aug 2005
Posts: 1,230
Thanks,

I tested some of the other examples, too. Here are the results:

OpenGL example:


Physics example

Sorry for the bad quality of the physics example, but I forgot to change the resolution.

Michael


Your friendly mod is at your service.
Re: Hosting Acknex/LiteC in a Windows Forms Contro [Re: MichaelGale] #114264
03/01/07 17:34
03/01/07 17:34
Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
Rhuarc Offline
Expert
Rhuarc  Offline
Expert

Joined: Mar 2001
Posts: 3,298
Beverly, Massachusetts
How about native?

JCL recently pointed me towards this, and we discussed its implementation a little.
http://manual.conitec.net/hWndTarget.htm

It's only available in A6 betas and the current liteC though. Hopefully it comes through to release soon (pokes JCL).

C++
Code:
ev = engine_open(cmdline);
*ev->d3d_lockable = _VAR(1);
engine_frame();
ev->hWndTarget = hWnd; // hWnd = target window



C# (some utility functions not included here, see my other post in higher languages for C# wrapper snippets)
Code:
IntPtr evPtr = engine_open(cmdline, 0);
EngineVars ev = Marshal.PtrToStructure(evPtr, typeof(EngineVars));
ev.d3d_lockable.SetValue(1);
engine_frame();
ev.hWnd.SetValue(hWnd); //(pass IntPtr Handle of the window/control as hWnd)



-Rhuarc


I no longer post on these forums, keep in touch with me via:
Linkedin.com
My MSDN blog
Re: Hosting Acknex/LiteC in a Windows Forms Contro [Re: Rhuarc] #114265
03/01/07 20:04
03/01/07 20:04
Joined: Aug 2005
Posts: 1,230
M
MichaelGale Offline OP
Serious User
MichaelGale  Offline OP
Serious User
M

Joined: Aug 2005
Posts: 1,230
Yes, that's the better way indeed.


Your friendly mod is at your service.

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1