共用方式為


IWindowNative::get_WindowHandle 方法 (microsoft.ui.xaml.window.h)

擷取實作 IWindowNative 之物件所表示之視窗的視窗句柄 (HWND) 。

如需詳細資訊和程式代碼範例,請參閱擷 取視窗句柄 (HWND)

語法

HRESULT get_WindowHandle(
  HWND *hWnd
);

參數

hWnd

視窗句柄 (HWND) 。

傳回值

如果此方法成功,則會傳回 S_OK。 否則,它會傳回 HRESULT 錯誤碼。

備註

範例

遵循此範例之前,請先檢閱下列主題:

自訂視窗圖示

在此範例中,我們會示範如何擷取主視窗的視窗句柄 (HWND) ,並使用該句柄來自定義視窗的標題列及其內容。

建立新專案

  1. 在 Visual Studio 中,從 桌面) 項目範本中的空白應用程式、封裝 (WinUI 3 建立新的 C# 或 C++/WinRT 專案。

MainWindow.xaml

注意

如果您需要圖示檔案搭配本逐步解說使用,您可以從WirelessHostednetwork範例應用程式下載computer.ico檔案。 將該檔案放在您的 Assets 資料夾中,並將檔案新增至專案做為內容。 接著,您將能夠使用url Assets/computer.ico來參考檔案。

否則,您可以隨意使用您已經擁有的圖示檔案,並在下列程式代碼清單中變更其兩個參考。

  1. 在下列程式代碼清單中,您會看到我們已 MainWindow.xaml 新增兩個按鈕,併為每個按鈕指定 Click 處理程式。 在第一個按鈕的 Click 處理程式中, (basicButton_Click) ,我們會設定標題列圖示和文字。 在第二個 (customButton_Click) 中,我們將標題列取代為名為 customTitleBarPanelStackPanel 內容,來示範更重要的自定義。
<Window
    x:Class="window_titlebar.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:window_titlebar"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid x:Name="rootElement" RowDefinitions="100, *, 100, *">

        <StackPanel x:Name="customTitleBarPanel" Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Top" Visibility="Collapsed">
            <Image Source="Images/windowIcon.gif" />
            <TextBlock VerticalAlignment="Center" Text="Full customization of title bar"/>
        </StackPanel>

        <StackPanel x:Name="buttonPanel"  Grid.Row="2" Orientation="Horizontal" HorizontalAlignment="Center">
            <Button x:Name="basicButton" Click="basicButton_Click" Margin="25">Set the Window title and icon</Button>
            <Button x:Name="customButton" Click="customButton_Click" Margin="25">Customize the window title bar</Button>
        </StackPanel>

    </Grid>
</Window>

MainWindow.xaml.cs/cpp

  1. 在下列程式代碼中,basicButton_Click處理程式的程式代碼中,為了保持自定義標題欄隱藏,我們會折疊 customTitleBarPanel StackPanel,並將 ExtendsContentIntoTitleBar 屬性設定為 false
  2. 然後,我們會使用 Interop 協助程式方法 GetWindowHandle) 來擷取主視窗 (HWND) 的 IWindowNative::get_WindowHandle (C# 呼叫 IWindowNative::get_WindowHandle
  3. 接下來,我們會藉由呼叫 LoadImageSendMessage 函式,使用 PInvoke.User32 NuGet 套件) ,為 C# 設定應用程式圖示 (。
  4. 最後,我們會呼叫 SetWindowText 來更新標題欄字串。
private void basicButton_Click(object sender, RoutedEventArgs e)
{
    // Ensure the custom title bar content is not displayed.
    customTitleBarPanel.Visibility = Visibility.Collapsed;

    // Disable custom title bar content.
    ExtendsContentIntoTitleBar = false;

    //Get the Window's HWND
    var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);

    IntPtr hIcon = PInvoke.User32.LoadImage(
        IntPtr.Zero,
        "Images/windowIcon.ico",
        PInvoke.User32.ImageType.IMAGE_ICON,
        20, 20,
        PInvoke.User32.LoadImageFlags.LR_LOADFROMFILE);

    PInvoke.User32.SendMessage(
        hwnd,
        PInvoke.User32.WindowMessage.WM_SETICON,
        (IntPtr)0,
        hIcon);

    PInvoke.User32.SetWindowText(hwnd, "Basic customization of title bar");
}
// pch.h
...
#include <microsoft.ui.xaml.window.h>
...

