닫기(×) 버튼 비활성화
× 버튼을 눌러도 닫히지 않게하는 방법
MainWindow.xaml<Window
x:Class="SampleApp1.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:local="clr-namespace:SampleApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="200"
Closing="Window_Closing"
ResizeMode="CanResizeWithGrip"
mc:Ignorable="d">
<Grid>
<Button
Margin="20"
Click="Button_Click"
Content="終了" />
</Grid>
</Window>
MainWindow.xaml.csusing System.Windows;
namespace SampleApp1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// 終了フラグ
/// </summary>
public bool IsTerminate { get; set; } = false;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// Closeが呼び出された直後
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (IsTerminate)
{
// 終了
return;
}
MessageBox.Show("終了できません。");
// 終了処理をキャンセル
e.Cancel = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IsTerminate = true;
Close();
}
}
}
× 버튼을 누르지 않는 방법
MainWindow.xaml<Window
x:Class="SampleApp2.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:local="clr-namespace:SampleApp2"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="200"
ResizeMode="CanResizeWithGrip"
SourceInitialized="Window_SourceInitialized"
mc:Ignorable="d">
<Grid>
<Button
Margin="20"
Click="Button_Click"
Content="終了" />
</Grid>
</Window>
MainWindow.xaml.csusing System;
using System.Runtime.InteropServices;
using System.Windows;
namespace SampleApp2
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// メニューのハンドル取得
/// </summary>
/// <param name="hWnd"></param>
/// <param name="bRevert"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
/// <summary>
/// メニュー項目の削除
/// </summary>
/// <param name="hMenu"></param>
/// <param name="uPosition"></param>
/// <param name="uFlags"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
/// <summary>
/// ウィンドウを閉じる
/// </summary>
private const int SC_CLOSE = 0xf060;
/// <summary>
/// uPositionに設定するのは項目のID
/// </summary>
private const int MF_BYCOMMAND = 0x0000;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 初期化時
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_SourceInitialized(object sender, EventArgs e)
{
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper((Window)sender).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
Reference
이 문제에 관하여(닫기(×) 버튼 비활성화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다
https://qiita.com/Kosen-amai/items/b7064952cd68545ab648
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념
(Collection and Share based on the CC Protocol.)
<Window
x:Class="SampleApp1.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:local="clr-namespace:SampleApp1"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="200"
Closing="Window_Closing"
ResizeMode="CanResizeWithGrip"
mc:Ignorable="d">
<Grid>
<Button
Margin="20"
Click="Button_Click"
Content="終了" />
</Grid>
</Window>
using System.Windows;
namespace SampleApp1
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// 終了フラグ
/// </summary>
public bool IsTerminate { get; set; } = false;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// Closeが呼び出された直後
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (IsTerminate)
{
// 終了
return;
}
MessageBox.Show("終了できません。");
// 終了処理をキャンセル
e.Cancel = true;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
IsTerminate = true;
Close();
}
}
}
MainWindow.xaml
<Window
x:Class="SampleApp2.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:local="clr-namespace:SampleApp2"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="200"
ResizeMode="CanResizeWithGrip"
SourceInitialized="Window_SourceInitialized"
mc:Ignorable="d">
<Grid>
<Button
Margin="20"
Click="Button_Click"
Content="終了" />
</Grid>
</Window>
MainWindow.xaml.cs
using System;
using System.Runtime.InteropServices;
using System.Windows;
namespace SampleApp2
{
/// <summary>
/// MainWindow.xaml の相互作用ロジック
/// </summary>
public partial class MainWindow : Window
{
/// <summary>
/// メニューのハンドル取得
/// </summary>
/// <param name="hWnd"></param>
/// <param name="bRevert"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
/// <summary>
/// メニュー項目の削除
/// </summary>
/// <param name="hMenu"></param>
/// <param name="uPosition"></param>
/// <param name="uFlags"></param>
/// <returns></returns>
[DllImport("user32.dll")]
private static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
/// <summary>
/// ウィンドウを閉じる
/// </summary>
private const int SC_CLOSE = 0xf060;
/// <summary>
/// uPositionに設定するのは項目のID
/// </summary>
private const int MF_BYCOMMAND = 0x0000;
public MainWindow()
{
InitializeComponent();
}
/// <summary>
/// 初期化時
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_SourceInitialized(object sender, EventArgs e)
{
IntPtr hwnd = new System.Windows.Interop.WindowInteropHelper((Window)sender).Handle;
IntPtr hMenu = GetSystemMenu(hwnd, false);
RemoveMenu(hMenu, SC_CLOSE, MF_BYCOMMAND);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}
Reference
이 문제에 관하여(닫기(×) 버튼 비활성화), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/Kosen-amai/items/b7064952cd68545ab648텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)