[学习][笔记]C++/WinRT入门 01Hello world

微软 C++/WinRT简介

windows RT开发笔记:WinRT DLL及其调用研究

How to access the Windows 10 APIs from C++

sample

C++/WinRT学习笔记(五):异步操作和强弱引用

使用 C++/WinRT 创建“Hello, World!” 应用

C++/WinRT入门

一、创建空白应用(C++/WinRT)

项目目录
在这里插入图片描述

xaml文件
XAML 标记文件以创建 UI 元素,并且可以将这些元素绑定到数据源(此任务称为数据绑定)。 你可以修改 .h 和 .cpp 文件(有时为 .idl 文件),为 XAML 页面添加自定义逻辑 — 例如事件处理程序。

App.xaml
App.idl、App.xaml、App.h 和 App.cpp。 这些文件表示应用对 Windows::UI::Xaml::Application 类的专业化,其中包括应用的入口点。 App.xaml 不包含任何特定于页面的标记,但你可以在其中添加用户界面元素样式,还可以添加你希望可从所有页面访问的任何其他元素。 .h 和 .cpp 文件包含各种应用程序生命周期事件的处理程序。 通常,你在此处添加自定义代码,以在应用启动时初始化应用并在它暂停或终止时执行清理。

MainPage.xaml
MainPage.idl、MainPage.xaml、MainPage.h 和 MainPage.cpp。 包含应用中默认主(启动)页面类型的 XAML 标记和实现,即 MainPage 运行时类。 MainPage 没有导航支持,但它提供了一些默认的 UI 和一个事件处理程序来帮助你入门。

pch.h
pch.h 和 pch.cpp。 这些文件表示项目的预编译头文件。 在 pch.h 中,包含不经常更改的任何头文件,然后将 pch.h 包含在项目中的其他文件中。

module.g.cpp

Package.appxmanifest

packages.config

PropertySheet.props
nut包管理相关配置

二、修改MainPage.xaml

步骤 1: 修改启动页 MainPage.xaml

删除StackPanel
粘贴复制以下内容到MainPage.xaml:

<StackPanel x:Name="contentPanel" Margin="120,30,0,0">
    <TextBlock HorizontalAlignment="Left" Text="Hello, World!" FontSize="36"/>
    <TextBlock Text="What's your name?"/>
    <StackPanel x:Name="inputPanel" Orientation="Horizontal" Margin="0,20,0,20">
        <TextBox x:Name="nameInput" Width="300" HorizontalAlignment="Left"/>
        <Button x:Name="inputButton" Content="Say &quot;Hello&quot;"/>
    </StackPanel>
    <TextBlock x:Name="greetingOutput"/>
</StackPanel>

已删除名为 myButton 的 Button,因此必须从代码中删除对它的引用。
MainPage.cpp 中删除 MainPage::ClickHandler 函数内的相应代码行。
MainPage.h中删除void ClickHandler

三、添加事件处理程序

在 MainPage.xaml 中,找到名为 inputButton 的 Button,并为其 ButtonBase::Click 事件声明一个事件处理程序。 Button 的标记现应如下所示。
XAML

复制

<Button x:Name="inputButton" Content="Say &quot;Hello&quot;" Click="inputButton_Click"/>

按如下所示实现该事件处理程序。
C++/WinRT
复制

