To achieve your goal of displaying a list of running window names in a TreeView within your WinUI 3 application, you'll need to:
- Create an Observable Collection to store window names.
- Populate the collection using
EnumWindows.
- Bind the collection to the
TreeView's ItemsSource.
- Display the window names in the
TreeView using a DataTemplate.
Below are the steps and code modifications needed to implement this functionality.
1. Define a WindowItem Class
First, create a WindowItem class to represent each window, with a Name property for the window title.
Modify your RunningAppsCard.idl file:
import "ui\controls\cards\Card.idl";
namespace EvolvShell {
runtimeclass WindowItem
{
WindowItem();
String Name;
}
[default_interface]
runtimeclass RunningAppsCard : Card {
RunningAppsCard();
Windows.Foundation.Collections.IObservableVector<WindowItem> WindowItems { get; };
}
} // namespace EvolvShell
This defines WindowItem and a public WindowItems property in RunningAppsCard for data binding.
2. Implement the WindowItem and RunningAppsCard Classes
Update RunningAppsCard.h:
#pragma once
#include "RunningAppsCard.g.h"
#include "WindowItem.g.h"
namespace winrt::EvolvShell::implementation {
struct WindowItem : WindowItemT<WindowItem>
{
WindowItem() = default;
hstring Name() const { return m_name; }
void Name(hstring const& value) { m_name = value; }
private:
hstring m_name;
};
struct RunningAppsCard : RunningAppsCardT<RunningAppsCard> {
RunningAppsCard();
Windows::Foundation::Collections::IObservableVector<EvolvShell::WindowItem> WindowItems();
private:
Windows::Foundation::Collections::IObservableVector<EvolvShell::WindowItem> m_windowItems{ nullptr };
void InitializeWindowItems();
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);
BOOL EnumWindowsCallback(HWND hwnd);
};
} // namespace winrt::EvolvShell::implementation
namespace winrt::EvolvShell::factory_implementation {
struct RunningAppsCard : RunningAppsCardT<RunningAppsCard, implementation::RunningAppsCard> {};
}
Implement RunningAppsCard in RunningAppsCard.cpp:
#include "pch.h"
#include "RunningAppsCard.xaml.h"
#include "WindowItem.h"
#include <windows.h>
#if __has_include("RunningAppsCard.g.cpp")
#include "RunningAppsCard.g.cpp"
#endif
using namespace winrt;
using namespace Microsoft::UI::Xaml;
using namespace Windows::Foundation::Collections;
using namespace Windows::Foundation;
namespace winrt::EvolvShell::implementation {
RunningAppsCard::RunningAppsCard() {
InitializeComponent();
m_windowItems = single_threaded_observable_vector<EvolvShell::WindowItem>();
InitializeWindowItems();
}
Windows::Foundation::Collections::IObservableVector<EvolvShell::WindowItem> RunningAppsCard::WindowItems()
{
return m_windowItems;
}
void RunningAppsCard::InitializeWindowItems()
{
EnumWindows(&RunningAppsCard::EnumWindowsProc, reinterpret_cast<LPARAM>(this));
}
BOOL CALLBACK RunningAppsCard::EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
RunningAppsCard* pThis = reinterpret_cast<RunningAppsCard*>(lParam);
return pThis->EnumWindowsCallback(hwnd);
}
BOOL RunningAppsCard::EnumWindowsCallback(HWND hwnd)
{
WCHAR windowText[256];
if (IsWindowVisible(hwnd) && GetWindowTextW(hwnd, windowText, 256) != 0)
{
// We have a visible window with a title
auto windowItem = winrt::make<WindowItem>();
windowItem.Name(windowText);
m_windowItems.Append(windowItem);
}
return TRUE;
}
} // namespace winrt::EvolvShell::implementation
This code:
- Initializes the
WindowItems collection.
- Populates it by enumerating all top-level windows using
EnumWindows.
- Filters visible windows with titles.
- Adds each window's name to the collection.
3. Bind the Collection to the TreeView
Update your RunningAppsCard.xaml file:
<?xml version="1.0" encoding="utf-8" ?>
<local:Card x:Class="EvolvShell.RunningAppsCard"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="using:EvolvShell"
xmlns:controls="using:Microsoft.UI.Xaml.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
x:Uid="/Cards/RunningAppsCard"
Title="Running apps"
mc:Ignorable="d">
<Grid x:Name="ContentStackPanel">
<Grid.RowDefinitions>
<RowDefinition Height="32" />
<RowDefinition Height="4" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<AutoSuggestBox x:Uid="/Cards/RunningAppsSearch"
PlaceholderText="Search for running apps"
QueryIcon="Find" />
<controls:TreeView x:Name="RunningAppsCardTree"
Grid.Row="2"
ItemsSource="{x:Bind WindowItems}">
<controls:TreeView.ItemTemplate>
<DataTemplate x:DataType="local:WindowItem">
<TextBlock Text="{x:Bind Name}" />
</DataTemplate>
</controls:TreeView.ItemTemplate>
</controls:TreeView>
</Grid>
</local:Card>
Make sure to:
- Use
x:Bind to bind the ItemsSource to the WindowItems property.
- Define a
DataTemplate to specify how each WindowItem is displayed.
- Update the
xmlns:controls to include Microsoft.UI.Xaml.Controls for the TreeView.
4. Ensure Namespaces are Correct
To ensure that the WindowItem class is recognized in XAML, verify that your namespaces are correctly defined.
- The
WindowItem class should be in the EvolvShell namespace, which is mapped to local in XAML.
- Confirm that the
xmlns:local="using:EvolvShell" mapping is present in your XAML file.
5. Build and Run the Application
- Build your solution to ensure there are no compilation errors.
- Run your application. The
TreeView should now display a list of all visible windows with their titles.
Additional Notes
- Threading Considerations:
EnumWindows should be called on the UI thread since you're updating an ObservableCollection bound to UI elements.
- Error Handling: You may want to add error handling to manage cases where window titles cannot be retrieved.
- Performance: For large numbers of windows, consider running
EnumWindows on a background thread and marshaling updates to the UI thread using DispatcherQueue.
By following these steps, you should have a functional TreeView displaying all running windows' names in your WinUI 3 application.