<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                呵呵,? 這幾天看到社區里大家對如何在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)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看