Skip to content

Commit d8fffa2

Browse files
samanthamsongmeveyemhuang-msftrkarman
authored
Updating windowing samples for 1.1 (#221)
* Updating cs-winui windowing sample to 1.1 preview2 * Updating cpp-winui Windowing sample to 1.1 preview2 * updating cs-wpf windowing sample to 1.1 preview2 * updating cs-winui-unpackaged windowing sample to 1.1 preview2 * modifying wpf csproj * targeting .net6 in cs-winui windowing sample * adding button for AppWindow.ResizeClient(size) * updating cpp-winui windowing sample to include ResizeClient * reformatting wpf sample UI, adding ResizeClient * updating cs-winforms sample to include ResizeClient * [Windowing sample update] Tall title bar code samples (#208) * cs winui tall titlebar support * tall titlebar support for cs winui sample * tall title bar support for cs wpf sample * tall titlebar support for cs winforms sample * updating cs-winui samples, and adding win32 samples * User/emhuang/windowing samples1.1 (#216) * committing progress so far * fixed a bug in the code * Closed handler for ZOrder sample page (#214) Quick fix for handling closing of the secondary windows used to show how the ZOrder APIs work. * made sure index continues from the last number * cleaning up buttons and changing descriptions * cleaning up code for cs winui sample Co-authored-by: Roberth Karman <[email protected]> Co-authored-by: samanthamsong <[email protected]> * changing name of win32 sample * cpp-win32 file name changing * deleting test project * edits to cs-winui sample * changes to cpp-win32 and cpp-winui sample * changing listboxes in cs-winui sample * updating cpp-win32 sample to 1.1 GA * updating cpp-winui sample to 1.1 GA * updating cs-winforms-unpackaged sample to 1.1 GA * updating cs-winui samples to 1.1 GA * updating cs-winforms sample to 1.1 GA * Fix to tall titlebar functionality (#229) * changing icon image file Co-authored-by: Eve <[email protected]> Co-authored-by: Emily Huang <[email protected]> Co-authored-by: Roberth Karman <[email protected]>
1 parent c8e37b5 commit d8fffa2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1143
-127
lines changed

Samples/Mica/cpp-win32/WinAppSDKMicaSample/WinAppSDKMicaSample.vcxproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,4 @@
195195

196196
<!-- Each release of Visual Studio produces larger intermediate files.
197197
To prevent build agents from running out of disk space, clean as we go. -->
198-
<Target Name="CleanIntermediateFiles" AfterTargets="Build">
199-
<RemoveDir Directories="$(IntDir)" />
200-
</Target>
201198
</Project>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31729.503
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Windowing", "Windowing\Windowing.vcxproj", "{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|x64 = Debug|x64
11+
Debug|x86 = Debug|x86
12+
Release|x64 = Release|x64
13+
Release|x86 = Release|x86
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Debug|x64.ActiveCfg = Debug|x64
17+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Debug|x64.Build.0 = Debug|x64
18+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Debug|x86.ActiveCfg = Debug|Win32
19+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Debug|x86.Build.0 = Debug|Win32
20+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Release|x64.ActiveCfg = Release|x64
21+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Release|x64.Build.0 = Release|x64
22+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Release|x86.ActiveCfg = Release|Win32
23+
{3CB3ADB7-B8C8-42A1-8F4A-236F77FB40A8}.Release|x86.Build.0 = Release|Win32
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {2630D304-6F1D-4CA3-90FE-A69A11D6B5D2}
30+
EndGlobalSection
31+
EndGlobal
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include <Windows.UI.Composition.Interop.h>
6+
7+
#include <winrt/Windows.UI.Composition.Desktop.h>
8+
9+
// DesktopWindow class defines a window, extended by MyAppWindow class
10+
template <typename T>
11+
struct DesktopWindow
12+
{
13+
using base_type = DesktopWindow<T>;
14+
HWND m_window = nullptr;
15+
16+
static T* GetThisFromHandle(const HWND window) noexcept
17+
{
18+
return reinterpret_cast<T*>(GetWindowLongPtr(window, GWLP_USERDATA));
19+
}
20+
21+
static LRESULT __stdcall WndProc(const HWND window, const UINT message, const WPARAM wparam, const LPARAM lparam) noexcept
22+
{
23+
WINRT_ASSERT(window);
24+
25+
if (WM_NCCREATE == message)
26+
{
27+
auto cs = reinterpret_cast<CREATESTRUCT*>(lparam);
28+
T* that = static_cast<T*>(cs->lpCreateParams);
29+
WINRT_ASSERT(that);
30+
WINRT_ASSERT(!that->m_window);
31+
that->m_window = window;
32+
SetWindowLongPtr(window, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(that));
33+
}
34+
else if (T* that = GetThisFromHandle(window))
35+
{
36+
return that->MessageHandler(message, wparam, lparam);
37+
}
38+
39+
return DefWindowProc(window, message, wparam, lparam);
40+
}
41+
42+
LRESULT MessageHandler(const UINT message, const WPARAM wparam, const LPARAM lparam) noexcept
43+
{
44+
if (WM_DESTROY == message)
45+
{
46+
PostQuitMessage(0);
47+
return 0;
48+
}
49+
50+
return DefWindowProc(m_window, message, wparam, lparam);
51+
}
52+
53+
winrt::Windows::UI::Composition::Desktop::DesktopWindowTarget CreateWindowTarget(const winrt::Windows::UI::Composition::Compositor& compositor)
54+
{
55+
namespace abi = ABI::Windows::UI::Composition::Desktop;
56+
57+
auto interop = compositor.as<abi::ICompositorDesktopInterop>();
58+
winrt::Windows::UI::Composition::Desktop::DesktopWindowTarget target{ nullptr };
59+
winrt::check_hresult(interop->CreateDesktopWindowTarget(m_window, true, reinterpret_cast<abi::IDesktopWindowTarget**>(winrt::put_abi(target))));
60+
return target;
61+
}
62+
};
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#include "pch.h"
4+
#include "MyAppWindow.h"
5+
using namespace winrt::Microsoft::UI::Windowing;
6+
7+
// global
8+
winrt::Microsoft::UI::Windowing::AppWindow appWindow{ nullptr };
9+
10+
namespace winrt
11+
{
12+
using namespace Microsoft::UI;
13+
using namespace Microsoft::UI::Windowing;
14+
using namespace Microsoft::UI::Composition::SystemBackdrops;
15+
using namespace Windows::UI::Composition;
16+
}
17+
18+
// static
19+
const std::wstring MyAppWindow::ClassName = L"MyAppWindow";
20+
21+
// static
22+
void MyAppWindow::RegisterWindowClass()
23+
{
24+
auto instance = winrt::check_pointer(GetModuleHandleW(nullptr));
25+
WNDCLASSEX wcex = { sizeof(wcex) };
26+
wcex.style = CS_HREDRAW | CS_VREDRAW;
27+
wcex.lpfnWndProc = WndProc;
28+
wcex.hInstance = instance;
29+
wcex.hIcon = LoadIconW(instance, IDI_APPLICATION);
30+
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
31+
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
32+
wcex.lpszClassName = ClassName.c_str();
33+
wcex.hIconSm = LoadIconW(wcex.hInstance, IDI_APPLICATION);
34+
winrt::check_bool(RegisterClassExW(&wcex)); // check if the window class was registered succesfully
35+
}
36+
37+
// Create the AppWindow and enable Mica
38+
MyAppWindow::MyAppWindow(const winrt::Compositor& compositor, const std::wstring& windowTitle)
39+
{
40+
auto instance = winrt::check_pointer(GetModuleHandleW(nullptr));
41+
WINRT_ASSERT(!m_window); // check that window is not initialized
42+
WINRT_VERIFY(
43+
// Window Properties
44+
CreateWindowExW(
45+
WS_EX_COMPOSITED,
46+
ClassName.c_str(), // declared in MyAppWindow.h and defined above
47+
windowTitle.c_str(),
48+
WS_OVERLAPPEDWINDOW,
49+
CW_USEDEFAULT,
50+
CW_USEDEFAULT,
51+
800, 600,
52+
nullptr,
53+
nullptr,
54+
instance,
55+
this
56+
));
57+
58+
//Create AppWindow from existing hWnd)
59+
winrt::WindowId windowId{ (UINT64) m_window };
60+
windowId = winrt::GetWindowIdFromWindow(m_window);
61+
62+
// Get the AppWindow for the WindowId
63+
appWindow = winrt::Microsoft::UI::Windowing::AppWindow::GetFromWindowId(windowId);
64+
65+
//Create Compact Overlay Presenter
66+
winrt::Microsoft::UI::Windowing::AppWindowPresenterKind newPresenterKind = winrt::Microsoft::UI::Windowing::AppWindowPresenterKind::CompactOverlay;
67+
appWindow.SetPresenter(newPresenterKind);
68+
69+
appWindow.Create();
70+
appWindow.Show();
71+
72+
// The Mica controller needs to set a target with a root to recognize the visual base layer
73+
m_target = CreateWindowTarget(compositor);
74+
75+
// Need to set a root before we can enable Mica.
76+
m_target.Root(compositor.CreateContainerVisual());
77+
78+
m_micaController = winrt::MicaController();
79+
m_isMicaSupported = m_micaController.SetTarget(winrt::Microsoft::UI::WindowId{ reinterpret_cast<uint64_t>(m_window) }, m_target);
80+
}
81+
82+
// Don't forget to free memory on WM_DESTROY!
83+
LRESULT MyAppWindow::MessageHandler(const UINT message, const WPARAM wparam, const LPARAM lparam) noexcept
84+
{
85+
if (WM_DESTROY == message)
86+
{
87+
m_micaController = nullptr;
88+
}
89+
90+
return base_type::MessageHandler(message, wparam, lparam);
91+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include "DesktopWindow.h"
6+
7+
#include <winrt/Microsoft.UI.Composition.SystemBackdrops.h>
8+
9+
struct MyAppWindow : DesktopWindow<MyAppWindow>
10+
{
11+
static const std::wstring ClassName;
12+
static void RegisterWindowClass();
13+
14+
MyAppWindow(const winrt::Windows::UI::Composition::Compositor& compositor, const std::wstring& windowTitle);
15+
16+
LRESULT MessageHandler(const UINT message, const WPARAM wparam, const LPARAM lparam) noexcept;
17+
18+
winrt::Windows::UI::Composition::Visual Root() { return m_target.Root(); }
19+
void Root(const winrt::Windows::UI::Composition::Visual& visual) { m_target.Root(visual); }
20+
21+
private:
22+
winrt::Windows::UI::Composition::CompositionTarget m_target{ nullptr };
23+
winrt::Microsoft::UI::Composition::SystemBackdrops::MicaController m_micaController{ nullptr };
24+
bool m_isMicaSupported{ false };
25+
};
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#pragma once
4+
5+
#include <DispatcherQueue.h>
6+
#include <MddBootstrap.h>
7+
8+
#include <winrt/Windows.System.h>
9+
10+
namespace Utilities
11+
{
12+
inline auto CreateDispatcherQueueControllerForCurrentThread()
13+
{
14+
namespace abi = ABI::Windows::System;
15+
16+
DispatcherQueueOptions options
17+
{
18+
sizeof(DispatcherQueueOptions),
19+
DQTYPE_THREAD_CURRENT,
20+
DQTAT_COM_NONE
21+
};
22+
23+
winrt::Windows::System::DispatcherQueueController controller{ nullptr };
24+
winrt::check_hresult(CreateDispatcherQueueController(options, reinterpret_cast<abi::IDispatcherQueueController**>(winrt::put_abi(controller))));
25+
return controller;
26+
}
27+
28+
struct WindowsAppSDKBootstrapperContext
29+
{
30+
WindowsAppSDKBootstrapperContext()
31+
{
32+
// Take a dependency on Windows App SDK 1.1
33+
// If using version 1.1 Preview,
34+
// replace with majorMinorVersion{ 0x00010001 } and versionTag{ L"preview" }.
35+
const UINT32 majorMinorVersion{ 0x00010000 };
36+
PCWSTR versionTag{ L"" };
37+
const PACKAGE_VERSION minVersion{};
38+
39+
winrt::check_hresult(MddBootstrapInitialize(majorMinorVersion, versionTag, minVersion));
40+
}
41+
42+
~WindowsAppSDKBootstrapperContext()
43+
{
44+
// Release the DDLM and clean up.
45+
MddBootstrapShutdown();
46+
}
47+
};
48+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
#include "pch.h"
4+
5+
#include "MyAppWindow.h"
6+
#include "Utilities.h"
7+
8+
namespace winrt
9+
{
10+
using namespace Windows::UI::Composition;
11+
}
12+
13+
int __stdcall WinMain(_In_ HINSTANCE, _In_opt_ HINSTANCE, _In_ PSTR, _In_ int)
14+
{
15+
// Initialize WinRt Instance
16+
winrt::init_apartment();
17+
18+
// Enable referencing the WindowsAppSDK from an unpackaged app.
19+
// Remember to have a matching Microsoft.WindowsAppRuntime.Redist installed.
20+
// https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/deploy-unpackaged-apps
21+
// There are two options to initialize the bootstrapper, use the utility function here
22+
// Utilities::WindowsAppSDKBootstrapperContext sdkContext;
23+
// or add the following tags to your .vcx project file, as done in this sample:
24+
// <WindowsPackageType>None</WindowsPackageType>
25+
// <LogSDKReferenceResolutionErrorsAsWarnings>true< / LogSDKReferenceResolutionErrorsAsWarnings>
26+
//
27+
28+
// Register Window Class before making the window
29+
MyAppWindow::RegisterWindowClass();
30+
31+
// Mica requires a compositor, which also requires a dispatcher queue
32+
auto controller = Utilities::CreateDispatcherQueueControllerForCurrentThread();
33+
34+
auto compositor = winrt::Compositor();
35+
36+
// Here we initialize the main window and set the title
37+
auto window = MyAppWindow(compositor, L"Hello, AppWindow");
38+
39+
// Message pump.
40+
MSG msg;
41+
while (GetMessageW(&msg, nullptr, 0, 0))
42+
{
43+
TranslateMessage(&msg);
44+
DispatchMessageW(&msg);
45+
}
46+
47+
return static_cast<int>(msg.wParam);
48+
}

0 commit comments

Comments
 (0)