zoukankan      html  css  js  c++  java
  • Win系列:VC++编写自定义组件

    在Visual Studio 中新建一个Visual C++的 Windows应用商店的Windows运行时组件项目,并将项目命名为FilePickerComponent。然后在项目的解决方案资源管理器窗口中右键点击项目图标,在弹出的菜单栏中选中"添加", 并在"添加"的子菜单栏中选择"新建项",在出现的"添加新项"窗口中选中"头文件(.h)",添加名为"FilePicker.h"的头文件。然后使用同样的方法在"添加新项"窗口中选中"C++ 文件(.cpp)",并添加名为"FilePicker.cpp"的源文件。

    接下来打开FilePicker.h头文件,并添加如下的代码:

    #pragma once

    namespace FilePickerComponent

    {

        public ref class FilePicker sealed

        {

        public:

            //构造函数

            FilePicker();

        private:

            //声明成员变量openPicker

            Windows::Storage::Pickers::FileOpenPicker^ openPicker;

        public:

            //声明属性FileContent

            property Platform::String^ FileContent;

        public:

            //读取文件

            void ReadFile();

        };

    }

    在上面的代码中,定义了一个FilePicker类,在这个类中使用public关键字声明一个公有的构造函数FilePicker。接着使用private关键字声明一个FileOpenPicker类型的私有成员变量openPicker,并使用public关键字声明一个String类型的公有属性FileContent。最后使用public关键字声明一个公有的ReadFile函数,用于读取文件。

    定义了FilePicker类以后,接下来打开FilePicker.cpp源文件,添加如下的代码:

    // FilePicker.cpp

    #include "pch.h"

    #include "FilePicker.h"

    #include "ppltasks.h"

    using namespace Windows::Foundation;

    using namespace FilePickerComponent;

    using namespace Platform;

    using namespace Windows::Storage;

    using namespace Windows::Storage::Streams;

    using namespace Windows::Storage::Pickers;

    using namespace Concurrency;

    在上面的代码中,使用include关键字引用头文件pch.h、FilePicker.h和ppltasks.h,并使用using指令引用命名空间Windows::Foundation、FilePickerComponent、Platform、Windows::Storage、Windows::Storage::Streams、Windows::Storage::Pickers和Concurrency。

    引用了上面的头文件和命名空间以后,接下来在FilePicker.cpp源文件中添加FilePicker构造函数的实现代码,此构造函数中并不实现任何的功能,具体代码如下所示:

    FilePicker::FilePicker()

    {

    }

    添加了FilePicker构造函数的实现代码以后,接下来在FilePicker.cpp源文件中添加ReadFile函数的实现代码,具体代码如下所示:

    //读取文件

    void FilePickerComponent::FilePicker::ReadFile()

    {

        //创建文件打开选择器

        openPicker =ref new FileOpenPicker();

        //设置视图模式

        openPicker->ViewMode = PickerViewMode::List;

        //设置访问的初始位置

        openPicker->SuggestedStartLocation = PickerLocationId:: Desktop;

        //设置显示的文件类型,允许读取的文件类型

        openPicker->FileTypeFilter->Append(".txt");

        openPicker->FileTypeFilter->Append(".BAK");

        //显示文件选择器,选择文件

        create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)

        {

            //读取文件内容

            return FileIO::ReadTextAsync(file,UnicodeEncoding::Utf8);

        }).then([this](task<String^> fileTask){

            //将文本内容赋值给FileContent

            FileContent = fileTask.get();

        });

    }

    在上面的代码中,首先创建一个FileOpenPicker类的对象openPicker,并将PickerViewMode枚举中的枚举成员List赋值给openPicker对象的ViewMode属性,设置文件打开选取器的视图模式为列表模式。接着将PickerLocationId枚举中的枚举成员Desktop赋值给openPicker对象的SuggestedStartLocation属性,设置文件打开选取器的初始位置为桌面。然后使用openPicker对象的FileTypeFilter属性得到文件打开选取器显示的文件类型集合,并调用Append函数将文件类型".txt"和".BAK"添加到这个集合中。

    接下来调用openPicker对象的PickSingleFileAsync函数选取单个文件,并得到一个StorageFile类型的对象file。然后将file对象作为参数传递给FileIO类的ReadTextAsync函数,并以Utf8编码格式来读取所选择的文件,得到一个task<String^>类型的对象fileTask。最后调用fileTask对象的get函数得到文件中的内容,并赋值给FileContent属性。

  • 相关阅读:
    西山赏梅归来
    创建 WebRequest 实例并返回响应
    C# WinForm开发系列 DataGridView
    DataTable C#
    delphi 制作资源文件
    Windows Mobile上使用钩子
    OO系统分析员之路用例分析系列(7)用例规约的编写业务规则和实体描述[整理重发]
    iis下PHP5配置
    ie firefox 点flash 卡死解决方案
    PHP获取字符串编码类型
  • 原文地址:https://www.cnblogs.com/finehappy/p/6645675.html
Copyright © 2011-2022 走看看