├── .gitattributes
├── .gitignore
├── .gitmodules
├── Directory.Build.props
├── ExtendIntoTitlebar.sln
├── ExtendIntoTitlebar
├── ExtendIntoTitlebar.vcxproj
├── ExtendIntoTitlebar.vcxproj.filters
├── Win32XamlApp.cpp
├── packages.config
├── pch.cpp
└── pch.h
├── Minimal
├── RootInterop.h
├── Win32XamlApp.cpp
├── Win32XamlApp.vcxproj
├── Win32XamlApp.vcxproj.filters
├── XamlApplication.h
├── XamlApplication.idl
├── packages.config
├── pch.cpp
└── pch.h
├── MultiWindow
├── App.xaml
├── MultiWindowXamlApp.cpp
├── MultiWindowXamlApp.vcxproj
├── MultiWindowXamlApp.vcxproj.filters
├── packages.config
├── pch.cpp
├── pch.h
├── resource.h
└── resource.rc
├── MultiWindowXamlApp.sln
├── MultiXamlAndWindow
├── MultiXamlAndWindowApp.cpp
├── MultiXamlAndWindowApp.vcxproj
├── MultiXamlAndWindowApp.vcxproj.filters
├── packages.config
├── pch.cpp
└── pch.h
├── MultiXamlAndWindowApp.sln
├── NuGet.Config
├── Win32XamlApp.png
├── Win32XamlApp.sln
├── Win32XamlAppMultiWindow.png
├── XamlThread-AtProcessRundown.txt
├── XamlThread-Baseline.txt
├── XamlThread-Running.txt
├── inc
├── XamlWin32Helpers.h
└── reference_waiter.h
├── readme.md
└── win32_app.manifest
/.gitattributes:
--------------------------------------------------------------------------------
1 | ###############################################################################
2 | # Set default behavior to automatically normalize line endings.
3 | ###############################################################################
4 | * text=auto
5 |
6 | ###############################################################################
7 | # Set default behavior for command prompt diff.
8 | #
9 | # This is need for earlier builds of msysgit that does not have it on by
10 | # default for csharp files.
11 | # Note: This is only used by command line
12 | ###############################################################################
13 | #*.cs diff=csharp
14 |
15 | ###############################################################################
16 | # Set the merge driver for project and solution files
17 | #
18 | # Merging from the command prompt will add diff markers to the files if there
19 | # are conflicts (Merging from VS is not affected by the settings below, in VS
20 | # the diff markers are never inserted). Diff markers may cause the following
21 | # file extensions to fail to load in VS. An alternative would be to treat
22 | # these files as binary and thus will always conflict and require user
23 | # intervention with every merge. To do so, just uncomment the entries below
24 | ###############################################################################
25 | #*.sln merge=binary
26 | #*.csproj merge=binary
27 | #*.vbproj merge=binary
28 | #*.vcxproj merge=binary
29 | #*.vcproj merge=binary
30 | #*.dbproj merge=binary
31 | #*.fsproj merge=binary
32 | #*.lsproj merge=binary
33 | #*.wixproj merge=binary
34 | #*.modelproj merge=binary
35 | #*.sqlproj merge=binary
36 | #*.wwaproj merge=binary
37 |
38 | ###############################################################################
39 | # behavior for image files
40 | #
41 | # image files are treated as binary by default.
42 | ###############################################################################
43 | #*.jpg binary
44 | #*.png binary
45 | #*.gif binary
46 |
47 | ###############################################################################
48 | # diff behavior for common document formats
49 | #
50 | # Convert binary document formats to text before diffing them. This feature
51 | # is only available from the command line. Turn it on by uncommenting the
52 | # entries below.
53 | ###############################################################################
54 | #*.doc diff=astextplain
55 | #*.DOC diff=astextplain
56 | #*.docx diff=astextplain
57 | #*.DOCX diff=astextplain
58 | #*.dot diff=astextplain
59 | #*.DOT diff=astextplain
60 | #*.pdf diff=astextplain
61 | #*.PDF diff=astextplain
62 | #*.rtf diff=astextplain
63 | #*.RTF diff=astextplain
64 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 | ##
4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
5 |
6 | # User-specific files
7 | *.rsuser
8 | *.suo
9 | *.user
10 | *.userosscache
11 | *.sln.docstates
12 |
13 | # User-specific files (MonoDevelop/Xamarin Studio)
14 | *.userprefs
15 |
16 | # Mono auto generated files
17 | mono_crash.*
18 |
19 | # Build results
20 | [Dd]ebug/
21 | [Dd]ebugPublic/
22 | [Rr]elease/
23 | [Rr]eleases/
24 | x64/
25 | x86/
26 | [Ww][Ii][Nn]32/
27 | [Aa][Rr][Mm]/
28 | [Aa][Rr][Mm]64/
29 | bld/
30 | [Bb]in/
31 | [Oo]bj/
32 | [Oo]ut/
33 | [Ll]og/
34 | [Ll]ogs/
35 |
36 | # Visual Studio 2015/2017 cache/options directory
37 | .vs/
38 | # Uncomment if you have tasks that create the project's static files in wwwroot
39 | #wwwroot/
40 |
41 | # Visual Studio 2017 auto generated files
42 | Generated\ Files/
43 |
44 | # MSTest test Results
45 | [Tt]est[Rr]esult*/
46 | [Bb]uild[Ll]og.*
47 |
48 | # NUnit
49 | *.VisualState.xml
50 | TestResult.xml
51 | nunit-*.xml
52 |
53 | # Build Results of an ATL Project
54 | [Dd]ebugPS/
55 | [Rr]eleasePS/
56 | dlldata.c
57 |
58 | # Benchmark Results
59 | BenchmarkDotNet.Artifacts/
60 |
61 | # .NET Core
62 | project.lock.json
63 | project.fragment.lock.json
64 | artifacts/
65 |
66 | # ASP.NET Scaffolding
67 | ScaffoldingReadMe.txt
68 |
69 | # StyleCop
70 | StyleCopReport.xml
71 |
72 | # Files built by Visual Studio
73 | *_i.c
74 | *_p.c
75 | *_h.h
76 | *.ilk
77 | *.meta
78 | *.obj
79 | *.iobj
80 | *.pch
81 | *.pdb
82 | *.ipdb
83 | *.pgc
84 | *.pgd
85 | *.rsp
86 | *.sbr
87 | *.tlb
88 | *.tli
89 | *.tlh
90 | *.tmp
91 | *.tmp_proj
92 | *_wpftmp.csproj
93 | *.log
94 | *.vspscc
95 | *.vssscc
96 | .builds
97 | *.pidb
98 | *.svclog
99 | *.scc
100 |
101 | # Chutzpah Test files
102 | _Chutzpah*
103 |
104 | # Visual C++ cache files
105 | ipch/
106 | *.aps
107 | *.ncb
108 | *.opendb
109 | *.opensdf
110 | *.sdf
111 | *.cachefile
112 | *.VC.db
113 | *.VC.VC.opendb
114 |
115 | # Visual Studio profiler
116 | *.psess
117 | *.vsp
118 | *.vspx
119 | *.sap
120 |
121 | # Visual Studio Trace Files
122 | *.e2e
123 |
124 | # TFS 2012 Local Workspace
125 | $tf/
126 |
127 | # Guidance Automation Toolkit
128 | *.gpState
129 |
130 | # ReSharper is a .NET coding add-in
131 | _ReSharper*/
132 | *.[Rr]e[Ss]harper
133 | *.DotSettings.user
134 |
135 | # TeamCity is a build add-in
136 | _TeamCity*
137 |
138 | # DotCover is a Code Coverage Tool
139 | *.dotCover
140 |
141 | # AxoCover is a Code Coverage Tool
142 | .axoCover/*
143 | !.axoCover/settings.json
144 |
145 | # Coverlet is a free, cross platform Code Coverage Tool
146 | coverage*.json
147 | coverage*.xml
148 | coverage*.info
149 |
150 | # Visual Studio code coverage results
151 | *.coverage
152 | *.coveragexml
153 |
154 | # NCrunch
155 | _NCrunch_*
156 | .*crunch*.local.xml
157 | nCrunchTemp_*
158 |
159 | # MightyMoose
160 | *.mm.*
161 | AutoTest.Net/
162 |
163 | # Web workbench (sass)
164 | .sass-cache/
165 |
166 | # Installshield output folder
167 | [Ee]xpress/
168 |
169 | # DocProject is a documentation generator add-in
170 | DocProject/buildhelp/
171 | DocProject/Help/*.HxT
172 | DocProject/Help/*.HxC
173 | DocProject/Help/*.hhc
174 | DocProject/Help/*.hhk
175 | DocProject/Help/*.hhp
176 | DocProject/Help/Html2
177 | DocProject/Help/html
178 |
179 | # Click-Once directory
180 | publish/
181 |
182 | # Publish Web Output
183 | *.[Pp]ublish.xml
184 | *.azurePubxml
185 | # Note: Comment the next line if you want to checkin your web deploy settings,
186 | # but database connection strings (with potential passwords) will be unencrypted
187 | *.pubxml
188 | *.publishproj
189 |
190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to
191 | # checkin your Azure Web App publish settings, but sensitive information contained
192 | # in these scripts will be unencrypted
193 | PublishScripts/
194 |
195 | # NuGet Packages
196 | *.nupkg
197 | # NuGet Symbol Packages
198 | *.snupkg
199 | # The packages folder can be ignored because of Package Restore
200 | **/[Pp]ackages/*
201 | # except build/, which is used as an MSBuild target.
202 | !**/[Pp]ackages/build/
203 | # Uncomment if necessary however generally it will be regenerated when needed
204 | #!**/[Pp]ackages/repositories.config
205 | # NuGet v3's project.json files produces more ignorable files
206 | *.nuget.props
207 | *.nuget.targets
208 |
209 | # Microsoft Azure Build Output
210 | csx/
211 | *.build.csdef
212 |
213 | # Microsoft Azure Emulator
214 | ecf/
215 | rcf/
216 |
217 | # Windows Store app package directories and files
218 | AppPackages/
219 | BundleArtifacts/
220 | Package.StoreAssociation.xml
221 | _pkginfo.txt
222 | *.appx
223 | *.appxbundle
224 | *.appxupload
225 |
226 | # Visual Studio cache files
227 | # files ending in .cache can be ignored
228 | *.[Cc]ache
229 | # but keep track of directories ending in .cache
230 | !?*.[Cc]ache/
231 |
232 | # Others
233 | ClientBin/
234 | ~$*
235 | *~
236 | *.dbmdl
237 | *.dbproj.schemaview
238 | *.jfm
239 | *.pfx
240 | *.publishsettings
241 | orleans.codegen.cs
242 |
243 | # Including strong name files can present a security risk
244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424)
245 | #*.snk
246 |
247 | # Since there are multiple workflows, uncomment next line to ignore bower_components
248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
249 | #bower_components/
250 |
251 | # RIA/Silverlight projects
252 | Generated_Code/
253 |
254 | # Backup & report files from converting an old project file
255 | # to a newer Visual Studio version. Backup files are not needed,
256 | # because we have git ;-)
257 | _UpgradeReport_Files/
258 | Backup*/
259 | UpgradeLog*.XML
260 | UpgradeLog*.htm
261 | ServiceFabricBackup/
262 | *.rptproj.bak
263 |
264 | # SQL Server files
265 | *.mdf
266 | *.ldf
267 | *.ndf
268 |
269 | # Business Intelligence projects
270 | *.rdl.data
271 | *.bim.layout
272 | *.bim_*.settings
273 | *.rptproj.rsuser
274 | *- [Bb]ackup.rdl
275 | *- [Bb]ackup ([0-9]).rdl
276 | *- [Bb]ackup ([0-9][0-9]).rdl
277 |
278 | # Microsoft Fakes
279 | FakesAssemblies/
280 |
281 | # GhostDoc plugin setting file
282 | *.GhostDoc.xml
283 |
284 | # Node.js Tools for Visual Studio
285 | .ntvs_analysis.dat
286 | node_modules/
287 |
288 | # Visual Studio 6 build log
289 | *.plg
290 |
291 | # Visual Studio 6 workspace options file
292 | *.opt
293 |
294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
295 | *.vbw
296 |
297 | # Visual Studio LightSwitch build output
298 | **/*.HTMLClient/GeneratedArtifacts
299 | **/*.DesktopClient/GeneratedArtifacts
300 | **/*.DesktopClient/ModelManifest.xml
301 | **/*.Server/GeneratedArtifacts
302 | **/*.Server/ModelManifest.xml
303 | _Pvt_Extensions
304 |
305 | # Paket dependency manager
306 | .paket/paket.exe
307 | paket-files/
308 |
309 | # FAKE - F# Make
310 | .fake/
311 |
312 | # CodeRush personal settings
313 | .cr/personal
314 |
315 | # Python Tools for Visual Studio (PTVS)
316 | __pycache__/
317 | *.pyc
318 |
319 | # Cake - Uncomment if you are using it
320 | # tools/**
321 | # !tools/packages.config
322 |
323 | # Tabs Studio
324 | *.tss
325 |
326 | # Telerik's JustMock configuration file
327 | *.jmconfig
328 |
329 | # BizTalk build output
330 | *.btp.cs
331 | *.btm.cs
332 | *.odx.cs
333 | *.xsd.cs
334 |
335 | # OpenCover UI analysis results
336 | OpenCover/
337 |
338 | # Azure Stream Analytics local run output
339 | ASALocalRun/
340 |
341 | # MSBuild Binary and Structured Log
342 | *.binlog
343 |
344 | # NVidia Nsight GPU debugger configuration file
345 | *.nvuser
346 |
347 | # MFractors (Xamarin productivity tool) working folder
348 | .mfractor/
349 |
350 | # Local History for Visual Studio
351 | .localhistory/
352 |
353 | # BeatPulse healthcheck temp database
354 | healthchecksdb
355 |
356 | # Backup folder for Package Reference Convert tool in Visual Studio 2017
357 | MigrationBackup/
358 |
359 | # Ionide (cross platform F# VS Code tools) working folder
360 | .ionide/
361 |
362 | # Fody - auto-generated XML schema
363 | FodyWeavers.xsd
--------------------------------------------------------------------------------
/.gitmodules:
--------------------------------------------------------------------------------
1 | [submodule "Win32AppHelpers"]
2 | path = Win32AppHelpers
3 | url = https://github.com/ChrisGuzak/Win32AppHelpers.git
4 |
--------------------------------------------------------------------------------
/Directory.Build.props:
--------------------------------------------------------------------------------
1 |
2 |
3 | x64
4 | true
5 | native
6 | false
7 |
8 |
9 |
10 | Debug
11 | Win32
12 |
13 |
14 | Release
15 | Win32
16 |
17 |
18 | Debug
19 | x64
20 |
21 |
22 | Release
23 | x64
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31410.357
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ExtendIntoTitlebar", "ExtendIntoTitlebar\ExtendIntoTitlebar.vcxproj", "{93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}"
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 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Debug|x64.ActiveCfg = Debug|x64
17 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Debug|x64.Build.0 = Debug|x64
18 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Debug|x86.ActiveCfg = Debug|Win32
19 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Debug|x86.Build.0 = Debug|Win32
20 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Release|x64.ActiveCfg = Release|x64
21 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Release|x64.Build.0 = Release|x64
22 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Release|x86.ActiveCfg = Release|Win32
23 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {76893886-8263-43D8-8D3B-BA1D0A63B781}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/ExtendIntoTitlebar.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16.0
6 | Win32Proj
7 | {93dfaade-2251-48e3-8fcb-fe16eb4eb3fe}
8 | ExtendIntoTitlebar
9 | 10.0
10 | Application
11 | v143
12 | Unicode
13 |
14 |
15 |
16 |
17 | true
18 |
19 |
20 | false
21 | true
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | Level3
31 | true
32 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
33 | true
34 | Use
35 | pch.h
36 | stdcpp17
37 |
38 |
39 | Windows
40 | true
41 | gdiplus.lib;%(AdditionalDependencies)
42 |
43 |
44 |
45 |
46 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
47 |
48 |
49 |
50 |
51 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
52 |
53 |
54 |
55 |
56 |
57 | Create
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
72 |
73 |
74 |
75 |
76 |
77 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/ExtendIntoTitlebar.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
7 |
8 |
9 |
10 |
11 |
12 | build
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | build
22 |
23 |
24 |
25 |
26 | build
27 |
28 |
29 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/Win32XamlApp.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 | #include
3 |
4 | // https://microsoft.visualstudio.com/DefaultCollection/WinUI/_git/microsoft-ui-xaml-lift?path=/docs/design-notes/customtitlebar.md&_a=preview
5 | // https://www.osgwiki.com/wiki/Spy++#winspy.2B.2B
6 | // File Explorer
7 | // https://microsoft.visualstudio.com/DefaultCollection/WinUI/_git/microsoft-ui-xaml-lift/pullrequest/6655288
8 |
9 | inline constexpr auto contentText = LR"(
10 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | )";
59 |
60 | struct AppWindow
61 | {
62 | LRESULT Create()
63 | {
64 | m_xamlSource = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
65 |
66 | auto interop = m_xamlSource.as();
67 | THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
68 | THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow));
69 |
70 | // When this fails look in the debug output window, it shows the line and offset
71 | // that has the parsing problem.
72 | auto page = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(contentText).as
73 | ();
74 | auto navView = page.Content().as();
75 |
76 | m_itemInvokedRevoker = navView.ItemInvoked(winrt::auto_revoke, [this](auto&& sender, auto&& args)
77 | {
78 | auto item = args.InvokedItemContainer().as();
79 | auto text = winrt::unbox_value(item.Content());
80 |
81 | auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
82 | tb.Text(L"Hello " + text);
83 |
84 | auto stackPanel = sender.FindName(L"StackPanel1").as();
85 | stackPanel.Children().Append(tb);
86 | });
87 |
88 | m_pointerPressedRevoker = page.PointerPressed(winrt::auto_revoke, [this](auto&& sender, auto&& args)
89 | {
90 | auto pointerID = args.Pointer().PointerId();
91 | auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
92 | tb.Text(L"PointerPressed");
93 |
94 | auto page = sender.as();
95 | auto stackPanel = page.FindName(L"StackPanel1").as();
96 | stackPanel.Children().Append(tb);
97 | });
98 |
99 | m_xamlSource.Content(page);
100 |
101 | return 0;
102 | }
103 |
104 | LRESULT Size(WORD dx, WORD dy)
105 | {
106 | SetWindowPos(m_xamlSourceWindow, nullptr, 0, 0, dx, dy, SWP_SHOWWINDOW);
107 | return 0;
108 | }
109 |
110 | LRESULT Destroy()
111 | {
112 | // Since the xaml rundown is async and requires message dispatching,
113 | // run it down here while the message loop is still running.
114 | m_xamlSource.Close();
115 | PostQuitMessage(0);
116 | return 0;
117 | }
118 |
119 | void Show(int nCmdShow)
120 | {
121 | win32app::create_top_level_window_for_xaml(*this, L"Win32XamlAppWindow", L"Win32 Xaml App");
122 | win32app::enter_simple_message_loop(*this, nCmdShow);
123 | }
124 |
125 | wil::unique_hwnd m_window;
126 | HWND m_xamlSourceWindow{}; // This is owned by m_xamlSource, destroyed when Close() is called.
127 |
128 | winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource{ nullptr };
129 |
130 | winrt::Windows::UI::Xaml::Controls::NavigationView::ItemInvoked_revoker m_itemInvokedRevoker;
131 | winrt::Windows::UI::Xaml::UIElement::PointerPressed_revoker m_pointerPressedRevoker;
132 | };
133 |
134 | int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
135 | {
136 | auto coInit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
137 | std::make_unique()->Show(nCmdShow);
138 | return 0;
139 | }
140 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/pch.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 |
--------------------------------------------------------------------------------
/ExtendIntoTitlebar/pch.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #define WIN32_LEAN_AND_MEAN
7 | #define NOMINMAX
8 | #include
9 | #undef GetCurrentTime
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 |
23 | #include
24 | #include
25 | #include
26 |
27 | #include // COM interop
28 |
--------------------------------------------------------------------------------
/Minimal/RootInterop.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 |
4 | // Proposed addition to Xaml Islands to get the HWNDs in the graph of objects.
5 |
6 | DECLARE_INTERFACE_IID_(IXamlRootInterop, IUnknown, "68601abf-23cc-4017-a946-fb25c574fbab")
7 | {
8 | virtual HRESULT STDMETHODCALLTYPE GetHostingChidWindow(_Out_ HWND * childHwnd) = 0;
9 | virtual HRESULT STDMETHODCALLTYPE GetTopLevelWindow(_Out_ HWND * topLevelHwnd) = 0;
10 | };
11 |
12 | /* Example usage:
13 |
14 | HWND ownerWindow{};
15 | page.as().XamlRoot().as()->GetTopLevelWindow(&ownerWindow);
16 | MessageBoxW(ownerWindow, L"Hello from XamlControl!", L"XamlControl", MB_OK);
17 | */
18 |
19 |
20 |
--------------------------------------------------------------------------------
/Minimal/Win32XamlApp.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 | #include
3 | #include "XamlApplication.h"
4 |
5 | inline constexpr auto contentText = LR"(
6 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 | )";
55 |
56 | struct AppWindow
57 | {
58 | LRESULT Create()
59 | {
60 | // TODO: Proof of concept test of XamlApplication, work in progress.
61 | // auto winuiIXMP = winrt::Microsoft::UI::Xaml::XamlTypeInfo::XamlControlsXamlMetaDataProvider();
62 | // auto markupIXMP = winrt::AppMarkup::XamlMetaDataProvider();
63 | // auto app = winrt::make_xaml_application(winuiIXMP, markupIXMP);
64 |
65 | auto app = winrt::make_xaml_application();
66 |
67 | m_xamlSource = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
68 |
69 | auto interop = m_xamlSource.as();
70 | THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
71 | THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow));
72 |
73 | // When this fails look in the debug output window, it shows the line and offset
74 | // that has the parsing problem.
75 | auto page = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(contentText).as
76 | ();
77 | auto navView = page.Content().as();
78 |
79 | m_itemInvokedRevoker = navView.ItemInvoked(winrt::auto_revoke, [this](auto&& sender, auto&& args)
80 | {
81 | auto item = args.InvokedItemContainer().as();
82 | auto text = winrt::unbox_value(item.Content());
83 |
84 | auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
85 | tb.Text(L"Hello " + text);
86 |
87 | auto stackPanel = sender.FindName(L"StackPanel1").as();
88 | stackPanel.Children().Append(tb);
89 | });
90 |
91 | m_pointerPressedRevoker = page.PointerPressed(winrt::auto_revoke, [this](auto&& sender, auto&& args)
92 | {
93 | auto pointerID = args.Pointer().PointerId();
94 | auto tb = winrt::Windows::UI::Xaml::Controls::TextBlock();
95 | tb.Text(L"PointerPressed");
96 |
97 | auto page = sender.as();
98 | auto stackPanel = page.FindName(L"StackPanel1").as();
99 | stackPanel.Children().Append(tb);
100 | });
101 |
102 | m_xamlSource.Content(page);
103 |
104 | return 0;
105 | }
106 |
107 | LRESULT Size(WORD dx, WORD dy)
108 | {
109 | SetWindowPos(m_xamlSourceWindow, nullptr, 0, 0, dx, dy, SWP_SHOWWINDOW);
110 | return 0;
111 | }
112 |
113 | LRESULT Destroy()
114 | {
115 | winrt::Windows::UI::Xaml::Application::Current().as().Close();
116 |
117 | // Since the xaml rundown is async and requires message dispatching,
118 | // run it down here while the message loop is still running.
119 | m_xamlSource.Close();
120 | PostQuitMessage(0);
121 | return 0;
122 | }
123 |
124 | void Show(int nCmdShow)
125 | {
126 | win32app::create_top_level_window_for_xaml(*this, L"Win32XamlAppWindow", L"Win32 Xaml App");
127 | win32app::enter_simple_message_loop(*this, nCmdShow);
128 | }
129 |
130 | wil::unique_hwnd m_window;
131 | HWND m_xamlSourceWindow{}; // This is owned by m_xamlSource, destroyed when Close() is called.
132 |
133 | winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource{ nullptr };
134 |
135 | winrt::Windows::UI::Xaml::Controls::NavigationView::ItemInvoked_revoker m_itemInvokedRevoker;
136 | winrt::Windows::UI::Xaml::UIElement::PointerPressed_revoker m_pointerPressedRevoker;
137 | };
138 |
139 | int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
140 | {
141 | auto coInit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
142 | std::make_unique()->Show(nCmdShow);
143 | return 0;
144 | }
145 |
--------------------------------------------------------------------------------
/Minimal/Win32XamlApp.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16.0
6 | Win32Proj
7 | {46e37ff4-e2ef-40f9-8479-acefed56213b}
8 | Win32XamlApp
9 | 10.0
10 | Application
11 | v143
12 | Unicode
13 |
14 |
15 |
16 |
17 | true
18 |
19 |
20 | false
21 | true
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | true
31 | Use
32 | pch.h
33 | stdcpp17
34 |
35 |
36 | Windows
37 | true
38 | gdiplus.lib;%(AdditionalDependencies)
39 |
40 |
41 |
42 |
43 | true
44 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
45 |
46 |
47 |
48 |
49 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
50 |
51 |
52 |
53 |
54 | Create
55 |
56 |
57 |
58 |
59 |
60 | XamlApplication.idl
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
75 |
76 |
77 |
78 |
79 |
80 |
--------------------------------------------------------------------------------
/Minimal/Win32XamlApp.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
7 |
8 |
9 |
10 |
11 |
12 | build
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 | build
22 |
23 |
24 |
25 |
26 | build
27 |
28 |
29 |
30 | build
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/Minimal/XamlApplication.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include "pch.h"
4 | #include "WinUIHelpers.XamlApplication.g.h"
5 |
6 | #include
7 |
8 | // Minimal Xaml Application Object needed to host WinUI Controls and provide access to the resources and metadata
9 | // provider (IXMP).
10 |
11 | // Example usage:
12 | //
13 | // void Create() {
14 | // ...
15 | // auto providers = winrt::single_threaded_vector();
16 | // providers.Append(provider1);
17 | // providers.Append(provider2);
18 | // auto app = winrt::make(providers);
19 | //
20 | // void Destroy() {
21 | // ...
22 | // winrt::Windows::UI::Xaml::Application::Current().as().Close();
23 |
24 | namespace winrt::WinUIHelpers::implementation
25 | {
26 | struct XamlApplication : XamlApplicationT
27 | {
28 | XamlApplication() = default;
29 |
30 | XamlApplication(std::initializer_list providers)
31 | {
32 | for (auto&& provider : providers)
33 | {
34 | m_providers.Append(provider);
35 | }
36 | }
37 |
38 | ~XamlApplication()
39 | {
40 | Close();
41 | }
42 |
43 | void InitializeComponent() // C++ WinRT 2 phase construction
44 | {
45 | m_windowsXamlManager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
46 | }
47 |
48 | winrt::Windows::Foundation::Collections::IVector MetadataProviders()
49 | {
50 | return m_providers;
51 | }
52 |
53 | winrt::Windows::Foundation::IClosable WindowsXamlManager()
54 | {
55 | return m_windowsXamlManager;
56 | }
57 |
58 | // ICloseable
59 | void Close()
60 | {
61 | if (b_closed) return;
62 |
63 | b_closed = true;
64 |
65 | m_windowsXamlManager.Close();
66 | m_providers.Clear();
67 | m_windowsXamlManager = nullptr;
68 |
69 | // Toolkit has a message loop here.
70 | }
71 |
72 | // IXamlMetadataProvider
73 | winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(winrt::Windows::UI::Xaml::Interop::TypeName const& type)
74 | {
75 | for (const auto& provider : m_providers)
76 | {
77 | if (const auto xamlType = provider.GetXamlType(type))
78 | {
79 | return xamlType;
80 | }
81 | }
82 | return nullptr;
83 | }
84 |
85 | winrt::Windows::UI::Xaml::Markup::IXamlType GetXamlType(hstring const& fullName)
86 | {
87 | for (const auto& provider : m_providers)
88 | {
89 | if (const auto xamlType = provider.GetXamlType(fullName))
90 | {
91 | return xamlType;
92 | }
93 | }
94 | return nullptr;
95 | }
96 |
97 | com_array GetXmlnsDefinitions()
98 | {
99 | std::list definitions;
100 | for (const auto& provider : m_providers)
101 | {
102 | auto defs = provider.GetXmlnsDefinitions();
103 | for (const auto& def : defs)
104 | {
105 | definitions.insert(definitions.begin(), def);
106 | }
107 | }
108 |
109 | return winrt::com_array(definitions.begin(), definitions.end());
110 | }
111 |
112 | private:
113 | winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager m_windowsXamlManager{ nullptr };
114 | winrt::Windows::Foundation::Collections::IVector m_providers
115 | = winrt::single_threaded_vector();
116 | bool b_closed{};
117 | };
118 | }
119 |
120 | namespace winrt
121 | {
122 | template
123 | auto make_xaml_application(TProviders&&... providers)
124 | {
125 | return winrt::make(
126 | std::initializer_list{providers...});
127 | }
128 | }
129 |
130 |
--------------------------------------------------------------------------------
/Minimal/XamlApplication.idl:
--------------------------------------------------------------------------------
1 |
2 | namespace WinUIHelpers
3 | {
4 | runtimeclass XamlApplication : Windows.UI.Xaml.Application,
5 | Windows.UI.Xaml.Markup.IXamlMetadataProvider,
6 | Windows.Foundation.IClosable
7 | {
8 | Windows.Foundation.Collections.IVector MetadataProviders{ get; };
9 | Windows.Foundation.IClosable WindowsXamlManager{ get; };
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/Minimal/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Minimal/pch.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 |
--------------------------------------------------------------------------------
/Minimal/pch.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #define WIN32_LEAN_AND_MEAN
7 | #define NOMINMAX
8 | #include
9 | #undef GetCurrentTime
10 |
11 | #include
12 | #include
13 | #include
14 |
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 | #include
22 |
23 | #include
24 | #include
25 | #include
26 |
27 | #include // COM interop
28 |
--------------------------------------------------------------------------------
/MultiWindow/App.xaml:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChrisGuzak/Win32XamlApp/b1102d9970f2992bea96a8f17c5cf832de682c2a/MultiWindow/App.xaml
--------------------------------------------------------------------------------
/MultiWindow/MultiWindowXamlApp.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 | #include "resource.h"
3 | #include
4 | #include
5 | #include
6 | #include
7 |
8 | struct AppWindow : public std::enable_shared_from_this
9 | {
10 | AppWindow(winrt::Windows::System::DispatcherQueueController queueController, bool rightClickLaunch = false) :
11 | m_queueController(std::move(queueController)),
12 | m_rightClickLaunch(rightClickLaunch),
13 | m_xamlManager(winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread())
14 | {
15 | }
16 |
17 | LRESULT Create()
18 | {
19 | using namespace winrt::Windows::UI::Xaml;
20 | using namespace winrt::Windows::UI::Xaml::Controls;
21 | using namespace winrt::Windows::UI::Xaml::Input;
22 |
23 | // WindowsXamlManager must be used if multiple islands are created on the thread or in the process.
24 | // It must be constructed before the first DesktopWindowXamlSource.
25 | m_xamlSource = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
26 |
27 | auto interop = m_xamlSource.as();
28 | THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
29 | THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow));
30 |
31 | auto xamlString = get_resource_view(L"App.xaml");
32 | auto content = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(xamlString).as();
33 | m_xamlSource.Content(content);
34 |
35 | m_status = content.as().FindName(L"Status").as();
36 |
37 | m_rootChangedRevoker = content.XamlRoot().Changed(winrt::auto_revoke, [this](auto&& sender, auto&& args)
38 | {
39 | auto scale = sender.RasterizationScale();
40 | auto visible = sender.IsHostVisible();
41 |
42 | m_status.Text(std::to_wstring(scale));
43 | });
44 |
45 | m_pointerPressedRevoker = content.PointerPressed(winrt::auto_revoke, [](auto&& sender, auto&& args)
46 | {
47 | const bool isRightClick = args.GetCurrentPoint(sender.as()).Properties().IsRightButtonPressed();
48 |
49 | BroadcastAsync([](auto&& appWindow)
50 | {
51 | appWindow.m_status.Text(L"Broadcast");
52 | });
53 |
54 | StartThreadAsync([isRightClick](auto&& queueController)
55 | {
56 | std::make_shared(std::move(queueController), isRightClick)->Show(SW_SHOWNORMAL);
57 | });
58 | });
59 |
60 | return 0;
61 | }
62 |
63 | LRESULT Size(WORD dx, WORD dy)
64 | {
65 | SetWindowPos(m_xamlSourceWindow, nullptr, 0, 0, dx, dy, SWP_SHOWWINDOW);
66 | return 0;
67 | }
68 |
69 | void Show(int nCmdShow)
70 | {
71 | win32app::create_top_level_window_for_xaml(*this, L"Win32XamlAppWindow", L"Win32 Xaml App");
72 | ShowWindow(m_window.get(), nCmdShow);
73 |
74 | AddWeakRef(this);
75 | m_selfRef = shared_from_this();
76 | m_appRefHolder.emplace(m_appThreadsWaiter.take_reference());
77 | }
78 |
79 | LRESULT Destroy()
80 | {
81 | RemoveWeakRef(this);
82 |
83 | // Close the DesktopWindowXamlSource and the WindowsXamlManager. This will start Xaml's run down since all the
84 | // DWXS/WXM on the thread will now be closed. Xaml's run-down is async, so we need to keep the message loop running.
85 | m_xamlSource.Close();
86 | m_xamlManager.Close();
87 |
88 | [](auto that) -> winrt::fire_and_forget
89 | {
90 | auto delayedRelease = std::move(that->m_selfRef);
91 | co_await that->m_queueController.ShutdownQueueAsync();
92 | }(this);
93 |
94 | m_appRefHolder.reset();
95 |
96 | return 0;
97 | }
98 |
99 | winrt::Windows::System::DispatcherQueue DispatcherQueue() const
100 | {
101 | return m_queueController.DispatcherQueue();
102 | }
103 |
104 | template
105 | static winrt::fire_and_forget StartThreadAsync(Lambda fn)
106 | {
107 | auto queueController = winrt::Windows::System::DispatcherQueueController::CreateOnDedicatedThread();
108 | co_await wil::resume_foreground(queueController.DispatcherQueue());
109 | fn(std::move(queueController));
110 | }
111 |
112 | static std::vector> GetAppWindows()
113 | {
114 | std::vector> result;
115 |
116 | auto lock = std::lock_guard(m_appWindowLock);
117 | result.reserve(m_appWindows.size());
118 | for (auto& weakWindow : m_appWindows)
119 | {
120 | if (auto strong = weakWindow.lock())
121 | {
122 | result.emplace_back(std::move(strong));
123 | }
124 | }
125 | return result;
126 | }
127 |
128 | template
129 | static winrt::fire_and_forget BroadcastAsync(Lambda fn)
130 | {
131 | auto windows = GetAppWindows();
132 | for (const auto& windowRef : windows)
133 | {
134 | co_await wil::resume_foreground(windowRef.get()->DispatcherQueue());
135 | fn(*windowRef.get());
136 | }
137 | }
138 |
139 | static auto take_reference()
140 | {
141 | return m_appThreadsWaiter.take_reference();
142 | }
143 |
144 | static void wait_until_zero()
145 | {
146 | m_appThreadsWaiter.wait_until_zero();
147 | }
148 |
149 | static void AddWeakRef(AppWindow* that)
150 | {
151 | auto lock = std::lock_guard(m_appWindowLock);
152 | AppWindow::m_appWindows.emplace_back(that->weak_from_this());
153 | }
154 |
155 | static void RemoveWeakRef(AppWindow* that)
156 | {
157 | auto lock = std::lock_guard(m_appWindowLock);
158 | m_appWindows.erase(std::find_if(m_appWindows.begin(), m_appWindows.end(), [&](auto&& weakOther)
159 | {
160 | if (auto strong = weakOther.lock())
161 | {
162 | return strong.get() == that;
163 | }
164 | return false;
165 | }));
166 | }
167 |
168 | wil::unique_hwnd m_window;
169 |
170 | private:
171 | bool m_rightClickLaunch{};
172 | HWND m_xamlSourceWindow{}; // This is owned by m_xamlSource, destroyed when Close() is called.
173 |
174 | std::shared_ptr m_selfRef; // needed to extend lifetime during async rundown
175 | std::optional m_appRefHolder; // need to ensure lifetime of the app process
176 |
177 | // This is needed to coordinate the use of Xaml from multiple threads.
178 | winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager m_xamlManager{nullptr};
179 | winrt::Windows::System::DispatcherQueueController m_queueController{ nullptr };
180 | winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource{ nullptr };
181 | winrt::Windows::UI::Xaml::Controls::TextBlock m_status{ nullptr };
182 | winrt::Windows::UI::Xaml::UIElement::PointerPressed_revoker m_pointerPressedRevoker;
183 | winrt::Windows::UI::Xaml::XamlRoot::Changed_revoker m_rootChangedRevoker;
184 |
185 | inline static reference_waiter m_appThreadsWaiter;
186 | inline static std::mutex m_appWindowLock;
187 | inline static std::vector> m_appWindows;
188 | };
189 |
190 | _Use_decl_annotations_ int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
191 | {
192 | auto coInit = wil::CoInitializeEx();
193 |
194 | // Create a Xaml Application object.
195 | // If we don't create an Application manually, Xaml will create one automatically when the first WindowsXamlManager
196 | // or DesktopWindowXamlSource is created. There's an issue with that codepath where after Xaml shuts down on the
197 | // thread where the Application object was automatically created, future calls to WindowsXamlManager.Close() will crash.
198 | // We can avoid this entirely by just creating an Application object ourselves first.
199 | winrt::Windows::UI::Xaml::Application app{};
200 |
201 | AppWindow::StartThreadAsync([nCmdShow, procRef = AppWindow::take_reference()](const auto&& queueController)
202 | {
203 | std::make_shared(std::move(queueController))->Show(nCmdShow);
204 | });
205 |
206 | AppWindow::wait_until_zero();
207 | return 0;
208 | }
209 |
--------------------------------------------------------------------------------
/MultiWindow/MultiWindowXamlApp.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16.0
6 | Win32Proj
7 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}
8 | MultiWindowXamlApp
9 | 10.0
10 | Application
11 | v143
12 | Unicode
13 |
14 |
15 |
16 |
17 | true
18 |
19 |
20 | false
21 | true
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | true
31 | Use
32 | pch.h
33 | stdcpp17
34 |
35 |
36 | Windows
37 | true
38 | gdiplus.lib;%(AdditionalDependencies)
39 |
40 |
41 |
42 |
43 | true
44 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
45 |
46 |
47 |
48 |
49 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
50 |
51 |
52 |
53 |
54 |
55 |
56 | Create
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
79 |
80 |
81 |
82 |
83 |
84 |
--------------------------------------------------------------------------------
/MultiWindow/MultiWindowXamlApp.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
7 |
8 |
9 |
10 |
11 | build
12 |
13 |
14 | build
15 |
16 |
17 |
18 |
19 |
20 | build
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | build
29 |
30 |
31 |
32 |
33 | build
34 |
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/MultiWindow/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/MultiWindow/pch.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 |
--------------------------------------------------------------------------------
/MultiWindow/pch.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #define WIN32_LEAN_AND_MEAN
7 | #define NOMINMAX
8 | #include
9 | #undef GetCurrentTime
10 |
11 | #include
12 |
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #include // COM interop
23 |
24 | #include
25 |
26 | #include
27 |
--------------------------------------------------------------------------------
/MultiWindow/resource.h:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChrisGuzak/Win32XamlApp/b1102d9970f2992bea96a8f17c5cf832de682c2a/MultiWindow/resource.h
--------------------------------------------------------------------------------
/MultiWindow/resource.rc:
--------------------------------------------------------------------------------
1 | App.xaml RCDATA "App.xaml"
2 |
--------------------------------------------------------------------------------
/MultiWindowXamlApp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31410.357
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MultiWindowXamlApp", "MultiWindow\MultiWindowXamlApp.vcxproj", "{5258b1a8-0874-48a5-840b-fec42c31c2e5}"
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 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Debug|x64.ActiveCfg = Debug|x64
17 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Debug|x64.Build.0 = Debug|x64
18 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Debug|x86.ActiveCfg = Debug|Win32
19 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Debug|x86.Build.0 = Debug|Win32
20 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Release|x64.ActiveCfg = Release|x64
21 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Release|x64.Build.0 = Release|x64
22 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Release|x86.ActiveCfg = Release|Win32
23 | {5258b1a8-0874-48a5-840b-fec42c31c2e5}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {76893886-8263-43D8-8D3B-BA1D0A63B781}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/MultiXamlAndWindowApp.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 | #include
3 | #include
4 |
5 | const PCWSTR contentText = LR"(
6 |
10 |
11 |
12 |
13 |
14 | Multi-Window App, Click on a box to create a new window
15 |
16 |
17 | )";
18 |
19 | struct AppWindow
20 | {
21 | LRESULT Create()
22 | {
23 | using namespace winrt::Windows::UI::Xaml;
24 | using namespace winrt::Windows::UI::Xaml::Controls;
25 | using namespace winrt::Windows::UI::Xaml::Input;
26 |
27 | m_xamlSource1 = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
28 |
29 | auto interop = m_xamlSource1.as();
30 | THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
31 | THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow1));
32 |
33 | auto content = winrt::Windows::UI::Xaml::Markup::XamlReader::Load(contentText).as();
34 |
35 | m_xamlSource1.Content(content);
36 |
37 | m_status = content.as().FindName(L"Status").as();
38 |
39 | m_rootChangedRevoker = content.XamlRoot().Changed(winrt::auto_revoke, [this](auto&& sender, auto&& args)
40 | {
41 | auto scale = sender.RasterizationScale();
42 | auto visible = sender.IsHostVisible();
43 |
44 | m_status.Text(std::to_wstring(scale));
45 | });
46 |
47 | m_pointerPressedRevoker = content.PointerPressed(winrt::auto_revoke, [](auto&& sender, auto&& args)
48 | {
49 | const bool isRightClick = args.GetCurrentPoint(sender.as()).Properties().IsRightButtonPressed();
50 |
51 | StartThread([isRightClick]()
52 | {
53 | auto coInit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
54 | std::make_unique()->Show(SW_SHOWNORMAL);
55 | });
56 | });
57 |
58 | m_xamlSource2 = winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource();
59 | interop = m_xamlSource2.as();
60 | THROW_IF_FAILED(interop->AttachToWindow(m_window.get()));
61 | THROW_IF_FAILED(interop->get_WindowHandle(&m_xamlSourceWindow2));
62 |
63 | m_xamlSource2.Content(winrt::Windows::UI::Xaml::Markup::XamlReader::Load(contentText).as());
64 |
65 | return 0;
66 | }
67 |
68 | LRESULT Size(WORD dx, WORD dy)
69 | {
70 | SetWindowPos(m_xamlSourceWindow1, nullptr, 0, 0, dx / 2, dy, SWP_SHOWWINDOW);
71 | SetWindowPos(m_xamlSourceWindow2, nullptr, dx / 2, 0, dx / 2, dy, SWP_SHOWWINDOW);
72 | return 0;
73 | }
74 |
75 | LRESULT Destroy()
76 | {
77 | // Since the xaml rundown is async and requires message dispatching,
78 | // run it down here while the message loop is still running.
79 | // Work around http://task.ms/33900412, to be fixed
80 |
81 | // In this use scenario we need to work around http://task.ms/33900412 (XamlCore leaked)
82 | // Verify Windows.UI.Xaml.dll!DirectUI::WindowsXamlManager::XamlCore::Close is called
83 | // in the debugger.
84 | m_xamlSource1.Close(); // Either order here works, but there is an order related bug
85 | m_xamlSource2.Close();
86 |
87 | PostQuitMessage(0);
88 | return 0;
89 | }
90 |
91 | void Show(int nCmdShow)
92 | {
93 | win32app::create_top_level_window_for_xaml(*this, L"Win32XamlAppWindow", L"Win32 Xaml App");
94 | win32app::enter_simple_message_loop(*this, nCmdShow);
95 | }
96 |
97 | template
98 | static void StartThread(Lambda&& fn)
99 | {
100 | std::unique_lock holdLock(m_lock);
101 | m_threads.emplace_back([fn = std::forward(fn), threadRef = m_appThreadsWaiter.take_reference()]() mutable
102 | {
103 | std::forward(fn)();
104 | });
105 | }
106 |
107 | static void WaitUntilAllWindowsAreClosed()
108 | {
109 | m_appThreadsWaiter.wait_until_zero();
110 |
111 | // now we are safe to iterate over m_threads as it can't change any more
112 | for (auto& thread : AppWindow::m_threads)
113 | {
114 | thread.join();
115 | }
116 | }
117 |
118 | inline static reference_waiter m_appThreadsWaiter;
119 | inline static std::mutex m_lock;
120 | inline static std::vector m_threads;
121 |
122 | wil::unique_hwnd m_window;
123 | HWND m_xamlSourceWindow1{}; // This is owned by m_xamlSource, destroyed when Close() is called.
124 | HWND m_xamlSourceWindow2{}; // This is owned by m_xamlSource, destroyed when Close() is called.
125 |
126 | // This is needed to coordinate the use of Xaml from multiple threads.
127 | winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager m_xamlManager = winrt::Windows::UI::Xaml::Hosting::WindowsXamlManager::InitializeForCurrentThread();
128 |
129 | winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource1{ nullptr };
130 | winrt::Windows::UI::Xaml::Hosting::DesktopWindowXamlSource m_xamlSource2{ nullptr };
131 |
132 | winrt::Windows::UI::Xaml::Controls::TextBlock m_status{ nullptr };
133 |
134 | winrt::Windows::UI::Xaml::UIElement::PointerPressed_revoker m_pointerPressedRevoker;
135 | winrt::Windows::UI::Xaml::XamlRoot::Changed_revoker m_rootChangedRevoker;
136 | };
137 |
138 | int APIENTRY wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR lpCmdLine, int nCmdShow)
139 | {
140 | auto coInit = wil::CoInitializeEx();
141 |
142 | AppWindow::StartThread([nCmdShow]()
143 | {
144 | auto coInit = wil::CoInitializeEx(COINIT_APARTMENTTHREADED);
145 | std::make_unique()->Show(nCmdShow);
146 | });
147 |
148 | AppWindow::WaitUntilAllWindowsAreClosed();
149 | return 0;
150 | }
151 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/MultiXamlAndWindowApp.vcxproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 16.0
6 | Win32Proj
7 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}
8 | MultiXamlAndWindowApp
9 | 10.0
10 | Application
11 | v143
12 | Unicode
13 |
14 |
15 |
16 |
17 | true
18 |
19 |
20 | false
21 | true
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 | true
31 | Use
32 | pch.h
33 | stdcpp17
34 |
35 |
36 | Windows
37 | true
38 | gdiplus.lib;%(AdditionalDependencies)
39 |
40 |
41 |
42 |
43 | true
44 | WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)
45 |
46 |
47 |
48 |
49 | WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)
50 |
51 |
52 |
53 |
54 |
55 | Create
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 | This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
71 |
72 |
73 |
74 |
75 |
76 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/MultiXamlAndWindowApp.vcxproj.filters:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | {93995380-89BD-4b04-88EB-625FBE52EBFB}
6 | h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd
7 |
8 |
9 |
10 |
11 | build
12 |
13 |
14 |
15 |
16 |
17 | build
18 |
19 |
20 |
21 |
22 | build
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/pch.cpp:
--------------------------------------------------------------------------------
1 | #include "pch.h"
2 |
--------------------------------------------------------------------------------
/MultiXamlAndWindow/pch.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 |
3 | #include
4 | #include
5 |
6 | #define WIN32_LEAN_AND_MEAN
7 | #define NOMINMAX
8 | #include
9 | #undef GetCurrentTime
10 |
11 | #include
12 | #include
13 |
14 | #include
15 | #include
16 | #include
17 | #include
18 | #include
19 | #include
20 | #include
21 |
22 | #include // COM interop
23 |
--------------------------------------------------------------------------------
/MultiXamlAndWindowApp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31410.357
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "MultiXamlAndWindowApp", "MultiXamlAndWindow\MultiXamlAndWindowApp.vcxproj", "{32c22b29-4e28-4efb-abcc-3f0956bfceae}"
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 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Debug|x64.ActiveCfg = Debug|x64
17 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Debug|x64.Build.0 = Debug|x64
18 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Debug|x86.ActiveCfg = Debug|Win32
19 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Debug|x86.Build.0 = Debug|Win32
20 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Release|x64.ActiveCfg = Release|x64
21 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Release|x64.Build.0 = Release|x64
22 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Release|x86.ActiveCfg = Release|Win32
23 | {32c22b29-4e28-4efb-abcc-3f0956bfceae}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {76893886-8263-43D8-8D3B-BA1D0A63B781}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/NuGet.Config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/Win32XamlApp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChrisGuzak/Win32XamlApp/b1102d9970f2992bea96a8f17c5cf832de682c2a/Win32XamlApp.png
--------------------------------------------------------------------------------
/Win32XamlApp.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio Version 16
4 | VisualStudioVersion = 16.0.31410.357
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Win32XamlApp", "Minimal\Win32XamlApp.vcxproj", "{46E37FF4-E2EF-40F9-8479-ACEFED56213B}"
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 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Debug|x64.ActiveCfg = Debug|x64
17 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Debug|x64.Build.0 = Debug|x64
18 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Debug|x86.ActiveCfg = Debug|Win32
19 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Debug|x86.Build.0 = Debug|Win32
20 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Release|x64.ActiveCfg = Release|x64
21 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Release|x64.Build.0 = Release|x64
22 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Release|x86.ActiveCfg = Release|Win32
23 | {46E37FF4-E2EF-40F9-8479-ACEFED56213B}.Release|x86.Build.0 = Release|Win32
24 | EndGlobalSection
25 | GlobalSection(SolutionProperties) = preSolution
26 | HideSolutionNode = FALSE
27 | EndGlobalSection
28 | GlobalSection(ExtensibilityGlobals) = postSolution
29 | SolutionGuid = {76893886-8263-43D8-8D3B-BA1D0A63B781}
30 | EndGlobalSection
31 | EndGlobal
32 |
--------------------------------------------------------------------------------
/Win32XamlAppMultiWindow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/ChrisGuzak/Win32XamlApp/b1102d9970f2992bea96a8f17c5cf832de682c2a/Win32XamlAppMultiWindow.png
--------------------------------------------------------------------------------
/XamlThread-AtProcessRundown.txt:
--------------------------------------------------------------------------------
1 | Not Flagged > 18744 0 Main Thread Main Thread Win32XamlApp.exe!__scrt_common_main_seh
2 | Not Flagged 18776 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
3 | Not Flagged 15228 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
4 | Not Flagged 11064 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
5 | Not Flagged 10140 0 Worker Thread ntdll.dll thread win32u.dll!00007fff2879abf4
6 | Not Flagged 7392 0 Worker Thread combase.dll thread combase.dll!00007fff29d43fe8
7 | Not Flagged 15700 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
8 | Not Flagged 6708 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
9 | Not Flagged 2188 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
10 | Not Flagged 4600 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d3aa06
11 | Not Flagged 3988 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
12 | Not Flagged 3272 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
13 | Not Flagged 2504 0 Worker Thread SHCore.dll thread win32u.dll!00007fff2879abf4
14 | Not Flagged 19152 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
15 | Not Flagged 10096 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
16 |
--------------------------------------------------------------------------------
/XamlThread-Baseline.txt:
--------------------------------------------------------------------------------
1 | Not Flagged > 18744 0 Main Thread Main Thread Win32XamlApp.exe!wWinMain
2 | Not Flagged 18776 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
3 | Not Flagged 15228 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
4 | Not Flagged 11064 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
5 |
--------------------------------------------------------------------------------
/XamlThread-Running.txt:
--------------------------------------------------------------------------------
1 | Not Flagged > 18744 0 Main Thread Main Thread win32u.dll!00007fff28791414
2 | Not Flagged 18776 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
3 | Not Flagged 15228 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
4 | Not Flagged 11064 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
5 | Not Flagged 10140 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
6 | Not Flagged 7392 0 Worker Thread combase.dll thread combase.dll!00007fff29d43fe8
7 | Not Flagged 15700 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
8 | Not Flagged 10032 0 Worker Thread SHCore.dll thread win32u.dll!00007fff2879abf4
9 | Not Flagged 9560 0 Worker Thread DManip Delegate Thread win32u.dll!00007fff2879abf4
10 | Not Flagged 7516 0 Worker Thread Windows.UI.Xaml.dll!CompositorScheduler::RenderThreadMainStatic Windows.UI.Xaml.dll!CWindowsServices::WaitForObjects
11 | Not Flagged 2124 0 Worker Thread combase.dll thread combase.dll!00007fff29d43fe8
12 | Not Flagged 19348 0 Worker Thread combase.dll thread combase.dll!00007fff29d43fe8
13 | Not Flagged 14232 0 Worker Thread SHCore.dll thread win32u.dll!00007fff2879abf4
14 | Not Flagged 6708 0 Worker Thread ntdll.dll thread ntdll.dll!00007fff2ae068e4
15 | Not Flagged 2188 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
16 | Not Flagged 4600 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d3aa06
17 | Not Flagged 3988 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
18 | Not Flagged 3272 0 Worker Thread Microsoft.VisualStudio.DesignTools.UwpTap.dll thread Microsoft.VisualStudio.DesignTools.UwpTap.dll!00007ffeb7d43cbd
19 |
--------------------------------------------------------------------------------
/inc/XamlWin32Helpers.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 |
6 | inline const wchar_t* MakeIntResourceId(int i)
7 | {
8 | return reinterpret_cast(static_cast(i));
9 | }
10 |
11 | #define XAMLRESOURCE 256 // see resource.h, try to get rid of this by using a standard "file" type.
12 |
13 | template
14 | T LoadXamlResource(HMODULE mod, uint32_t id, const wchar_t* type = MakeIntResourceId(XAMLRESOURCE))
15 | {
16 | auto rc = ::FindResourceW(mod, MakeIntResourceId(id), type);
17 | winrt::check_bool(rc != nullptr);
18 | HGLOBAL rcData = ::LoadResource(mod, rc);
19 | winrt::check_bool(rcData != nullptr);
20 | auto textStart = static_cast(::LockResource(rcData));
21 | auto size = SizeofResource(mod, rc);
22 | winrt::hstring text{ textStart, size / sizeof(*textStart) }; // need a copy to null terminate, see if this can be avoided
23 | return winrt::Windows::UI::Xaml::Markup::XamlReader::Load(text).as();
24 | }
25 |
26 | template
27 | void RegisterWindowClass(const wchar_t* windowClassName)
28 | {
29 | WNDCLASSEXW wcex{ sizeof(wcex) };
30 | wcex.style = CS_HREDRAW | CS_VREDRAW;
31 | wcex.lpfnWndProc = [](HWND window, UINT message, WPARAM wparam, LPARAM lparam) noexcept -> LRESULT
32 | {
33 | if (message == WM_NCCREATE)
34 | {
35 | auto cs = reinterpret_cast(lparam);
36 | auto that = static_cast(cs->lpCreateParams);
37 | that->m_window.reset(window); // take ownership
38 | SetWindowLongPtrW(window, GWLP_USERDATA, reinterpret_cast(that));
39 | }
40 | else if (message == WM_NCDESTROY)
41 | {
42 | SetWindowLongPtrW(window, GWLP_USERDATA, 0);
43 | }
44 | else if (auto that = reinterpret_cast(GetWindowLongPtrW(window, GWLP_USERDATA)))
45 | {
46 | return that->MessageHandler(message, wparam, lparam);
47 | }
48 |
49 | return DefWindowProcW(window, message, wparam, lparam);
50 | };
51 | wcex.hInstance = wil::GetModuleInstanceHandle();
52 | auto imageResMod = LoadLibraryExW(L"imageres.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_AS_DATAFILE);
53 | wcex.hIcon = LoadIconW(imageResMod, reinterpret_cast(5206)); // App Icon
54 | wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
55 | wcex.lpszClassName = windowClassName;
56 |
57 | RegisterClassExW(&wcex);
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/inc/reference_waiter.h:
--------------------------------------------------------------------------------
1 | #pragma once
2 | #include
3 | #include
4 | #include
5 |
6 | struct reference_waiter
7 | {
8 | void wait_until_zero()
9 | {
10 | std::unique_lock lock(m_mutex);
11 | m_cv.wait(lock, [&] { return m_count == 0; });
12 | }
13 |
14 | struct reference_waiter_holder
15 | {
16 | reference_waiter_holder(reference_waiter& s) : m_s(s)
17 | {
18 | add();
19 | }
20 |
21 | ~reference_waiter_holder()
22 | {
23 | subtract();
24 | }
25 |
26 | reference_waiter_holder() = delete;
27 | reference_waiter_holder(const reference_waiter_holder&) = delete;
28 | const reference_waiter_holder& operator=(const reference_waiter_holder&) = delete;
29 |
30 | reference_waiter_holder(reference_waiter_holder&& other) noexcept : m_s(other.m_s)
31 | {
32 | add();
33 | }
34 | reference_waiter_holder& operator=(reference_waiter_holder&&) noexcept
35 | {
36 | add();
37 | return *this;
38 | }
39 |
40 | private:
41 | void add()
42 | {
43 | std::unique_lock lock(m_s.m_mutex);
44 | m_s.m_count++;
45 | m_s.m_cv.notify_all(); // notify the waiting thread
46 | }
47 |
48 | void subtract()
49 | {
50 | std::unique_lock lock(m_s.m_mutex);
51 | m_s.m_count--;
52 | m_s.m_cv.notify_all(); // notify the waiting thread
53 | }
54 |
55 | reference_waiter& m_s;
56 | };
57 |
58 | [[nodiscard]] reference_waiter_holder take_reference()
59 | {
60 | return reference_waiter_holder(*this);
61 | }
62 |
63 | private:
64 | std::mutex m_mutex;
65 | std::condition_variable m_cv;
66 | int m_count = 0;
67 | };
68 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # Win32 Xaml App Samples
2 | C++ samples that demonstrate basic Win32 use of Xaml using [DesktopWindowXamlSource](https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.hosting.desktopwindowxamlsource?view=winrt-20348), also known
3 | as Xaml Islands.
4 |
5 | * [Minimal](./Minimal/Win32XamlApp.cpp) - The app client area is all Xaml. The markup is stored as a string and parsed at runtime.
6 | Less that 150 lines of code with 50 of it being markup (the `NavigationView` stolen from Terminals Settings UI)
7 |
8 | 
9 |
10 | * [Multi Window App ](./MultiWindow/MultiWindowXamlApp.cpp) - A multi-window Win32 app that uses Xaml. This hooks up some
11 | handlers to the markups contents to demonstrate responding to Xaml originated events.
12 |
13 | 
14 |
15 | * [Multi Xaml Mulit Window App](./MultiXamlAndWindow/MultiXamlAndWindowApp.cpp) - Multi window app that creates 2 islands in each window. Mostly a diagnostic case for testing.
16 |
17 | ## Limitations
18 | * Not using WinUI 2.x, using System Xaml instead.
19 | * Does not implement accelerator handling or any integration with Win32 UI (who wants to use Win32 UI anyway!).
20 | * Uses the system provided Xaml Application object, so no way to provide custom metadata.
21 | * Intellisense in the .xaml files is broken, VS is confused seeing a .xaml file in a Win32 app project type.
22 |
23 | ## Debugging tips
24 | Look in the debugger output window for Xaml parsing errors, it identifies line and offset of the error.
25 |
26 | ## Speical clone instructions
27 | Be sure to clone this repo with `--recurse-submodules` ([docs with details](http://git-scm.com/book/en/v2/Git-Tools-Submodules#Cloning-a-Project-with-Submodules)).
28 | Alternatively run `git submodule update --init` to use the Win32AppHelper submodule.
29 |
30 | `git clone --recurse-submodules https://github.com/ChrisGuzak/Win32XamlApp.git`
31 |
32 |
--------------------------------------------------------------------------------
/win32_app.manifest:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | true/PM
17 | PerMonitorV2, PerMonitor
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------