From 1dd6e993401042e318f43a4240804e731cde520e Mon Sep 17 00:00:00 2001 From: Mike Griese Date: Mon, 18 Jul 2022 16:57:00 -0500 Subject: [PATCH] prototype --- scratch/ScratchIslandApp/SampleApp/MyPage.cpp | 77 ++++++++++++++++++- .../ScratchIslandApp/SampleApp/MyPage.xaml | 29 +++---- .../SampleApp/dll/SampleApp.vcxproj | 7 ++ scratch/ScratchIslandApp/SampleApp/pch.h | 4 + 4 files changed, 96 insertions(+), 21 deletions(-) diff --git a/scratch/ScratchIslandApp/SampleApp/MyPage.cpp b/scratch/ScratchIslandApp/SampleApp/MyPage.cpp index b230f573fb0..9c0e2b76053 100644 --- a/scratch/ScratchIslandApp/SampleApp/MyPage.cpp +++ b/scratch/ScratchIslandApp/SampleApp/MyPage.cpp @@ -8,7 +8,10 @@ #include "MyPage.g.cpp" #include "..\..\..\src\cascadia\UnitTests_Control\MockControlSettings.h" #include "..\..\..\src\types\inc\utils.hpp" - +#include +#include +#include +#include using namespace std::chrono_literals; using namespace winrt::Microsoft::Terminal; @@ -19,6 +22,9 @@ namespace winrt using IInspectable = Windows::Foundation::IInspectable; } +using namespace winrt::Windows::Graphics::Imaging; +using namespace winrt::Windows::Storage::Streams; + namespace winrt::SampleApp::implementation { MyPage::MyPage() @@ -30,10 +36,79 @@ namespace winrt::SampleApp::implementation { } + winrt::Windows::Graphics::Imaging::SoftwareBitmap MyConvertToSoftwareBitmap(HICON hicon, + winrt::Windows::Graphics::Imaging::BitmapPixelFormat pixelFormat, + winrt::Windows::Graphics::Imaging::BitmapAlphaMode alphaMode, + IWICImagingFactory* imagingFactory) + { + // Load the icon into an IWICBitmap + wil::com_ptr iconBitmap; + THROW_IF_FAILED(imagingFactory->CreateBitmapFromHICON(hicon, iconBitmap.put())); + + // Put the IWICBitmap into a SoftwareBitmap. This may fail if WICBitmap's format is not supported by + // SoftwareBitmap. CreateBitmapFromHICON always creates RGBA8 so we're ok. + auto softwareBitmap = winrt::capture( + winrt::create_instance(CLSID_SoftwareBitmapNativeFactory), + &ISoftwareBitmapNativeFactory::CreateFromWICBitmap, + iconBitmap.get(), + false); + + // Convert the pixel format and alpha mode if necessary + if (softwareBitmap.BitmapPixelFormat() != pixelFormat || softwareBitmap.BitmapAlphaMode() != alphaMode) + { + softwareBitmap = winrt::Windows::Graphics::Imaging::SoftwareBitmap::Convert(softwareBitmap, pixelFormat, alphaMode); + } + + return softwareBitmap; + } + + winrt::Windows::Graphics::Imaging::SoftwareBitmap MyGetBitmapFromIconFileAsync(const winrt::hstring& iconPath, + int32_t iconIndex, + uint32_t iconSize) + { + wil::unique_hicon hicon; + LOG_IF_FAILED(SHDefExtractIcon(iconPath.c_str(), iconIndex, 0, &hicon, nullptr, iconSize)); + + if (!hicon) + { + return nullptr; + } + + wil::com_ptr wicImagingFactory; + THROW_IF_FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicImagingFactory))); + + return MyConvertToSoftwareBitmap(hicon.get(), BitmapPixelFormat::Bgra8, BitmapAlphaMode::Premultiplied, wicImagingFactory.get()); + } + winrt::fire_and_forget MyPage::CreateClicked(const IInspectable& sender, const WUX::Input::TappedRoutedEventArgs& eventArgs) { + // Try: + // * c:\Windows\System32\SHELL32.dll, 210 + // * c:\Windows\System32\notepad.exe, 0 + // * C:\Program Files\PowerShell\6-preview\pwsh.exe, 0 (this doesn't exist for me) + // * C:\Program Files\PowerShell\7\pwsh.exe, 0 + auto text{ GuidInput().Text() }; + auto index{ static_cast(IconIndex().Value()) }; + co_await winrt::resume_background(); + auto swBitmap{ MyGetBitmapFromIconFileAsync(text, index, 32) }; + if (swBitmap == nullptr) + { + co_return; + } + co_await winrt::resume_foreground(Dispatcher()); + winrt::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource bitmapSource{}; + co_await bitmapSource.SetBitmapAsync(swBitmap); + co_await winrt::resume_foreground(Dispatcher()); + + winrt::Microsoft::UI::Xaml::Controls::ImageIconSource imageIconSource{}; + imageIconSource.ImageSource(bitmapSource); + winrt::Microsoft::UI::Xaml::Controls::ImageIcon icon{}; + icon.Source(bitmapSource); + icon.Width(32); + icon.Height(32); + InProcContent().Children().Append(icon); } void MyPage::CloseClicked(const IInspectable& /*sender*/, diff --git a/scratch/ScratchIslandApp/SampleApp/MyPage.xaml b/scratch/ScratchIslandApp/SampleApp/MyPage.xaml index 033651fae76..cb87b5fe6d4 100644 --- a/scratch/ScratchIslandApp/SampleApp/MyPage.xaml +++ b/scratch/ScratchIslandApp/SampleApp/MyPage.xaml @@ -22,24 +22,12 @@ + PlaceholderText="path here" /> + - - @@ -53,12 +41,13 @@ - + + + + + + $(OpenConsoleCommonOutDir)\ConTypes.lib;WindowsApp.lib;shell32.lib;user32.lib;%(AdditionalDependencies) + + diff --git a/scratch/ScratchIslandApp/SampleApp/pch.h b/scratch/ScratchIslandApp/SampleApp/pch.h index ba01e45e453..347389b44a1 100644 --- a/scratch/ScratchIslandApp/SampleApp/pch.h +++ b/scratch/ScratchIslandApp/SampleApp/pch.h @@ -33,6 +33,8 @@ #include #include #include +#include +// #include #include #include #include @@ -41,10 +43,12 @@ #include #include #include +#include #include #include #include "winrt/Windows.UI.Xaml.Markup.h" #include "winrt/Windows.UI.ViewManagement.h" +#include "winrt/Windows.Storage.Streams.h" #include #include