Click here to Skip to main content
15,919,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C++
#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));
		// write function to detect NULL wc
		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,      // use function to obtain
									(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
C#
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
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Windows_loaded(object sender, RoutedEventArgs e)
        {
            HwndHost host = new MyHwndHost();
            test.Child = host;

        }
    }
}

Thanks
Posted
Updated 8-Sep-10 7:45am
v4
Comments
HimanshuJoshi 8-Sep-10 13:34pm    
Added PRE tags

1 solution

Here is an blog post about this type of thing:

http://zamjad.wordpress.com/2009/09/01/call-unmanaged-dialog-box-from-wpf/[^]

If you were looking for putting a WinForm inside of a WPF, it would have been easier just using the WindowsFormsHost element.
 
Share this answer
 
Comments
jasyong 9-Sep-10 21:18pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900