トップ回答者
C++/WinRT、Win2DでCanvasTextFormatクラスのオブジェクト生成方法を教えてください

質問
-
いつもお世話になります。質問ばかりで恐縮ですが、よろしくお願いします。
args.DrawingSession().DrawText()関数で文字を描画する際に、フォントの種類とサイズをコードで指定したいのですが、うまくいきません。
1.参考にしたサイト
(1)c++ winrt - How to DrawText in Win2d with text,rect,color, and format arguments? - Stack Overflow
(2)CanvasTextFormat Class (microsoft.github.io)
2.これまでにやってみたこと
DrawText()関数の最後の引数(名前は「format」とします)で書式指定をするのだと思いますが、「format」の生成方法がわかりません。次のようにプログラムしてみたのですが、いずれも1行追加するだけで、ビルドエラーになります。
2.1 1項(1)のサイトを参考にしたプログラム
(1)プログラム
winrt::Microsoft::Graphics::Canvas::Text::CanvasTextFormat format;
(2)ビルドエラー
1>D:\WinRT\TestProgram\win2d_3\x64\Debug\win2d_1\win2d_1.exe : fatal error LNK1120: 1 件の未解決の外部参照
2.2 1項(2)のサイトを参考にしたプログラム
(1)プログラム
auto format = new winrt::Microsoft::Graphics::Canvas::Text::CanvasTextFormat();
(2)ビルドエラー
1>D:\WinRT\TestProgram\win2d_3\win2d_1\MainPage.cpp(70,1): error C2280: 'void *winrt::Windows::Foundation::IUnknown::operator new(size_t)': 削除された関数を参照しようとしています
1> D:\WinRT\TestProgram\win2d_3\win2d_1\Generated Files\winrt\base.h(2028): message : 'winrt::Windows::Foundation::IUnknown::operator new' の宣言を確認してください
1>C:\Program Files (x86)\Windows Kits\10\bin\10.0.19041.0\XamlCompiler
\Microsoft.Windows.UI.Xaml.Common.targets(486,5): error MSB4181: "CompileXaml" タスクから false が返されましたが、エラーがログに記録されませんでした。
以上です。
回答
-
前のコードをちょっと変えて
//pch.h #pragma once #include <windows.h> #include <unknwn.h> #include <restrictederrorinfo.h> #include <hstring.h> #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.ApplicationModel.Activation.h> #include <winrt/Windows.UI.Xaml.h> #include <winrt/Windows.UI.Xaml.Controls.h> #include <winrt/Windows.UI.Xaml.Controls.Primitives.h> #include <winrt/Windows.UI.Xaml.Data.h> #include <winrt/Windows.UI.Xaml.Interop.h> #include <winrt/Windows.UI.Xaml.Markup.h> #include <winrt/Windows.UI.Xaml.Navigation.h> #include <winrt/Windows.UI.Input.h> #include <winrt/Windows.UI.Xaml.Input.h> #include <winrt/Microsoft.Graphics.Canvas.UI.Xaml.h> #include <winrt/Windows.UI.Text.Core.h> #include <winrt/Microsoft.Graphics.Canvas.Text.h>
//MainPage.cpp #include "pch.h" #include "MainPage.h" #include "MainPage.g.cpp" using namespace winrt; using namespace Windows::Foundation; using namespace Windows::UI::Input; using namespace Windows::UI::Xaml; using namespace Microsoft::Graphics::Canvas::UI::Xaml; using namespace Microsoft::Graphics::Canvas::Text; using namespace winrt::Windows::UI::Text; namespace winrt::BlankApp1::implementation { void MainPage::draw1(CanvasDrawEventArgs const& args) { auto weight = winrt::Windows::UI::Text::FontWeights::Bold(); auto stretch = winrt::Windows::UI::Text::FontStretch::Expanded; auto style = winrt::Windows::UI::Text::FontStyle::Italic; hstring fontFamilyName = L"BIZ UDPゴシック"; Microsoft::Graphics::Canvas::Text::CanvasTextFormat format; //こっちでも //Microsoft::Graphics::Canvas::Text::CanvasTextFormat format{ nullptr }; //format = Microsoft::Graphics::Canvas::Text::CanvasTextFormat(); format.FontSize(50); format.FontWeight(weight); format.FontStretch(stretch); format.FontStyle(style); format.FontFamily(fontFamilyName); args.DrawingSession().DrawText(L"Hello, world!", 10.0f, 10.0f, winrt::Windows::UI::Colors::Black(), format); ////フォントがあるか調べる場合 //CanvasFontSet fontSets = CanvasFontSet::GetSystemFontSet(); //for (auto f : fontSets.Fonts()) //{ // if (f.Weight() == FontWeights::Bold() && f.Style() == FontStyle::Italic) // { // } // Windows::Foundation::Collections::IMapView<hstring, hstring> familyNames = f.FamilyNames(); // for (auto fn : f.FamilyNames()) // { // auto key = fn.Key(); // auto name = fn.Value(); // size_t size = key.size() + name.size() + 4; // wchar_t* buff; // buff = new wchar_t[size](); // swprintf_s(buff, size, L"%s\t%s\r\n", key.c_str(), name.c_str()); // OutputDebugString(buff); // delete[] buff; // if (name == L"BIZ UDPゴシック") //フォントが見つかった // { // format.FontFamily(name); // // break; // } // } //} } (以下略)
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク fghck856 2022年7月23日 8:45
すべての返信
-
前のコードをちょっと変えて
//pch.h #pragma once #include <windows.h> #include <unknwn.h> #include <restrictederrorinfo.h> #include <hstring.h> #include <winrt/Windows.Foundation.h> #include <winrt/Windows.Foundation.Collections.h> #include <winrt/Windows.ApplicationModel.Activation.h> #include <winrt/Windows.UI.Xaml.h> #include <winrt/Windows.UI.Xaml.Controls.h> #include <winrt/Windows.UI.Xaml.Controls.Primitives.h> #include <winrt/Windows.UI.Xaml.Data.h> #include <winrt/Windows.UI.Xaml.Interop.h> #include <winrt/Windows.UI.Xaml.Markup.h> #include <winrt/Windows.UI.Xaml.Navigation.h> #include <winrt/Windows.UI.Input.h> #include <winrt/Windows.UI.Xaml.Input.h> #include <winrt/Microsoft.Graphics.Canvas.UI.Xaml.h> #include <winrt/Windows.UI.Text.Core.h> #include <winrt/Microsoft.Graphics.Canvas.Text.h>
//MainPage.cpp #include "pch.h" #include "MainPage.h" #include "MainPage.g.cpp" using namespace winrt; using namespace Windows::Foundation; using namespace Windows::UI::Input; using namespace Windows::UI::Xaml; using namespace Microsoft::Graphics::Canvas::UI::Xaml; using namespace Microsoft::Graphics::Canvas::Text; using namespace winrt::Windows::UI::Text; namespace winrt::BlankApp1::implementation { void MainPage::draw1(CanvasDrawEventArgs const& args) { auto weight = winrt::Windows::UI::Text::FontWeights::Bold(); auto stretch = winrt::Windows::UI::Text::FontStretch::Expanded; auto style = winrt::Windows::UI::Text::FontStyle::Italic; hstring fontFamilyName = L"BIZ UDPゴシック"; Microsoft::Graphics::Canvas::Text::CanvasTextFormat format; //こっちでも //Microsoft::Graphics::Canvas::Text::CanvasTextFormat format{ nullptr }; //format = Microsoft::Graphics::Canvas::Text::CanvasTextFormat(); format.FontSize(50); format.FontWeight(weight); format.FontStretch(stretch); format.FontStyle(style); format.FontFamily(fontFamilyName); args.DrawingSession().DrawText(L"Hello, world!", 10.0f, 10.0f, winrt::Windows::UI::Colors::Black(), format); ////フォントがあるか調べる場合 //CanvasFontSet fontSets = CanvasFontSet::GetSystemFontSet(); //for (auto f : fontSets.Fonts()) //{ // if (f.Weight() == FontWeights::Bold() && f.Style() == FontStyle::Italic) // { // } // Windows::Foundation::Collections::IMapView<hstring, hstring> familyNames = f.FamilyNames(); // for (auto fn : f.FamilyNames()) // { // auto key = fn.Key(); // auto name = fn.Value(); // size_t size = key.size() + name.size() + 4; // wchar_t* buff; // buff = new wchar_t[size](); // swprintf_s(buff, size, L"%s\t%s\r\n", key.c_str(), name.c_str()); // OutputDebugString(buff); // delete[] buff; // if (name == L"BIZ UDPゴシック") //フォントが見つかった // { // format.FontFamily(name); // // break; // } // } //} } (以下略)
個別に明示されていない限りgekkaがフォーラムに投稿したコードにはフォーラム使用条件に基づき「MICROSOFT LIMITED PUBLIC LICENSE」が適用されます。(かなり自由に使ってOK!)
- 回答としてマーク fghck856 2022年7月23日 8:45