MFC中调用WPF教程

原文地址:http://blog.csdn.net/muzizongheng/article/details/5565170

1.新建一个项目MFCHostWpf, 建立2个工程, 一个为vc对话框的工程MFCDlgDemo,另一个为C#普通应用程序的工程WPFDemo.如图所示:

1278723_1273161107cp9e[1]

2.分别运行2个工程后, 程序截图如下:

1278723_1273161107MdJ7[1]

1278723_1273161107VLvW[1]

3.修改WPF工程以便MFC工程调用, 具体如下:

1.删除WPF工程中的 App.xaml和App.xaml.cs两个源文件.

2.双击WPF工程的Properties(属性), 选择Application(应用程序)选项卡, 将Output type(输出类型)下拉框选为Class Library(类库). 保存后关闭. 如图所示:

1278723_1273161108R27Y[1]

4.接下来修改MFC工程以便调用WPF组件, 具体如下:

1.右击MFC工程, 选择弹出菜单的Properties(属性), 在Configuration Properties/General/Common Language Runtime support中选择Common Language Runtime support(/clr), 保存关闭后按F7编译. 如图所示:

1278723_12731611095S2j[1]

2.重新右击MFC工程, 选择"工程属性", 在Common Properties中, 点击"Add New Reference", 在".net"选项卡下添加如下引用PresentationCore, PresentationFramework, System, System.Core, Systems.Data, Systems.Data.DataSetExtensions, Systems.Xml, System.Xml.Linq, WindowsBase. (p.s. 具体引用一定要和WPF工程中的引用一致),保存后退出, 如图所示:

1278723_1273161112au2U[1]

3.重新选择"工程属性", 在Common Properties中, 点击"Add New Reference", 在"Project"选项卡下选择WPFDemo工程, 选择"Ok"后保存退出, 如图所示:

1278723_12731611134Nim[1]

4.建立一个CLI类CHostWPFWnd, 代码如下:

//HostWPFWnd.h #pragma once using namespace System; 
using namespace System::Windows;
using namespace System::Windows::Interop; 
using namespace System::Runtime; 
usingnamespace WPFDemo; 
public ref class CHostWPFWnd { 
public
CHostWPFWnd(void){};
 ~CHostWPFWnd(void){}; 
protected:
 !CHostWPFWnd(){}; 
public
static Window1^ hostedWnd;
static HWND hWnd; 
};
 HWND GetHwnd(HWND hwnd = NULL); //HostWPFWnd.cpp
 #include "StdAfx.h
#include "HostWPFWnd.h
HWND GetHwnd(HWND hwnd) { 
CHostWPFWnd::hostedWnd = gcnew Window1(); 
WindowInteropHelper^ wih = gcnew WindowInteropHelper(CHostWPFWnd::hostedWnd); 
wih->Owner = IntPtr(hwnd);
 CHostWPFWnd::hWnd = (HWND) wih->Handle.ToPointer(); 
return CHostWPFWnd::hWnd; 
}

5.在MFC工程的App文件CMFCHostWpfApp中添加CLI类的引用#include "HostWPFWnd.h", 在App的InitInstance函数里, 修改如下代码:

CMFCHostWpfDlg dlgm_pMainWnd = &dlg
INT_PTR nResponse dlg.DoModal(); 
if (nResponse== IDOK) { 
// TODO: Place code here to handle when the dialog is // dismissed with OK 
}else if (nResponse == IDCANCEL) { 
// TODO: Place code here to handle when the dialog is // dismissed with Cancel 
}

为:

::GetHwnd(); 
if (CHostWPFWnd::hostedWnd) { 
CHostWPFWnd::hostedWnd->ShowDialog(); 
}

6.通过以上5步, 我们已经成功在MFC工程调用WPF, 按F7编译后, F5运行, 效果如下:

1278723_1273161113Zpd5[1]

ok, 相信细心的哥们已经发现这个运行出来的Dlg的程序图标已经换为咱们熟悉的MFC默认icon. O(∩_∩)O~. (p.s. 注意启动的是MFC工程, 应将MFCDemo设为首选项, 具体是右击MFCDemo, 选择Set as StartUp Project).

