🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
**本文主要讲的是win32程序中如何应用WPF界面** 1.WPF概述    WPF缩写为Windows Presentation Foundation的缩写 ,其原来代号为“Avalon”,因“我佩服”拼音首字母组合一样,国内有人调侃地称之为“我佩服”。WPF是微软新一代图形系统,运行在.NET Framework 3.0架构下,为用户界面、2D/3D 图形、文档和媒体提供了统一的描述和操作方法。基于DirectX 9/10技术的WPF不仅带来了前所未有的3D界面,而且其图形向量渲染引擎也大大改进了传统的2D界面,比如Vista中的半透明效果的窗体等都得益于WPF。 程序员在WPF的帮助下,要开发出媲美Mac程序的酷炫界面已不再是遥不可及的奢望。 WPF相对于Windows客户端的开发来说,向前跨出了巨大的一步,它提供了超丰富的.NET UI 框架,集成了矢量图形,丰富的流动文字支持flow text support,3D视觉效果和强大无比的控件模型框架。 2.MVVM概述 MVVM(Model-View-ViewModel) 是MVC模式演变而来的, 在MVP模式的基础上实现了与WPF完美的结合. Model是数据层; View也就是呈现层或者UI层; ViewModel是View的抽象,实现了View的接口和属性、命令以及Model的处理。如,下图为MVVM模式架构图:      ![MVVM架构](image/d41d8cd98f00b204e9800998ecf8427e.jpg) 3.Win32与WPF的混合编程 1> win32工程的工程属性添加/CLR支持 2> win32工程的Framework and References 中添加一些.net必须的引用, 如PresentationCore, PresentationFramework, System, WindowBase;  (ps: 其.net引用集应该与要调用的WPF的引用集相同) 3> 建立WPF应用程序,  将工程的工程属性的Application/Output type设为 Class Library, 删除App.xaml和App.xaml.cs文件, 并在Win32程序的Framework and References / Projects添加这个WPF程序集. 4> 通过clr语法关联c++ 和 C#. 如:         HWND GetHwnd(HWND parent, int x, int y, int width, int height)         {         HwndSource^ source = gcnew HwndSource(                   0, // class style                   WS_VISIBLE | WS_CHILD, // style                   0, // exstyle                   x, y, width, height,                   "WPF", // NAME                   IntPtr(parent)        // parent window                   );         WpfPageHost::hostedPage = gcnew MainView();         WpfPageHost::hostedViewModel = gcnew MainViewModel();          source->RootVisual = WpfPageHost::hostedPage;         return (HWND) source->Handle.ToPointer();         } 5> 利用Microsoft Expression Blend设计WPF界面 参考资料: 1.WPF[http://zh.wikipedia.org/zh-cn/Windows_Presentation_Foundation#_note-0](http://zh.wikipedia.org/zh-cn/Windows_Presentation_Foundation#_note-0) 2.Expression Blend中文论坛[http://www.expressioncn.com/](http://www.expressioncn.com/) 3.Expression Blend官网[http://www.microsoft.com/expression/](http://www.microsoft.com/expression/) 4.WPF Apps With The Model-View-ViewModel Design Pattern[http://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx](http://msdn.microsoft.com/zh-cn/magazine/dd419663.aspx) 5.Walkthrough: Hosting Windows Presentation Foundation Content in a Win32 Application[http://msdn.microsoft.com/en-us/library/ms744829.aspx](http://msdn.microsoft.com/en-us/library/ms744829.aspx)