Can anyone help me out ? I am trying to host a Win32 window (black background) but when I compile I get nothing except an empty window with the default colouring.
There is no compilation error whatsoever.
#pragma once
#include "Stdafx.h"
#include <windows.h>
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Interop;
using namespace System::Runtime::InteropServices;
namespace UnmanagedCode
{
LRESULT WINAPI WndProc (HWND hwnd, UINT msg , WPARAM wParam , LPARAM lParam)
{
switch(msg)
{
case WM_IME_SETCONTEXT:
if(LOWORD(wParam)>0)
SetFocus(hwnd);
return 0;
default:
return DefWindowProc(hwnd,msg,wParam,lParam);
}
}
static HRESULT InitializeW()
{
WNDCLASS wc ;
wc.style = CS_VREDRAW | CS_HREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = GetModuleHandle(NULL);
wc.lpszMenuName = 0;
wc.lpszClassName = L"D3DWindow";
wc.hbrBackground = CreateSolidBrush(RGB(0,0,0));
RegisterClass(&wc);
return 0;
}
}
namespace WinHost
{
public ref class MyHwndHost : HwndHost
{
protected:
virtual HandleRef BuildWindowCore(HandleRef hwndParent)override
{
HWND hwnd = CreateWindow(L"D3DWindow",
NULL,
WS_CHILD|WS_VISIBLE,
CW_USEDEFAULT , 0,
800,600, (HWND)hwndParent.Handle.ToInt32(),
NULL,
GetModuleHandle(NULL),
NULL);
if(hwnd == NULL)
throw gcnew ApplicationException("Fail to create Window");
UnmanagedCode::InitializeW();
return HandleRef(this, IntPtr(hwnd));
}
virtual void DestroyWindowCore(HandleRef hwnd) override
{
}
};
}
and here's my C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Interop;
using System.Runtime.InteropServices;
using WinHost;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Windows_loaded(object sender, RoutedEventArgs e)
{
HwndHost host = new MyHwndHost();
test.Child = host;
}
}
}
Thanks