5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C# WPF 画面外をクリックすることで画面を閉じる方法

Posted at

概要

画面がフォーカスロストしたタイミングで対象の画面を閉じる必要があり、ハマったので備忘録用に。
厳密にはフォーカスロストではなく、ウィンドウの非活性化を検知することで対応。
分かってしまえば簡単。以下のコードを対象画面のCSに追記するだけ。

protected override void OnDeactivated(EventArgs ea)
{
    base.OnDeactivated(ea);
    Close();
}

簡単なサンプルコード

  • メイン画面(MainWindow)上のボタンから子画面(SubWindow)を開く。
  • 子画面の画面外をクリック(ウィンドウを非活性化)することで子画面が閉じる。
  • 親画面は閉じないよ!

【制約】
子画面を×ボタン等で閉じると例外エラーが出るから注意。
このため、×ボタン等で閉じられないように 子画面には WindowStyle="None" を入れている。

MainWindow.xaml
<Window x:Class="sample.MainWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:sample"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="Button" HorizontalAlignment="Left" Margin="50,50,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
    </Grid>
</Window>
MainWindow.xaml.cs
using System.Windows;

namespace sample
{
    /// <summary>
    /// MainWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            var SW = new SubWindow();
            SW.Show();
        }
    }
}
SubWindow.xaml
<Window x:Class="sample.SubWindow"
        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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:sample"
        mc:Ignorable="d"
        Title="SubWindow" Height="200" Width="300" WindowStyle="None">
    <Grid>
        
    </Grid>
</Window>
SubWindow.xaml.cs
using System;
using System.Windows;

namespace sample
{
    /// <summary>
    /// SubWindow.xaml の相互作用ロジック
    /// </summary>
    public partial class SubWindow : Window
    {
        public SubWindow()
        {
            InitializeComponent();
        }
        protected override void OnDeactivated(EventArgs e)
        {
            base.OnDeactivated(e);
            Close();
        }
    }
}
以上。
5
6
1

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
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?