// MainPage.h
struct MainPage : MainPageT<MainPage>
{
    ...
    void inputButton_Click(
        winrt::Windows::Foundation::IInspectable const& sender,
        winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
};

// MainPage.cpp
namespace winrt::HelloWorldCppWinRT::implementation
{
    ...
    void MainPage::inputButton_Click(
        winrt::Windows::Foundation::IInspectable const& sender,
        winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
    {
        greetingOutput().Text(L"Hello, " + nameInput().Text() + L"!");
    }
}

该实现从文本框中检索用户名称,使用该名称创建问候语,并在 greetingOutput 文本块中显示该问候语。
构建并运行应用。 在文本框中键入名称,然后单击按钮。 应用将显示个性化的问候语。

四、设置启动页的样式

选择主题

轻松自定义应用的外观。 默认情况下,应用使用具有浅色样式的资源。 系统资源还包含深色主题。
若要尝试使用深色主题,请编辑 App.xaml,并为 Application::RequestedTheme 添加一个值。

//App.xaml
<Application
    ...
    RequestedTheme="Dark">

</Application>

对于主要显示图像或视频的应用,我们建议使用深色主题;
对于包含大量文本的应用,我们建议使用浅色主题。
如果你使用的是自定义配色方案,则请使用最适合应用外观和感觉的主题。

备注
在应用启动时应用主题。 无法在应用运行时更改主题。

使用系统样式

更改文本的外观(例如将字号变大)。
在 MainPage.xaml 中,找到“What’s your name?” TextBlock。 将其 Style 属性设置为对 BaseTextBlockStyle 系统资源键的引用。
XAML

<TextBlock Text="What's your name?" Style="{ThemeResource HeaderTextBlockStyle}"/>

可以 对HeaderTextBlockStyle按F12查看具体属性

同样,在 MainPage.xaml 中找到名为 greetingOutput 的 TextBlock。 也将其 Style 设置为 HeaderTextBlockStyle。 如果你现在生成并运行应用,则可看到这两个文本块的外观已更改(例如字号现在变大了)。
在这里插入图片描述

五、使 UI 适应不同的窗口大小

调整 UI 布局
将此 XAML 块添加为根 StackPanel 元素的第一个子元素。

现在,我们将使 UI 动态地适应不断变化的窗口大小,使其在屏幕较小的设备上的显示效果不错。 为此,需要将 VisualStateManager 部分添加到 MainPage.xaml 中。 为不同的窗口大小定义不同的可视状态,然后设置属性以应用于每个可视状态。

MainPage.xaml修改后

<Page
    x:Class="_02HelloWinRTApp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:_02HelloWinRTApp"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel x:Name="contentPanel" Margin="120,30,0,0">
        <TextBlock HorizontalAlignment="Left" Text="Hello, World!" FontSize="36"/>
        <TextBlock Text="What's your name?" Style="{ThemeResource HeaderTextBlockStyle}"/>
        <StackPanel x:Name="inputPanel" Orientation="Horizontal" Margin="0,20,0,20">
            <TextBox x:Name="nameInput" Width="300" HorizontalAlignment="Left"/>
            <Button x:Name="inputButton" Content="Say &quot;Hello&quot;" Click="inputButton_Click" />
        </StackPanel>
        <TextBlock x:Name="greetingOutput" Style="{ThemeResource HeaderTextBlockStyle}" />
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup>
                <VisualState x:Name="wideState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="641" />
                    </VisualState.StateTriggers>
                </VisualState>
                <VisualState x:Name="narrowState">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="contentPanel.Margin" Value="20,30,0,0"/>
                        <Setter Target="inputPanel.Orientation" Value="Vertical"/>
                        <Setter Target="inputButton.Margin" Value="0,4,0,0"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </StackPanel>
    <!--<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
        <Button x:Name="myButton" Click="ClickHandler">Hello world</Button>
    </StackPanel>-->
</Page>

构建并运行应用。

请注意,UI 外观与以前相同,除非将窗口调整为窄于 641 与设备无关的像素 (DIP)。 此时,应用 narrowState 可视状态,同时应用为该状态定义的所有属性 Setter。
名为 wideState 的 VisualState 具有一个AdaptiveTrigger,并且其 MinWindowWidth 属性设置为 641 。这意味着仅在窗口宽度不小于 641 DIP的最小值时应用该状态。 你没有为此状态定义任何 Setter 对象, 因此它会将你在 XAML 中定义的布局属性用于页面内容。
名为narrowState 的第二个 VisualState 具有一个 AdaptiveTrigger,其 MinWindowWidth
属性设置为 0。 当窗口宽度大于 0 但小于 641 DIP 时,应用此状态。 正好为 641 DIP 时,wideState 将生效。
在narrowState 中,定义 Setter 对象以更改 UI 中控件的布局属性。 将 contentPanel 元素的左边距从 120
降低为 20。 将 inputPanel 元素的 Orientation 从 Horizontal 更改为 Vertical 。 将 4
DIP 的上边距添加到 inputButton 元素。

添加前
在这里插入图片描述
添加后在这里插入图片描述

总结

问题1

创建项目时候 第一次编译会提示各种头文件找不到
原因:
当前项目目录/Debug/Generated Files 一些头文件没有生成
解决办法是找一份生成了的拷进去(至少我是这样做的,[狗头],有更好办法请留言hhh)

问题2

删除clickhandler没删除干净 相关声明等 没重新编译 临时文件内提示。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
要在 Windows 10 中调用系统相机并录像,可以使用 Windows.Media.Capture 命名空间中的 API。以下是一个示例代码,它使用 Windows.Media.Capture 命名空间中的 API 捕获摄像头并将视频保存为 MP4 文件: ```c++ #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Media.Capture.h> #include <winrt/Windows.Storage.Streams.h> #include <iostream> using namespace winrt; using namespace Windows::Foundation; using namespace Windows::Media::Capture; using namespace Windows::Storage::Streams; using namespace std; int main() { // 创建 MediaCapture 对象 MediaCapture capture; capture.InitializeAsync().get(); // 创建 MediaEncodingProfile 对象 auto profile = MediaEncodingProfile::CreateMp4(VideoEncodingQuality::Auto); // 创建 StorageFile 对象并指定保存路径 auto file = co_await Windows::Storage::KnownFolders::VideosLibrary().CreateFileAsync( L"output.mp4", Windows::Storage::CreationCollisionOption::GenerateUniqueName); // 创建 MediaTranscoder 对象并指定输出文件 auto transcoder = MediaTranscoder(); transcoder.SetOutputFileAsync(file).get(); // 开始录像 auto record = capture.PrepareLowLagRecordToStorageFileAsync(profile, file).get(); record.StartAsync().get(); cout << "正在录像,请按 Enter 停止录像..." << endl; cin.get(); // 停止录像 record.StopAsync().get(); return 0; } ``` 这个示例代码会初始化 MediaCapture 对象,并使用 MediaEncodingProfile 来指定输出视频的编码格式和质量。然后,它会创建一个 StorageFile 对象来指定保存视频的路径,接着创建 MediaTranscoder 对象并将输出文件指定为刚才创建的 StorageFile 对象。最后,它会使用 PrepareLowLagRecordToStorageFileAsync 方法开始录像,等待用户按下 Enter 后再使用 StopAsync 方法停止录像。你可以修改输出视频的编码格式和质量,以及视频文件的名称和路径。需要注意的是,这个示例代码使用了 C++/WinRT,你需要将项目设置为使用 C++/WinRT,或手动添加 C++/WinRT 的头文件和库文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二进制怪兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值