在测试一些 Kenny Kerr 的 C++/Winrt 示例代码时,我发现了以下画布绘图操作演示:
CanvasTextFormat format;
format.HorizontalAlignment(CanvasHorizontalAlignment::Center);
format.VerticalAlignment(CanvasVerticalAlignment::Center);
format.FontSize(72.0f);
format.FontFamily(L"Segoe UI Semibold");
control.Draw([=](CanvasControl const& sender, CanvasDrawEventArgs const& args)
{
float2 size = sender.Size();
float2 center{ size.x / 2.0f, size.y / 2.0f };
Rect bounds{ 0.0f, 0.0f, size.x, size.y };
CanvasDrawingSession session = args.DrawingSession();
session.FillEllipse(center, center.x - 50.0f, center.y - 50.0f, Colors::DarkSlateGray());
session.DrawText(L"Win2D with\nC++/WinRT!", bounds, Colors::Orange(), format);
});
绘图操作在我的项目中效果很好,除了文本绘图 - 那条线不会像写的那样构建。VS 说参数 2 不能从 Rect 转换为浮点数:
C2664 'void winrt::impl::consume_Microsoft_Graphics_Canvas_ICanvasDrawingSession<winrt::Microsoft::Graphics::Canvas::ICanvasDrawingSession>::DrawText(const winrt::param::hstring &,float,float,float,float,const winrt::Windows::UI::Color &,const winrt::Microsoft::Graphics::Canvas::Text::CanvasTextFormat &) const': cannot convert argument 2 from 'winrt::Windows::Foundation::Rect' to 'float'
它似乎想将此调用解释为采用四个浮点参数的版本,即使我键入第二个参数也是如此。然而,winrt 中的一个 DrawText 方法清楚地表明参数 2 可以是一个矩形。这是唯一也使用颜色和格式参数的变体:
模板 void consume_Microsoft_Graphics_Canvas_ICanvasDrawingSession::DrawText(param::hstring const& text, Windows::Foundation::Rect const& rectangle, Windows::UI::Color const& color, Microsoft::Graphics::Canvas::Text::CanvasTextFormat const& 格式) const { check_hresult(WINRT_SHIM(Microsoft::Graphics::Canvas::ICanvasDrawingSession)->DrawTextAtRectWithColorAndFormat(get_abi(text), get_abi(rectangle), get_abi(color), get_abi(format))); }
在我看来,VS 并没有告诉我我的实现有什么问题——也许还有其他问题,而不是参数 2 不是浮点数?我试图密切复制样本对 DrawText 的使用。
[更新] 好奇和好奇:突然 VS 也返回文本格式的链接错误 - 以前它没有问题,但只有调用 DrawText。也许这是一个暗示。我试着改变椭圆的颜色;文本格式不会链接;将椭圆颜色改回原来的样子,但它仍然无法构建,尽管我根本没有触及格式声明。这是链接错误:
Error LNK2019 unresolved external symbol "public: void __thiscall winrt::impl::consume_Microsoft_Graphics_Canvas_Text_ICanvasTextFormat<struct winrt::Microsoft::Graphics::Canvas::Text::ICanvasTextFormat>::FontFamily(struct winrt::param::hstring const &)const " (?FontFamily@?$consume_Microsoft_Graphics_Canvas_Text_ICanvasTextFormat@UICanvasTextFormat@Text@Canvas@Graphics@Microsoft@winrt@@@impl@winrt@@QBEXABUhstring@param@3@@Z) referenced in function "public: void __thiscall <lambda_05084435c3f7f2f96a04d3453f0476aa>::operator()(struct winrt::Microsoft::Graphics::Canvas::UI::Xaml::CanvasControl const &,struct winrt::Microsoft::Graphics::Canvas::UI::Xaml::CanvasDrawEventArgs const &)const " (??R<lambda_05084435c3f7f2f96a04d3453f0476aa>@@QBEXABUCanvasControl@Xaml@UI@Canvas@Graphics@Microsoft@winrt@@ABUCanvasDrawEventArgs@234567@@Z)
所以,现在如果我注释掉文本格式的声明,它将构建并运行。以前它与该礼物一起运行良好 - 只是无法链接 DrawText 调用。