C++/CLI中創建WPF項目的方法
C++/CLI
下創建WPF項目的方法
Visual C++
中創建WPF項目的方法
由於WPF不僅僅支持C#/VB開發,還支持其他語言,比如: C++、F#等開發,於是大白我最近花了點時間摸索了一下,本文主要介紹C++/CLI
下創建WPF項目的方法。
我使用的開發環境是: Win10 x64 + Visual Studio 2019 (16.6.1版本)。
今天我們需要使用C++/CLI
,算是C++的一個子集吧。
要能正常使用C++/CLI
,首先需要確保你安裝了C++/CLI
build套件(見下圖),同時還需要確保你安裝好了Visual C++相應版本的運行庫。
進入控制面板
,找到 Visual Studio 2019,右擊"修改",然後切換到"獨立組件"(Individual components)這個選項卡。
如果沒安裝,勾選後安裝一下即可。
接下來我們可以創建項目了,建議選用模板 CLR Empty Project (.NET Framework)
,解決方案和項目名可以都用CppWpfDemo
。
這時一個空項目就創建完成了。
此時查看 Project的屬性,Configration Properties
-> “C/C++” -> “All Options”,輸入 "common"進行搜索,確保選中的是 Common Language Runtime Suppor(/clr)
.
接下來我們鼠標右擊項目下的文件夾"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
的內容如下:
#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]
.
還有個朋友說需要在項目屬性中設置"Entry Point"的值爲"main",測試過了填與不填沒影響,建議別填。
接下來,可以build了。
如果出現VCRUNTIME140.dll missing
的問題,安裝一下Visual C++ Redistributable for Visual Studio 2015 和 Microsoft Visual C++ 2015 Redistributable Update 3 RC 可以解決,x64和x86的運行庫都需要安裝。
如果還不行,
- 下載 VCRUNTIME140.DLL
- 以管理員權限複製這個 dll 到
C:\Windows\System32
- 檢查該 dll 的文件讀寫權限是否爲
只讀
,如果是隻讀,去掉前面的勾勾.
此時按F5(或 Ctrl + F5),運行結果如下:
美中不足的是後面一直有個命令行窗口。
網上找了下解決方案,發現將目前用的 int main()
改爲int WINAPI WinMain()
可以解決,要能使用WinMain()
則需要引入windows.h
頭文件。
當把 #include windows.h
加到#include "MyComponent.h"
下一行時,發現如下錯誤:
原因在於命令空間衝突,使得Window
的引用出現起義。
解決方法是: 將 #include windows.h
放在代碼的第一行。
此時,此時 MyComponent.cpp
的內容如下:
#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;
}
而運行結果爲:
大白今天躺坑完畢,總算解決了問題,先醬~
第一個版本代碼已上傳到 github
: https://github.com/yanglr/CppWpfDemo/tree/master/CppWpfDemo/CppWpfDemo.
改天接着聊,歡迎來評論區留言互動哈~