呵呵,? 這幾天看到社區里大家對如何在vc中調用WPF興致很高, 現在我就帶領大家按部就班來實現它.廢話不說了, 開始.
WPF是微軟主推的新一代桌面程序開發技術, 它極大加快了程序界面開發,也增強了界面的用戶體驗,至于詳細的介紹大家可以google,? 本文主要還是介紹如何在vc中處理wpf數據和事件.開發工具嘛vs2008+sp1+[Blend].
1.新建一個項目MFCHostWpf, 建立2個工程, 一個為vc對話框的工程MFCDlgDemo,另一個為C#普通應用程序的工程WPFDemo.如圖所示:
[![1278723_1273161107cp9e[1]](https://box.kancloud.cn/2016-02-02_56b0015936aec.gif "1278723_1273161107cp9e[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161107cp9e.jpg)
2.分別運行2個工程后, 程序截圖如下:
[![1278723_1273161107MdJ7[1]](https://box.kancloud.cn/2016-02-02_56b0015948717.gif "1278723_1273161107MdJ7[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161107MdJ7.jpg)
[![1278723_1273161107VLvW[1]](https://box.kancloud.cn/2016-02-02_56b0015956534.gif "1278723_1273161107VLvW[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161107VLvW.jpg)
3.修改WPF工程以便MFC工程調用, 具體如下:
> 1.刪除WPF工程中的 App.xaml和App.xaml.cs兩個源文件.
> 2.雙擊WPF工程的Properties(屬性), 選擇Application(應用程序)選項卡, 將Output type(輸出類型)下拉框選為Class Library(類庫). 保存后關閉. 如圖所示:
> [![1278723_1273161108R27Y[1]](https://box.kancloud.cn/2016-02-02_56b0015964625.gif "1278723_1273161108R27Y[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161108R27Y.jpg)
4.接下來修改MFC工程以便調用WPF組件, 具體如下:
> 1.右擊MFC工程, 選擇彈出菜單的Properties(屬性), 在Configuration Properties/General/Common Language Runtime support中選擇Common Language Runtime support(/clr), 保存關閉后按F7編譯. 如圖所示:
> [![1278723_12731611095S2j[1]](https://box.kancloud.cn/2016-02-02_56b0015974720.gif "1278723_12731611095S2j[1]")](http://hi.csdn.net/attachment/201005/6/1278723_12731611095S2j.jpg)
> 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]](https://box.kancloud.cn/2016-02-02_56b0015987697.gif "1278723_1273161112au2U[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161112au2U.jpg)
> 3.重新選擇"工程屬性", 在Common Properties中, 點擊"Add New Reference", 在"Project"選項卡下選擇WPFDemo工程, 選擇"Ok"后保存退出, 如圖所示:
> [![1278723_12731611134Nim[1]](https://box.kancloud.cn/2016-02-02_56b0015996418.gif "1278723_12731611134Nim[1]")](http://hi.csdn.net/attachment/201005/6/1278723_12731611134Nim.jpg)
> 4.建立一個CLI類CHostWPFWnd, 代碼如下:
>
~~~
//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
{
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
}
~~~
[](http://11011.net/software/vspaste)> 為:
~~~
::GetHwnd();
if (CHostWPFWnd::hostedWnd)
{
CHostWPFWnd::hostedWnd->ShowDialog();
}
~~~
[](http://11011.net/software/vspaste)> 6.通過以上5步, 我們已經成功在MFC工程調用WPF, 按F7編譯后, F5運行, 效果如下:
> [![1278723_1273161113Zpd5[1]](https://box.kancloud.cn/2016-02-02_56b00159a5f48.gif "1278723_1273161113Zpd5[1]")](http://hi.csdn.net/attachment/201005/6/1278723_1273161113Zpd5.jpg)
> 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 event
public event PropertyChangedEventHandler PropertyChanged;
// Create the OnPropertyChanged method to raise the event
protected 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();
}
}
}
~~~
[](http://11011.net/software/vspaste)
~~~
~~~
[](http://11011.net/software/vspaste)[](http://11011.net/software/vspaste)> //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>
~~~
[](http://11011.net/software/vspaste)> ?
> 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" />
~~~
[](http://11011.net/software/vspaste)[](http://11011.net/software/vspaste)[](http://11011.net/software/vspaste)> 運行效果如圖:
> 9.接下來我們自定義一個event, 在Button的Click事件中觸發此事件, 具體代碼如下:
~~~
public delegate void ButtonClickHandler();
public event ButtonClickHandler ClickEvent;
private void _btnTest_Click(object sender, RoutedEventArgs e)
{
//
ClickEvent();
}
~~~
[](http://11011.net/software/vspaste)> 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
{
public:
CHostWPFWnd(void){};
~CHostWPFWnd(void){};
protected:
!CHostWPFWnd(){};
public:
static Window1^ hostedWnd;
static HWND hWnd;
};
HWND GetHwnd(HWND hwnd = NULL);
void Add(); //Increase TestValue;
~~~
[](http://11011.net/software/vspaste)
~~~
//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++;
}
~~~
[](http://11011.net/software/vspaste)[](http://11011.net/software/vspaste)> 11.F7編譯后, F5運行, 結果如下:
> [![1278723_12731611135MH4[1]](https://box.kancloud.cn/2016-02-02_56b00159b2493.gif "1278723_12731611135MH4[1]")](http://hi.csdn.net/attachment/201005/6/1278723_12731611135MH4.jpg)
ok, 通過本文相信大家都了解了如何在MFC中調用WPF, 有什么問題歡迎大家和我討論.
工程附件:[MFCHostWPF](http://download.csdn.net/source/2323576)
- 前言
- win32與WPF的混合編程
- WPF: 一個可以用StoryBoard動態改變Grid行寬/列高的類
- MFC中調用WPF教程
- Expression Blend操作: 使用behavior來控制Storyboard
- WPF DatePicker 的textbox的焦點
- WPF 使用MultiBinding ,TwoWay ,ValidationRule ,需要注意的事項
- WPF TreeView 后臺C#選中指定的Item, 需要遍歷
- WPF GridViewColumn Sort DataTemplate
- DataGridColum的bug
- WPF Get Multibinding Expression, Update Source,
- WPF 后臺觸發 Validate UI‘s Element
- WPF ValidationRule 觸發ErrorTemplate 的注意事項
- WPF DelegateCommand CanExecute
- WPF TextBox PreviewTextInput handle IME (chinese)
- No overload for &#39;OnStartup&#39; matches delegate &#39;System.Windows.StartupEventHandler&#39;
- WPF error: does not contain a static &#39;Main&#39; method suitable for an entry point
- WPF GridView中的CellTemplate失效的原因
- DataGrid 顯示選中的item
- 如何得到WPF中控件綁定的EventTrigger
- 選中DataGrid的Cell而不是row
- ContextMenu的自定義
- 輸入框只能輸入英文
- TextBox的OnTextboxChanged事件里對Text重新賦值帶中文, 導致崩潰
- DataGrid當列寬超出當前寬度時,沒有數據也恒有滾動條
- wpf如何獲取control template里的元素
- Set connectionId threw an exception.
- WPF中Visible設為Collapse時,VisualTreeHelper.GetChildrenCount為0
- XAML 編碼規范 (思考)
- 如何為現有控件的DependencyProperty添加Value Changed事件?
- TreeView滾動TreeViewItem
- 為BindingList添加Sort
- WPF Background的設置有坑
- 自定義Panel中添加依賴屬性需要注意的問題
- TextBlock截斷字符顯示為....
- DataGrid 支持字符截斷顯示
- TreeView控件實踐
- WPF如何更改系統控件的默認高亮顏色 (Highlight brush)
- ViewModel中C# Property自動添加OnPropertyChanged處理的小工具, 以及相應Python知識點
- WPF中Xaml編譯正常而Designer Time時出錯的解決辦法
- 關于Snoop的用法
- wpf中為DataGrid添加checkbox支持多選全選
- WPF中DataGrid控件的過濾(Filter)性能分析及優化
- wpf控件提示Value ‘’ can not convert
- DropShadowEffect導致下拉框控件抖動
- 再論WPF中的UseLayoutRounding和SnapsToDevicePixels
- WPF案例:如何設計歷史記錄查看UI
- WPF案例:如何設計搜索框(自定義控件的原則和方法)
- WPF基本概念入門
- WPF開發中Designer和碼農之間的合作
- 聊聊WPF中的Dispatcher
- 聊聊WPF中字體的設置
- Bug:DataGridCell的顯示不完整
- WPF中ToolTip的自定義
- WPF中ItemsControl綁定到Google ProtocolBuffer的結構體時的性能問題
- TreeView的性能問題
- Xaml中string(字符串)常量的定義以及空格的處理
- 依賴屬性
- WPF中的CheckBox的_ (underscore / 下劃線)丟失
- WPF錯誤:必須使“Property”具有非 null 值。
- WPF中ItemsControl應用虛擬化時找到子元素的方法
- WPF毫秒級桌面時鐘的實現-C#中Hook(鉤子)的應用
- KB2464222導致IsNonIdempotentProperty方法找不見
- WPF中PreviewMouseDownEvent的系統處理:TabItem的PreviewMouseDown 事件彈框后不切換的問題調查
- WPF文字渲染相關的問題及解決
- wpf中的默認右鍵菜單中的復制、粘貼、剪貼等沒有本地化的解決方案
- WPF內部DeliverEvent讀鎖和PrivateAddListener寫鎖導致死鎖
- Windbg調試WPF的依賴屬性
- WPF 后臺Render線程崩潰, Exception from HRESULT: 0x88980406
- WPF中DependencyObject與DependencyProperty的源碼簡單剖析
- 禁用WPF中DataGrid默認的鼠標左鍵拖動多選行的效果
- wpf工程中在Xaml文件下添加多個cs文件
- ScrollViewer滾動到底來觸發加載數據的Behavior