7.接下来, 我们在WPF工程中定义一个实现INotifyPropertyChanged 接口的类TestModel, 里面有个int字段TestValue, 添加一个Button和一个TextBox, 并添加一个Click事件, 具体代码如下:

//cs

using System
using System.Collections.Generic;
 using System.Linq
using System.Text;
using System.Windowsusing System.Windows.Controlsusing System.Windows.DatausingSystem.Windows.Documentsusing System.Windows.Input
using System.Windows.Media
usingSystem.Windows.Media.Imaging
using System.Windows.Navigation
usingSystem.Windows.Shapes
using System.ComponentModel
namespace WPFDemo /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 :Window 
public class TestModel INotifyPropertyChanged 
public TestModel() { }
private int _testValue = 0; 
public int TestValue get return _testValue; } set {_testValue valueOnPropertyChanged("TestValue"); } } // Declare the event public event PropertyChangedEventHandler PropertyChanged// Create the OnPropertyChanged method to raise the event protected void OnPropertyChanged(string name) {PropertyChangedEventHandler handler PropertyChangedif (handler != null) {handler(thisnew PropertyChangedEventArgs(name)); } } } 
public Window1() {InitializeComponent(); } 
private TestModel test
public TestModel Test get returntest; } set test value; } } 
public delegate void ButtonClickHandler(); 
public eventButtonClickHandler ClickEvent
private void _btnTest_Click(object sender,RoutedEventArgs e) { // ClickEvent(); } } }
 
  
  
 

//xaml

<Window x:Class="WPFDemo.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300"Width="300"> <Grid> <TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0"Name="_txtValue" VerticalAlignment="Top"
Width="120" /> <Button Height="23" Margin="136,10,67,0" Name="_btnTest"VerticalAlignment="Top" Click="_btnTest_Click">Test
</Button> </Grid> </Window>

 

8.这一步我们把_txtValue的Text属性绑定到我们上面定义的TestValue字段, 把_txtValue设为ReadOnly, 并修正下Dlg显示出来的位置, 修改后的xaml代码为:

<TextBox Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="_txtValue"VerticalAlignment="Top" Width="120"
Text="{Binding Test.TestValueElementName=window, Mode=OneWay}" IsReadOnly="True" />

运行效果如图:

9.接下来我们自定义一个event, 在Button的Click事件中触发此事件, 具体代码如下:

public delegate void ButtonClickHandler(); public event ButtonClickHandler ClickEvent;private void _btnTest_Click(object senderRoutedEventArgs e) { // ClickEvent(); }

10.然后我们在MFC通过自定义一个Add方法, 并在方法中通过CLI修改WPF中的TextValue字段, 然后通过CLI把此Add方法加到自定义event中. 修改后代码如下:

//HostWPFWnd.h 
#pragma once using namespace System;
 using namespace System::Windows;
using namespace System::Windows::Interop
using namespace System::Runtime
using namespace WPFDemo
public ref class CHostWPFWnd 
publicCHostWPFWnd(void){}; ~CHostWPFWnd(void){}; 
protected: !CHostWPFWnd(){}; publicstatic Window1hostedWnd;static HWND hWnd; }; HWND GetHwnd(HWND hwnd NULL); void Add(); //Increase TestValue;
//HostWPFWnd.cpp 
#include "StdAfx.h" 
#include "HostWPFWnd.h" 
HWND GetHwnd(HWND hwnd) {CHostWPFWnd::hostedWnd gcnew Window1(); CHostWPFWnd::hostedWnd->ClickEvent += gcnewWindow1::ButtonClickHandler(Add); WindowInteropHelperwih gcnewWindowInteropHelper(CHostWPFWnd::hostedWnd); wih->Owner IntPtr(hwnd);CHostWPFWnd::hWnd = (HWNDwih->Handle.ToPointer(); return CHostWPFWnd::hWnd; }
void Add() { CHostWPFWnd::hostedWnd->Test->TestValue++; }

11.F7编译后, F5运行, 结果如下:

1278723_12731611135MH4[1]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值