WPFアプリケーションを作成して、xaml 画面右上の「×」ボタンを無効化したのですが、
どのような理屈で無効になっているかが、いまいちわからず、
元に戻す(有効に戻す)方法が良く分かりません。
ボタン1を押したら無効、ボタン2を押したら有効、となるように
実装したいです。
下記のサイトを参考にしました。
https://www.pine4.net/Memo/Article/Archives/51
以下の様に作ってみました。
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApp2 { /// <summary> /// MainWindow.xaml の相互作用ロジック /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } internal const UInt32 MF_GRAYED = 0x00000001; internal const UInt32 MF_REMOVE = 0x00001000; internal const UInt32 MF_BYPOSITION = 0x00000400; internal const UInt32 SC_CLOSE = 0x0000F060; [DllImport("user32.dll")] static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); [DllImport("user32.dll")] static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); [DllImport("user32.dll")] static extern int GetMenuItemCount(IntPtr hMenu); [DllImport("user32.dll")] static extern bool RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags); private void Button1_Click(object sender, RoutedEventArgs e) { var hwnd = new WindowInteropHelper(this).Handle; IntPtr hMenu = GetSystemMenu(hwnd, false); if (hMenu != IntPtr.Zero) { EnableMenuItem(hMenu, SC_CLOSE, MF_GRAYED); int menuItemCount = GetMenuItemCount(hMenu); RemoveMenu(hMenu, (uint)menuItemCount - 1, MF_REMOVE | MF_BYPOSITION); } } private void Button2_Click(object sender, RoutedEventArgs e) { } } }
<Window x:Class="WpfApp2.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:WpfApp2" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="108,158,0,0" VerticalAlignment="Top" Width="75" Click="Button1_Click"/> <Button x:Name="button2" Content="Button" HorizontalAlignment="Left" Margin="276,158,0,0" VerticalAlignment="Top" Width="75" Click="Button2_Click"/> </Grid> </Window>
ご教授いただけないでしょうか?
宜しくお願い致します。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。