探索Visual C++下創(chuàng)建WPF項目的方法示例
C++/CLI 下創(chuàng)建WPF項目的方法
由于WPF不僅僅支持C#/VB開發(fā),還支持其他語言,比如: C++、F#等開發(fā),于是大白我最近花了點時間摸索了一下,本文主要介紹 C++/CLI 下創(chuàng)建WPF項目的方法。
我使用的開發(fā)環(huán)境是: Win10 x64 + Visual Studio 2019 (16.6.1版本)。
今天我們需要使用 C++/CLI ,算是C++的一個子集吧。
要能正常使用 C++/CLI ,首先需要確保你安裝了 C++/CLI build套件(見下圖),同時還需要確保你安裝好了Visual C++相應(yīng)版本的運行庫。
進入 控制面板 ,找到 Visual Studio 2019,右擊"修改",然后切換到"獨立組件"(Individual components)這個選項卡。

如果沒安裝,勾選后安裝一下即可。
接下來我們可以創(chuàng)建項目了,建議選用模板 CLR Empty Project (.NET Framework) ,解決方案和項目名可以都用 CppWpfDemo 。

這時一個空項目就創(chuàng)建完成了。
此時查看 Project的屬性, Configration Properties -> "C/C++" -> "All Options",輸入 "common"進行搜索,確保選中的是 Common Language Runtime Suppor(/clr) .

接下來我們鼠標(biāo)右擊項目下的文件夾"Resource Files",點"Add" -> "new item",類型選"Component Class",可使用默認的名字 MyComponent 。

此時, MyComponent.cpp 中的代碼如下:
#include "MyComponent.h"
為了正確引用到 WPF 中的各種庫,我們還需要加入 WPF中 3 個核心的 dll,操作方法是:
右鍵點擊項目中的 References ,然后點 Add Reference ,勾選上:
- PresentationCore
- PresentationFramework
- WindowsBase

接下來,進行了一番倒騰,我改成了這個,做成了一個簡單的界面:
此時 MyComponent.cpp 的內(nèi)容如下:
#include "MyComponent.h"
using namespace CppWpfDemo;
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;
[System::STAThreadAttribute]
int main(array<System::String^>^ args)
{
Application^ app = gcnew Application();
Window^ window = gcnew Window();
window->Title = "C++/CLI WPF demo";
TextBlock^ tb = gcnew TextBlock();
tb->Text = "Hello WPF";
// Add root Grid
Grid^ rootGrid = gcnew Grid();
rootGrid->Width = 120;
rootGrid->Height = 120;
RowDefinition^ myRowDef1 = gcnew RowDefinition();
rootGrid->RowDefinitions->Add(myRowDef1);
DataGrid^ grid = gcnew DataGrid();
grid->Background = Brushes::LightBlue;
grid->Width = 80;
grid->Height = 100;
// Define the Canvas
Canvas^ mainCanvas = gcnew Canvas();
mainCanvas->Children->Add(tb);
mainCanvas->Children->Add(grid);
Canvas::SetTop(tb, 20);
Canvas::SetLeft(tb, 20);
Canvas::SetTop(grid, 50);
Canvas::SetLeft(grid, 20);
rootGrid->Children->Add(mainCanvas);
Grid::SetRow(mainCanvas, 0);
window->Content = rootGrid;
app->Run(window);
return 0;
}
代碼中的 [STAThread] 是需要的,等價于 [System::STAThread] 或 [System::STAThreadAttribute] .
還有個朋友說需要在項目屬性中設(shè)置"Entry Point"的值為"main",測試過了填與不填沒影響,建議別填。

接下來,可以build了。
如果出現(xiàn) VCRUNTIME140.dll missing 的問題,安裝一下Visual C++ Redistributable for Visual Studio 2015 和 Microsoft Visual C++ 2015 Redistributable Update 3 RC 可以解決,x64和x86的運行庫都需要安裝。
如果還不行,
- 下載VCRUNTIME140.DLL
- 以管理員權(quán)限復(fù)制這個 dll 到
C:\Windows\System32 - 檢查該 dll 的文件讀寫權(quán)限是否為
只讀,如果是只讀,去掉前面的勾勾.
此時按F5(或 Ctrl + F5),運行結(jié)果如下:

