WPF问题中的主机Win32窗口 [英] Host Win32 Window in WPF Problem

查看:61
本文介绍了WPF问题中的主机Win32窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮我吗?我正在尝试托管Win32窗口(黑色背景),但是当我编译时,除了带有默认颜色的空窗口之外,我什么都没得到.
没有任何编译错误.

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));
		// 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
		{

		}
	};
}


这是我的C#代码


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
{
    /// <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;

        }
    }
}


感谢

推荐答案

这是有关此类事情的博客文章:

http://zamjad.wordpress.com/2009/09/01 /call-unmanaged-dialog-box-from-wpf/ [
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.


这篇关于WPF问题中的主机Win32窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