Skip to content

Commit 1dd6e99

Browse files
committed
prototype
1 parent 11bfc73 commit 1dd6e99

File tree

4 files changed

+96
-21
lines changed

4 files changed

+96
-21
lines changed

scratch/ScratchIslandApp/SampleApp/MyPage.cpp

+76-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88
#include "MyPage.g.cpp"
99
#include "..\..\..\src\cascadia\UnitTests_Control\MockControlSettings.h"
1010
#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>
1215
using namespace std::chrono_literals;
1316
using namespace winrt::Microsoft::Terminal;
1417

@@ -19,6 +22,9 @@ namespace winrt
1922
using IInspectable = Windows::Foundation::IInspectable;
2023
}
2124

25+
using namespace winrt::Windows::Graphics::Imaging;
26+
using namespace winrt::Windows::Storage::Streams;
27+
2228
namespace winrt::SampleApp::implementation
2329
{
2430
MyPage::MyPage()
@@ -30,10 +36,79 @@ namespace winrt::SampleApp::implementation
3036
{
3137
}
3238

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+
3383
winrt::fire_and_forget MyPage::CreateClicked(const IInspectable& sender,
3484
const WUX::Input::TappedRoutedEventArgs& eventArgs)
3585
{
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+
3694
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);
37112
}
38113

39114
void MyPage::CloseClicked(const IInspectable& /*sender*/,

scratch/ScratchIslandApp/SampleApp/MyPage.xaml

+9-20
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,12 @@
2222
<StackPanel Orientation="Horizontal">
2323
<TextBox x:Name="GuidInput"
2424
Width="400"
25-
PlaceholderText="{}{guid here}" />
25+
PlaceholderText="path here" />
26+
<mux:NumberBox x:Name="IconIndex" />
2627
<Button x:Name="CreateOutOfProcControl"
27-
Grid.Row="0"
2828
Tapped="CreateClicked">
2929
Create
3030
</Button>
31-
<Button x:Name="CloseOutOfProcControl"
32-
Grid.Row="0"
33-
Margin="4,0,0,0"
34-
Tapped="CloseClicked">
35-
Close
36-
</Button>
37-
<Button x:Name="KillOutOfProcControl"
38-
Grid.Row="0"
39-
Margin="4,0,0,0"
40-
Tapped="KillClicked">
41-
Kill
42-
</Button>
4331

4432
</StackPanel>
4533

@@ -53,12 +41,13 @@
5341
<ColumnDefinition Width="*" />
5442
</Grid.ColumnDefinitions>
5543

56-
<Grid x:Name="InProcContent"
57-
Grid.Column="0"
58-
Padding="16"
59-
HorizontalAlignment="Stretch"
60-
VerticalAlignment="Stretch"
61-
Background="#ff0000" />
44+
<StackPanel x:Name="InProcContent"
45+
Grid.Column="0"
46+
Padding="16"
47+
HorizontalAlignment="Stretch"
48+
VerticalAlignment="Stretch"
49+
Background="#ff0000"
50+
Orientation="Vertical" />
6251

6352
<Grid Grid.Column="1"
6453
HorizontalAlignment="Stretch"

scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj

+7
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@
8383
</Reference>
8484
</ItemGroup>
8585

86+
<!-- ====================== Compiler & Linker Flags ===================== -->
87+
88+
<ItemDefinitionGroup>
89+
<Link>
90+
<AdditionalDependencies>$(OpenConsoleCommonOutDir)\ConTypes.lib;WindowsApp.lib;shell32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
91+
</Link>
92+
</ItemDefinitionGroup>
8693

8794
<Import Project="$(OpenConsoleDir)packages\Microsoft.UI.Xaml.2.7.1\build\native\Microsoft.UI.Xaml.targets" Condition="Exists('$(OpenConsoleDir)packages\Microsoft.UI.Xaml.2.7.1\build\native\Microsoft.UI.Xaml.targets')" />
8895
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">

scratch/ScratchIslandApp/SampleApp/pch.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <winrt/Windows.Foundation.Collections.h>
3434
#include <winrt/Windows.Foundation.Metadata.h>
3535
#include <winrt/Windows.Graphics.Display.h>
36+
#include <winrt/Windows.Graphics.Imaging.h>
37+
// #include <winrt/Windows.Graphics.Imaging.Interop.h>
3638
#include <winrt/windows.ui.core.h>
3739
#include <winrt/Windows.ui.input.h>
3840
#include <winrt/Windows.UI.Text.h>
@@ -41,10 +43,12 @@
4143
#include <winrt/Windows.UI.Xaml.Data.h>
4244
#include <winrt/Windows.ui.xaml.media.h>
4345
#include <winrt/Windows.UI.Xaml.Media.Animation.h>
46+
#include <winrt/Windows.UI.Xaml.Media.Imaging.h>
4447
#include <winrt/Windows.ui.xaml.input.h>
4548
#include <winrt/Windows.UI.Xaml.Hosting.h>
4649
#include "winrt/Windows.UI.Xaml.Markup.h"
4750
#include "winrt/Windows.UI.ViewManagement.h"
51+
#include "winrt/Windows.Storage.Streams.h"
4852

4953
#include <winrt/Microsoft.Toolkit.Win32.UI.XamlHost.h>
5054
#include <winrt/Microsoft.UI.Xaml.Controls.h>

0 commit comments

Comments
 (0)