8
8
#include " MyPage.g.cpp"
9
9
#include " ..\..\..\src\cascadia\UnitTests_Control\MockControlSettings.h"
10
10
#include " ..\..\..\src\types\inc\utils.hpp"
11
-
11
+ #include < Shlobj.h>
12
+ #include < Shlobj_core.h>
13
+ #include < wincodec.h>
14
+ #include < Windows.Graphics.Imaging.Interop.h>
12
15
using namespace std ::chrono_literals;
13
16
using namespace winrt ::Microsoft::Terminal;
14
17
@@ -19,6 +22,9 @@ namespace winrt
19
22
using IInspectable = Windows::Foundation::IInspectable;
20
23
}
21
24
25
+ using namespace winrt ::Windows::Graphics::Imaging;
26
+ using namespace winrt ::Windows::Storage::Streams;
27
+
22
28
namespace winrt ::SampleApp::implementation
23
29
{
24
30
MyPage::MyPage ()
@@ -30,10 +36,79 @@ namespace winrt::SampleApp::implementation
30
36
{
31
37
}
32
38
39
+ winrt::Windows::Graphics::Imaging::SoftwareBitmap MyConvertToSoftwareBitmap (HICON hicon,
40
+ winrt::Windows::Graphics::Imaging::BitmapPixelFormat pixelFormat,
41
+ winrt::Windows::Graphics::Imaging::BitmapAlphaMode alphaMode,
42
+ IWICImagingFactory* imagingFactory)
43
+ {
44
+ // Load the icon into an IWICBitmap
45
+ wil::com_ptr<IWICBitmap> iconBitmap;
46
+ THROW_IF_FAILED (imagingFactory->CreateBitmapFromHICON (hicon, iconBitmap.put ()));
47
+
48
+ // Put the IWICBitmap into a SoftwareBitmap. This may fail if WICBitmap's format is not supported by
49
+ // SoftwareBitmap. CreateBitmapFromHICON always creates RGBA8 so we're ok.
50
+ auto softwareBitmap = winrt::capture<winrt::Windows::Graphics::Imaging::SoftwareBitmap>(
51
+ winrt::create_instance<ISoftwareBitmapNativeFactory>(CLSID_SoftwareBitmapNativeFactory),
52
+ &ISoftwareBitmapNativeFactory::CreateFromWICBitmap,
53
+ iconBitmap.get (),
54
+ false );
55
+
56
+ // Convert the pixel format and alpha mode if necessary
57
+ if (softwareBitmap.BitmapPixelFormat () != pixelFormat || softwareBitmap.BitmapAlphaMode () != alphaMode)
58
+ {
59
+ softwareBitmap = winrt::Windows::Graphics::Imaging::SoftwareBitmap::Convert (softwareBitmap, pixelFormat, alphaMode);
60
+ }
61
+
62
+ return softwareBitmap;
63
+ }
64
+
65
+ winrt::Windows::Graphics::Imaging::SoftwareBitmap MyGetBitmapFromIconFileAsync (const winrt::hstring& iconPath,
66
+ int32_t iconIndex,
67
+ uint32_t iconSize)
68
+ {
69
+ wil::unique_hicon hicon;
70
+ LOG_IF_FAILED (SHDefExtractIcon (iconPath.c_str (), iconIndex, 0 , &hicon, nullptr , iconSize));
71
+
72
+ if (!hicon)
73
+ {
74
+ return nullptr ;
75
+ }
76
+
77
+ wil::com_ptr<IWICImagingFactory> wicImagingFactory;
78
+ THROW_IF_FAILED (CoCreateInstance (CLSID_WICImagingFactory, nullptr , CLSCTX_INPROC_SERVER, IID_PPV_ARGS (&wicImagingFactory)));
79
+
80
+ return MyConvertToSoftwareBitmap (hicon.get (), BitmapPixelFormat::Bgra8, BitmapAlphaMode::Premultiplied, wicImagingFactory.get ());
81
+ }
82
+
33
83
winrt::fire_and_forget MyPage::CreateClicked (const IInspectable& sender,
34
84
const WUX::Input::TappedRoutedEventArgs& eventArgs)
35
85
{
86
+ // Try:
87
+ // * c:\Windows\System32\SHELL32.dll, 210
88
+ // * c:\Windows\System32\notepad.exe, 0
89
+ // * C:\Program Files\PowerShell\6-preview\pwsh.exe, 0 (this doesn't exist for me)
90
+ // * C:\Program Files\PowerShell\7\pwsh.exe, 0
91
+ auto text{ GuidInput ().Text () };
92
+ auto index { static_cast <int >(IconIndex ().Value ()) };
93
+
36
94
co_await winrt::resume_background ();
95
+ auto swBitmap{ MyGetBitmapFromIconFileAsync (text, index , 32 ) };
96
+ if (swBitmap == nullptr )
97
+ {
98
+ co_return ;
99
+ }
100
+ co_await winrt::resume_foreground (Dispatcher ());
101
+ winrt::Windows::UI::Xaml::Media::Imaging::SoftwareBitmapSource bitmapSource{};
102
+ co_await bitmapSource.SetBitmapAsync (swBitmap);
103
+ co_await winrt::resume_foreground (Dispatcher ());
104
+
105
+ winrt::Microsoft::UI::Xaml::Controls::ImageIconSource imageIconSource{};
106
+ imageIconSource.ImageSource (bitmapSource);
107
+ winrt::Microsoft::UI::Xaml::Controls::ImageIcon icon{};
108
+ icon.Source (bitmapSource);
109
+ icon.Width (32 );
110
+ icon.Height (32 );
111
+ InProcContent ().Children ().Append (icon);
37
112
}
38
113
39
114
void MyPage::CloseClicked (const IInspectable& /* sender*/ ,
0 commit comments