Skip to content

Commit

Permalink
prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
zadjii-msft committed Jul 18, 2022
1 parent 11bfc73 commit 1dd6e99
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 21 deletions.
77 changes: 76 additions & 1 deletion scratch/ScratchIslandApp/SampleApp/MyPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@
#include "MyPage.g.cpp"
#include "..\..\..\src\cascadia\UnitTests_Control\MockControlSettings.h"
#include "..\..\..\src\types\inc\utils.hpp"

#include <Shlobj.h>
#include <Shlobj_core.h>
#include <wincodec.h>
#include <Windows.Graphics.Imaging.Interop.h>
using namespace std::chrono_literals;
using namespace winrt::Microsoft::Terminal;

Expand All @@ -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()
Expand All @@ -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<IWICBitmap> 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::Windows::Graphics::Imaging::SoftwareBitmap>(
winrt::create_instance<ISoftwareBitmapNativeFactory>(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<IWICImagingFactory> 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<int>(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*/,
Expand Down
29 changes: 9 additions & 20 deletions scratch/ScratchIslandApp/SampleApp/MyPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,12 @@
<StackPanel Orientation="Horizontal">
<TextBox x:Name="GuidInput"
Width="400"
PlaceholderText="{}{guid here}" />
PlaceholderText="path here" />
<mux:NumberBox x:Name="IconIndex" />
<Button x:Name="CreateOutOfProcControl"
Grid.Row="0"
Tapped="CreateClicked">
Create
</Button>
<Button x:Name="CloseOutOfProcControl"
Grid.Row="0"
Margin="4,0,0,0"
Tapped="CloseClicked">
Close
</Button>
<Button x:Name="KillOutOfProcControl"
Grid.Row="0"
Margin="4,0,0,0"
Tapped="KillClicked">
Kill
</Button>

</StackPanel>

Expand All @@ -53,12 +41,13 @@
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>

<Grid x:Name="InProcContent"
Grid.Column="0"
Padding="16"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="#ff0000" />
<StackPanel x:Name="InProcContent"
Grid.Column="0"
Padding="16"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Background="#ff0000"
Orientation="Vertical" />

<Grid Grid.Column="1"
HorizontalAlignment="Stretch"
Expand Down
7 changes: 7 additions & 0 deletions scratch/ScratchIslandApp/SampleApp/dll/SampleApp.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
</Reference>
</ItemGroup>

<!-- ====================== Compiler & Linker Flags ===================== -->

<ItemDefinitionGroup>
<Link>
<AdditionalDependencies>$(OpenConsoleCommonOutDir)\ConTypes.lib;WindowsApp.lib;shell32.lib;user32.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>

<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')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
Expand Down
4 changes: 4 additions & 0 deletions scratch/ScratchIslandApp/SampleApp/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Foundation.Metadata.h>
#include <winrt/Windows.Graphics.Display.h>
#include <winrt/Windows.Graphics.Imaging.h>
// #include <winrt/Windows.Graphics.Imaging.Interop.h>
#include <winrt/windows.ui.core.h>
#include <winrt/Windows.ui.input.h>
#include <winrt/Windows.UI.Text.h>
Expand All @@ -41,10 +43,12 @@
#include <winrt/Windows.UI.Xaml.Data.h>
#include <winrt/Windows.ui.xaml.media.h>
#include <winrt/Windows.UI.Xaml.Media.Animation.h>
#include <winrt/Windows.UI.Xaml.Media.Imaging.h>
#include <winrt/Windows.ui.xaml.input.h>
#include <winrt/Windows.UI.Xaml.Hosting.h>
#include "winrt/Windows.UI.Xaml.Markup.h"
#include "winrt/Windows.UI.ViewManagement.h"
#include "winrt/Windows.Storage.Streams.h"

#include <winrt/Microsoft.Toolkit.Win32.UI.XamlHost.h>
#include <winrt/Microsoft.UI.Xaml.Controls.h>
Expand Down

1 comment on commit 1dd6e99

@github-actions

This comment was marked as outdated.

Please sign in to comment.