美文网首页互联网科技让前端飞我的大学
Visual C++下创建WPF项目的方法探索

Visual C++下创建WPF项目的方法探索

作者: 大白技术控 | 来源:发表于2020-06-29 17:24 被阅读0次

    C++/CLI下创建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)这个选项卡。

    img1

    如果没安装,勾选后安装一下即可。

    接下来我们可以创建项目了,建议选用模板 CLR Empty Project (.NET Framework),解决方案和项目名可以都用CppWpfDemo

    img2

    这时一个空项目就创建完成了。

    此时查看 Project的属性,Configration Properties -> "C/C++" -> "All Options",输入 "common"进行搜索,确保选中的是 Common Language Runtime Suppor(/clr).

    img3

    接下来我们鼠标右击项目下的文件夹"Resource Files",点"Add" -> "new item",类型选"Component Class",可使用默认的名字MyComponent

    img4

    此时,MyComponent.cpp中的代码如下:

    #include "MyComponent.h"
    

    为了正确引用到 WPF 中的各种库,我们还需要加入 WPF中 3 个核心的 dll,操作方法是:

    右键点击项目中的 References,然后点 Add Reference,勾选上:

    • PresentationCore
    • PresentationFramework
    • WindowsBase
    img5

    接下来,进行了一番倒腾,我改成了这个,做成了一个简单的界面:

    此时 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",测试过了填与不填没影响,建议别填。

    image6

    接下来,可以build了。

    如果出现VCRUNTIME140.dll missing的问题,安装一下Visual C++ Redistributable for Visual Studio 2015Microsoft Visual C++ 2015 Redistributable Update 3 RC 可以解决,x64和x86的运行库都需要安装。

    如果还不行,

    • 下载 VCRUNTIME140.DLL
    • 以管理员权限复制这个 dll 到 C:\Windows\System32
    • 检查该 dll 的文件读写权限是否为只读,如果是只读,去掉前面的勾勾.

    此时按F5(或 Ctrl + F5),运行结果如下:

    image9

    美中不足的是后面一直有个命令行窗口。

    网上找了下解决方案,发现将目前用的 int main()改为int WINAPI WinMain() 可以解决,要能使用WinMain()则需要引入windows.h头文件。

    当把 #include windows.h加到#include "MyComponent.h"下一行时,发现如下错误:

    image7

    原因在于命令空间冲突,使得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;
    }
    

    而运行结果为:

    image8

    大白今天躺坑完毕,总算解决了问题,先酱~

    第一个版本代码已上传到 github: https://github.com/yanglr/CppWpfDemo/tree/master/CppWpfDemo/CppWpfDemo.

    改天接着聊,欢迎来评论区留言互动哈~

    相关文章

      网友评论

        本文标题:Visual C++下创建WPF项目的方法探索

        本文链接:https://www.haomeiwen.com/subject/vrhafktx.html