最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

探索Visual C++下創(chuàng)建WPF項目的方法示例

 更新時間:2020年07月02日 08:29:51   作者:大白技術(shù)控  
這篇文章主要介紹了探索Visual C++下創(chuàng)建WPF項目的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

C++/CLI 下創(chuàng)建WPF項目的方法

由于WPF不僅僅支持C#/VB開發(fā),還支持其他語言,比如: C++、F#等開發(fā),于是大白我最近花了點(diǎn)時間摸索了一下,本文主要介紹 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)版本的運(yùn)行庫。

進(jìn)入 控制面板 ,找到 Visual Studio 2019,右擊"修改",然后切換到"獨(dú)立組件"(Individual components)這個選項卡。

如果沒安裝,勾選后安裝一下即可。

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

這時一個空項目就創(chuàng)建完成了。

此時查看 Project的屬性, Configration Properties -> "C/C++" -> "All Options",輸入 "common"進(jìn)行搜索,確保選中的是 Common Language Runtime Suppor(/clr) .

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

此時, MyComponent.cpp 中的代碼如下:

#include "MyComponent.h"

為了正確引用到 WPF 中的各種庫,我們還需要加入 WPF中 3 個核心的 dll,操作方法是:

右鍵點(diǎn)擊項目中的 References ,然后點(diǎn) Add Reference ,勾選上:

  • PresentationCore
  • PresentationFramework
  • WindowsBase

接下來,進(jìn)行了一番倒騰,我改成了這個,做成了一個簡單的界面:

此時 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的運(yùn)行庫都需要安裝。

如果還不行,

  • 下載VCRUNTIME140.DLL
  • 以管理員權(quán)限復(fù)制這個 dll 到 C:\Windows\System32
  • 檢查該 dll 的文件讀寫權(quán)限是否為 只讀 ,如果是只讀,去掉前面的勾勾.

此時按F5(或 Ctrl + F5),運(yùn)行結(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;
}

而運(yùn)行結(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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

库车县| 青浦区| 平潭县| 上思县| 桐城市| 城步| 宁陵县| 甘孜| 韶关市| 曲沃县| 鄢陵县| 天祝| 德阳市| 秦皇岛市| 甘孜县| 温宿县| 天津市| 东至县| 阜宁县| 通辽市| 永德县| 屏山县| 渑池县| 黑河市| 通州市| 宁武县| 三原县| 武城县| 郯城县| 运城市| 屏东县| 赤水市| 高州市| 望江县| 夏河县| 辽宁省| 汾阳市| 瓦房店市| 巫山县| 中西区| 文水县|