// MainWindow.xaml.h
...
void basicButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
...

// MainWindow.xaml.cpp
void MainWindow::basicButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    // Ensure the that custom title bar content is not displayed.
    customTitleBarPanel().Visibility(Visibility::Collapsed);

    // Disable custom title bar content.
    ExtendsContentIntoTitleBar(false);

    // Get the window's HWND
    auto windowNative{ this->m_inner.as<::IWindowNative>() };
    HWND hWnd{ 0 };
    windowNative->get_WindowHandle(&hWnd);

    HICON icon{ reinterpret_cast<HICON>(::LoadImage(nullptr, L"Assets/computer.ico", IMAGE_ICON, 0, 0, LR_DEFAULTSIZE | LR_LOADFROMFILE)) };
    ::SendMessage(hWnd, WM_SETICON, 0, (LPARAM)icon);

    this->Title(L"Basic customization of title bar");
}
  1. customButton_Click處理程式中,我們將 customTitleBarPanel StackPanel 的可見性設定為 Visible
  2. 然後將 ExtendsContentIntoTitleBar 屬性 true設定為 ,並呼叫 SetTitleBar 以顯示 customTitleBarPanelStackPanel 作為我們的自定義標題欄。
private void customButton_Click(object sender, RoutedEventArgs e)
{
    customTitleBarPanel.Visibility = Visibility.Visible;

    // Enable custom title bar content.
    ExtendsContentIntoTitleBar = true;
    // Set the content of the custom title bar.
    SetTitleBar(customTitleBarPanel);
}
// MainWindow.xaml.h
...
void customButton_Click(Windows::Foundation::IInspectable const& sender, Microsoft::UI::Xaml::RoutedEventArgs const& args);
...

// MainWindow.xaml.cpp
void MainWindow::customButton_Click(IInspectable const&, RoutedEventArgs const&)
{
    customTitleBarPanel().Visibility(Visibility::Visible);

    // Enable custom title bar content.
    ExtendsContentIntoTitleBar(true);

    // Set the content of the custom title bar.
    SetTitleBar(customTitleBarPanel());
}

App.xaml

  1. 在檔案中 App.xaml ,緊接在 <!-- Other app resources here --> 批註之後,我們已新增標題欄的一些自定義色彩筆刷,如下所示。
<Application
    x:Class="window_titlebar.App"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:window_titlebar">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
                <!-- Other merged dictionaries here -->
            </ResourceDictionary.MergedDictionaries>
            <!-- Other app resources here -->
            <SolidColorBrush x:Key="WindowCaptionBackground">Green</SolidColorBrush>
            <SolidColorBrush x:Key="WindowCaptionBackgroundDisabled">LightGreen</SolidColorBrush>
            <SolidColorBrush x:Key="WindowCaptionForeground">Red</SolidColorBrush>
            <SolidColorBrush x:Key="WindowCaptionForegroundDisabled">Pink</SolidColorBrush>
        </ResourceDictionary>
    </Application.Resources>
</Application>
  1. 如果您已在自己的應用程式中遵循這些步驟,您可以立即建置專案並執行應用程式。 您會看到類似下列 (的應用程式視窗,並顯示自訂應用程式圖示) :

    沒有自定義的範本應用程式。
    範本應用程式。

  • 以下是基本的自訂標題列:

    具有自訂應用程式圖示的範本應用程式。
    具有自訂應用程式圖示的範本應用程式。

  • 以下是完整的自訂標題列:

    具有自定義標題列的範本應用程式。
    具有自定義標題列的範本應用程式。

規格需求

需求
最低支援的用戶端 Windows 10 版本 1809 (Windows 應用程式 SDK 0.5 或更新版本)
標頭 microsoft.ui.xaml.window.h

另請參閱