WPF是微软主推的新一代桌面程序开发技术, 它极大加快了程序界面开发,也增强了界面的用户体验, 本文主要还是介绍如何在vc中处理wpf数据和事件?

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

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

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

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

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

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

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

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工程中的引用一致),保存后退出, 如图所示:

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

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

//HostWPFWnd.h
#pragma onceusing namespace System;
using namespace System::Windows;
using namespace System::Windows::Interop;
using namespace System::Runtime;
using namespace 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 dlg;m_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运行, 效果如下:

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.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.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 = value;OnPropertyChanged("TestValue");}}// Declare the eventpublic event PropertyChangedEventHandler PropertyChanged;// Create the OnPropertyChanged method to raise the eventprotected void OnPropertyChanged(string name){PropertyChangedEventHandler handler = PropertyChanged;if (handler != null){handler(this, new PropertyChangedEventArgs(name));}}}public Window1(){InitializeComponent();}private TestModel test;public TestModel Test{get { return test; }set { test = value; }}public delegate void ButtonClickHandler();public event ButtonClickHandler 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.TestValue, ElementName=window, Mode=OneWay}" IsReadOnly="True" />

运行效果如图:

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

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

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

//HostWPFWnd.h
#pragma onceusing namespace System;
using namespace System::Windows;
using namespace System::Windows::Interop;
using namespace System::Runtime;
using namespace WPFDemo;public ref class CHostWPFWnd
{
public:CHostWPFWnd(void){};~CHostWPFWnd(void){};
protected:!CHostWPFWnd(){};public:static Window1^ hostedWnd;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 += gcnew Window1::ButtonClickHandler(Add); WindowInteropHelper^ wih = gcnew WindowInteropHelper(CHostWPFWnd::hostedWnd);wih->Owner = IntPtr(hwnd);CHostWPFWnd::hWnd = (HWND) wih->Handle.ToPointer();return CHostWPFWnd::hWnd;
}
void Add()
{CHostWPFWnd::hostedWnd->Test->TestValue++;
}

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

工程附件:MFCHostWPF

MFC中调用WPF教程相关推荐

  1. 如何在MFC中调用CUDA

    如何在MFC中调用CUDA 有时候,我们需要在比较大的项目中调用CUDA,这就涉及到MFC+CUDA的环境配置问题,以矩阵相乘为例,在MFC中调用CUDA程序.我们参考罗振东iylzd@163.com ...

  2. Halcon初学者知识 【13】如何在MFC中调用Halcon代码

    要知道如何在MFC中调用Halcon代码,需要以下知识点: 在VC++2019的工程中,配置Halcon环境[请看此文] Halcon代码如何导出成C++代码 如何拆解halcon转换成C++的代码, ...

  3. ATL--创建简单的ATL之dll工程,添加类和类的接口并在MFC中调用

    资源打包 开发环境 Windows Server 2012 VS2010 Sp1 番茄助手 创建ATL简单dll工程 1.打开VS2010,新建ATL COM 项目,步骤:"文件" ...

  4. 在C# winform程序中调用WPF写的数学公式编辑器

    由于工作原因,需要在程序中加入数学公式编辑功能,因此在网上找了不少开源数学公式程序.经过比较,最终选择了Math-Editor-master程序(可以在github上搜索此名称). 我的程序(简称主程 ...

  5. 【MFC】MFC中调用系统软键盘的几种方法

    1.直接运行微软系统自带的虚拟键盘程序"osk.exe" 在普通MFC项目中可以调用ShellExecute或者WinExec方法来直接运行微软系统自带的虚拟键盘程序"o ...

  6. VC++/MFC中调用CHM帮助文档的方法--ShellExecute

    (1)用Word编辑好帮助文档,并保存为网页格式,如mhtml格式. (2)用EasyCHM软件生成chm文档.生成方法很简单的,相信你能很快搞定的!当然用其它方法制作CHM文档也可以了. (3)在M ...

  7. 在MFC中调用DLL .

    欲在.EXE 中调用PRO.DLL PRO.DLL中函数如: long MenuExecute( char *, long , HWND , long , char *, long , void * ...

  8. 来总结一下在VC中调用COM组件的方法

    来总结一下在VC中调用COM组件的方法(大家来补充) [问题点数:50分,结帖人_foo] http://bbs.csdn.net/topics/50319093 发表于: 2004-04-17 16 ...

  9. WinForm中使用WPF的控件

    在WinForm中可以使用WPF中的控件,或者由WPF创建的自定义控件: 步骤1:创建WinForm工程: 步骤2:在WinForm工程的解决方案资源管理器中,在刚刚创建的WinForm解决方案中新建 ...

  10. duilib进阶教程 -- 在MFC中使用duilib (1)

    由于入门教程的反响还不错,因此Alberl就以直播的形式来写<进阶教程>啦,本教程的前提: 1.请先阅读<仿迅雷播放器教程> 2.要有一定的duilib基础,如果还没,请先阅读 ...

最新文章

  1. 安卓蓝牙调试软件和微信小程序搜索不到设备
  2. crontab定时任务中文乱码问题
  3. leetcode1005. K 次取反后最大化的数组和
  4. 造轮子是什么意思_程序员为什么热衷于造轮子,升职加薪吗?
  5. 别让Vue3.0的谣言害了你!
  6. 千橡CEO给应聘者的信
  7. 手语识别_如何使用转移学习进行手语识别
  8. CentOS 7 启动与切换图形界面
  9. QT程序启动画面问题
  10. 安装使用ubuntu和opensuse
  11. Mybatis Plus配置以及单表操作
  12. apache 的batik生成svg文件和通过swing界面查看效果
  13. java持久化 seri_Java 的序列化 (Serialization) 教程
  14. DS1302时钟芯片(SPI协议)
  15. python爬虫—爬取taptap游戏的评论信息(通过fiddler抓包)
  16. bubbles html5游戏源码,html5 canvas弹性气泡爆破 | 撒花动画
  17. Linux 下的用户、用户组、文件权限设置
  18. 模板类的特例化(具体化)
  19. Eclipse时区出问题,与北京时间相差八小时
  20. iphone11的计算机在哪,iPhone11怎么连接电脑?iPhone11信任并连接电脑图文教程

热门文章

  1. 微信公众号开发之发送模板消息
  2. ISO26262解析(六)——硬件集成测试
  3. iphone配置实用工具iPhone Configuration Utility
  4. Wpf初学 ---03设计一个优美的注册登录界面(连接数据库)
  5. android获取mp3时间长,android 获取音频时长
  6. 初级web前端面试题
  7. 顺序表和链表的优缺点
  8. java计算机毕业设计基于web旅游网站的设计与实现源码+数据库+系统+lw文档+mybatis+运行部署
  9. echarts 自定义图表custom
  10. 几个实用又好看的纯css 按钮样式