3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

WinUI3 カスタムタイトルバーで、最大化、最小化、閉じるボタンを非表示にする

Last updated at Posted at 2025-06-03

WinUI3でライト、ダークのテーマの切り替えを、システムボタンにも反映させたい!

こちらの投稿に触発されて、アプリにテーマ切り替えを導入したい!と再び思いました。

カスタムタイトルバーを利用していたのですが、以前、テーマ切り替えボタンを試した時は、なんだか中途半端な色変えで、アレ??と思っていたのですが、どうやらxamlで背景の指示をしていなかったためのようです。

<Grid x:Name="RootGrid" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
・・・
    <AppBarButton Click="ToggleThemeButton_Click" Margin="8,0,0,0" ToolTipService.ToolTip="Change Theme">
        <AppBarButton.Icon>
            <FontIcon Glyph="&#xE793;"/>
        </AppBarButton.Icon>
    </AppBarButton>

private void ToggleTheme()
{
    // ルートの Grid(ここでは RootGrid)の ActualTheme プロパティで、現在のテーマをチェック
    if (RootGrid.ActualTheme == ElementTheme.Light)
    {
        RootGrid.RequestedTheme = ElementTheme.Dark;
    }
    else
    {
        RootGrid.RequestedTheme = ElementTheme.Light;
    }
}

image.png

テーマ変更の☀ボタンを押すと↓

image.png

いたはずの最大化、最小化、閉じるボタンがいなくなった!
・・・のではなくて、マウスを持っていくと見える!

image.png

最大化、最小化、閉じるボタンはシステムボタンで、アプリでのテーマ切り替えには左右されず、Windowsのシステム設定値のままらしい。

カスタムタイトルバーを使えばできますよ?

とCopilotは言うけれど、これがなかなか実現できなくて。
で、いろいろ遠回りして、最終的にWinUI 3 Galleryに載ってた方法で実現できそうな目途がついたので、おすそ分け。
最近更新されたのか、Windowing > TitleBar にありました。

// Retrieve the main window and extend its content into the title bar for custom styling.
Window window = App.MainWindow;
window.ExtendsContentIntoTitleBar = true;

// Set the preferred height option for the title bar
window.AppWindow.TitleBar.PreferredHeightOption = TitleBarHeightOption.Collapsed;

image.png
(設定した内容が、リアルタイムに反映されるので、すごいですよね、このアプリ)

手順的には、

  1. カスタムタイトルバーを作る
  2. 上記のコードでシステムボタンの高さを無しにする
  3. 必要に応じて閉じるボタン、最大化、最小化ボタンをつくる

この手順がWinUI3で完結していて、スッキリと実装できそうです。
暫定で、雑に閉じるボタンだけ実装してみましたが、
image.png
テーマ切り替えしても、アプリのボタンなので、ちゃんと色変えに追従してくれます。
image.png

その他の方法(3つ全てを無効にできなかった方法)

最大化、最小化の無効化は、次の記事を参考にできました。

閉じるボタンはどのようにするのかと探して、下記を見つけたのですが、結局うまくいきませんでした。

他にも、システムボタンの色を透明にしようとするなど、いくつか試してみたのですが結局うまくいかず、ボタンをいい感じに重ねるしかないのかなー?
と思っていた矢先、TitleBarHeightOption.Collapsedを見つけました。

あとはいい感じに閉じるボタンとかをレイアウトするだけ・・・!

3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?