您需要创建一个包装类来公开您的游戏的某些功能。
例如。这个函数在我的C++游戏引擎编辑器中。
extern "C" _declspec(dllexport) void SetPlayerPos(int id, const float x, const float y, const float z);
然后在C#WPF应用程序,你可以创建一个静态类,允许你使用这些功能
[DllImport(editorDllName, CallingConvention = CallingConvention.Cdecl)]
public static extern void SetPlayerPos(int id, float x, float y, float z);
你必须具备的功能,通过该dll你的游戏引擎的基本功能。 之类的东西
EditorMain RenderFrame /更新 DXShutdown
,那么你可以在你的WPF应用程序构造函数中调用editormain
System.IntPtr hInstance = System.Runtime.InteropServices.Marshal.GetHINSTANCE(this.GetType().Module);
IntPtr hwnd = this.DisplayPanel.Handle;
NativeMethods.EditorMain(hInstance, IntPtr.Zero, hwnd, 1, this.DisplayPanel.Width, this.DisplayPanel.Height);
你需要创建一个消息过滤器类的初始化构造函数以及
m_messageFilter = new MessageHandler(this.Handle, this.DisplayPanel.Handle, this);
这里是你的消息过滤呃类可以看看
public class MessageHandler : IMessageFilter
{
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
IntPtr m_formHandle;
IntPtr m_displayPanelHandle;
EngineDisplayForm m_parent;
public MessageHandler(IntPtr formHandle, IntPtr displayPanelHandle, EngineDisplayForm parent)
{
m_formHandle = formHandle;
m_displayPanelHandle = displayPanelHandle;
m_parent = parent;
}
public bool PreFilterMessage(ref Message m)
{
if (m.HWnd == m_displayPanelHandle || m.HWnd == m_formHandle)
{
switch (m.Msg)
{
case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
{
NativeMethods.WndProc(m_displayPanelHandle, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
if (m.Msg == WM_LBUTTONUP)
{
m_parent.SelectActor();
}
return true;
}
}
}
return false;
}
public void Application_Idle(object sender, EventArgs e)
{
try
{
// Render the scene
NativeMethods.RenderFrame();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
做赢形式和WPF互操作这里看看 http://msdn.microsoft.com/en-us/library/ms742474.aspx
我应该也可以使用SlimDx? – Stomp