您需要創建一個包裝類來公開您的遊戲的某些功能。
例如。這個函數在我的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