使用C++来开发WPF,主要是如何在MFC(Win32)的窗口中Host WPF的Page。下面我就做个详细的介绍.

一、创建工程, 由于MFC的Wizard会生成很多用不到的代码,所以我准备从一个空的工程开始创建一个MFC的工程。

a)         打开VS2005,菜单File->New->Projects…, 左面选择Visual C++->Win32,右面选择Win32 Console Application,给工程起个名字CPlusPlus_WPF, Ok进入下一步。见Figure 1使用C++来开发WPF

Figure 1 Create Project 1

b)        工程基本配置,在Application Setting中选择Console Application和MFC.。Finish进入下一步。见Figure 2
使用C++来开发WPF

Figure 2 Application Setting

c)        修改工程,使工程变成MFC Windows程序。

                                       i.              删除CPlusPlus_WPF.cpp和CPlusPlus_WPF.h文件

使用C++来开发WPF
Figure 3 Remove no used files

                                     ii.              添加CWinApp派生类, 在工程上点击鼠标右键,Add=>Class…

使用C++来开发WPF
Figure 4 Add New Class Menu

                     在弹出的对话框中,左边选择MFC,右面选择MFC Class,点击Add进入下一步

使用C++来开发WPF
Figure 5 Create New Class Dialog

              在弹出的对话框中输入类名: CCPlusPlus_WPFApp, 基类选择CWinApp

使用C++来开发WPF
Figure 6 Add CWinApp Derived Class

 

                                       i.              用同上的方法添加CWnd派生类,Class name为CCPlusPlus_WPFMainWnd, Base class为CWnd。

                                     ii.              修改工程属性。将属性中的System->SubSystem从Concole改成Windows,见Figure 7

  使用C++来开发WPF
 

Figure 7 Change property

到这一步,一个基本的MFC程序所需要的两个类CWinApp和CWnd派生类就添加完了。我们的程序可以顺利编译通过,但是还不能运行,请继续看下一步

d). 为CCPlusPlus_WPFMainWnd添加代码;
    l         添加创建窗口函数,函数如下:

使用C++来开发WPFBOOL CCPlusPlus_WPFMainWnd::CreateMainWnd(const CRect &rect, DWORD dwStyle, DWORD dwStyleEx)
}

l         继承CWnd类的PostNcDestroy函数,这个函数是CWnd类中的虚函数,是在窗口退出后,最后一个被调用的函数,我们在这个函数里还删除自己。

使用C++来开发WPFvoid CCPlusPlus_WPFMainWnd::PostNcDestroy()
}

             

e)       为CCPlusPlus_WPFApp添加代码

l         把构造函数改由protect改为public

l         定义theApp, 即CCPlusPlus_WPFAp theApp

l         实现InitInstance()

 

使用C++来开发WPFBOOL CCPlusPlus_WPFApp::InitInstance()
}

        编译运行,可以看到窗口了,到这步位置,一个基本的MFC程序就已经建立起来了,大家可以对窗口添加想要的风格。这里就不做详细说明了。

      二、              进一步修改工程,使其支持WPF

        1.         修改工程属性,在工程属性的General的Common Language Runtime Support中选择/Clr, 这个选项可以让这个工程支持Common Language Runtime. 这样我们的工程里就可以写C++/CLI的代码,用来操作WPF了。使用C++来开发WPF

Figure 8 Support clr
        2.         添加References,在工程属性中的左面的树上选择,Common Properties->References(C++程序员好像机会很少用到)。使用C++来开发WPF

Figure 9 Add References

          点击Add New Reference按钮,弹出如下对话框                            

 使用C++来开发WPF

 

           顺次添加如下.NET dlls. 到现在位置,这个工程就已经完全支持WPF了
                使用C++来开发WPF
 
        3.         添加显示WPF的部分
            a)         创建一个XAML文件,我已经上传一个,Clock.XAML,这段XAML在Windows SDK中也可以找到。
            b)        动态读取XAML文件
            c)        把WPF的Page Host到MFC的窗口中。完整代码如下     

using namespace System;
using namespace System::IO;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Markup;
using namespace System::Windows::Interop;

使用C++来开发WPFvoid CCPlusPlus_WPFMainWnd::CreateWPFWnd()

           d)        在CCPlusPlus_WPFMainWnd::OnCreate中调用上面的函数。
             e)         编译运行,一切Ok了。

当然,这只是基本的步骤,在这个基础上就可以利用C++和WPF来开发一个完整的软件了

相关文章:

  • 2022-12-23
  • 2021-09-25
  • 2021-10-18
  • 2021-08-17
  • 2021-07-04
  • 2021-10-07
  • 2022-01-08
猜你喜欢
  • 2022-12-23
  • 2022-01-11
  • 2022-12-23
  • 2021-06-30
  • 2021-05-31
  • 2021-07-17
相关资源
相似解决方案