美中不足的是后面一直有個命令行窗口。
網(wǎng)上找了下解決方案,發(fā)現(xiàn)將目前用的 int main() 改為 int WINAPI WinMain() 可以解決,要能使用 WinMain() 則需要引入 windows.h 頭文件。
當(dāng)把 #include windows.h 加到 #include "MyComponent.h" 下一行時,發(fā)現(xiàn)如下錯誤:

原因在于命令空間沖突,使得 Window 的引用出現(xiàn)起義。
解決方法是: 將 #include windows.h 放在代碼的第一行。
此時,此時 MyComponent.cpp 的內(nèi)容如下:
#include "windows.h"
#include "MyComponent.h"
using namespace System::Windows;
using namespace System::Windows::Controls;
using namespace System::Windows::Media;
[STAThread]
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmd, int nCmd)
{
Application^ app = gcnew Application();
Window^ window = gcnew Window();
window->Title = "C++/CLI WPF demo";
TextBlock^ tb = gcnew TextBlock();
tb->Text = "Hello WPF";
// Add root Grid
Grid^ rootGrid = gcnew Grid();
rootGrid->Width = 120;
rootGrid->Height = 120;
RowDefinition^ myRowDef1 = gcnew RowDefinition();
rootGrid->RowDefinitions->Add(myRowDef1);
DataGrid^ grid = gcnew DataGrid();
grid->Background = Brushes::LightBlue;
grid->Width = 80;
grid->Height = 100;
// Define the Canvas
Canvas^ mainCanvas = gcnew Canvas();
mainCanvas->Children->Add(tb);
mainCanvas->Children->Add(grid);
Canvas::SetTop(tb, 20);
Canvas::SetLeft(tb, 20);
Canvas::SetTop(grid, 50);
Canvas::SetLeft(grid, 20);
rootGrid->Children->Add(mainCanvas);
Grid::SetRow(mainCanvas, 0);
window->Content = rootGrid;
app->Run(window);
return 0;
}
而運行結(jié)果為:

大白今天躺坑完畢,總算解決了問題,先醬~
第一個版本代碼已上傳到 github : https://github.com/yanglr/CppWpfDemo/tree/master/CppWpfDemo/CppWpfDemo .
到此這篇關(guān)于Visual C++下創(chuàng)建WPF項目的方法探索的文章就介紹到這了,更多相關(guān)Visual C++下創(chuàng)建WPF項目的方法探索內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- VS2019創(chuàng)建C++工程的的實現(xiàn)步驟
- VS2019創(chuàng)建c++動態(tài)鏈接庫dll與調(diào)用方法實踐
- Visual Studio 如何創(chuàng)建C/C++項目問題
- c++利用vector創(chuàng)建二維數(shù)組的幾種方法總結(jié)
- Visual Studio 2022 的安裝和創(chuàng)建C++項目(圖文教程)
- VS2019如何創(chuàng)建C++項目的實現(xiàn)示例
- 如何使用visual studio2019創(chuàng)建簡單的MFC窗口(使用C++)
- Visual Studio 2019創(chuàng)建C++ Hello World項目的方法
- 在Visual Studio中用C++語言創(chuàng)建DLL動態(tài)鏈接庫圖文教程
- VC++創(chuàng)建msi文件的方法
- VC++6.0中創(chuàng)建C++項目的實現(xiàn)步驟
相關(guān)文章
Windows系統(tǒng)下使用C語言編寫單線程的文件備份程序
這篇文章主要介紹了Windows系統(tǒng)下使用C語言編寫單線程的文件備份程序,文中給出了實現(xiàn)的幾個關(guān)鍵代碼片段,剩下的只要套上main和線程調(diào)用的相關(guān)函數(shù)即可,非常詳細,需要的朋友可以參考下2016-02-02

