2013-07-31 16 views
11

Chciałbym wiedzieć, jak wyłączyć (nie usunąć/ukryć) przycisk Zamknij w oknie WPF. Wiem, jak to ukryć, która sprawia, że ​​pasek tytułu okna wyglądać następująco:Wyłącz przycisk zamykania na pasku tytułu okna WPF (C#)

enter image description here

Ale chcę go wyłączyć oznacza, powinno to wyglądać tak:

enter image description here

jestem wykonywanie skryptów w języku C# i korzystanie z WPF (Windows Presentation Foundation).

+0

znalazłem ten link Zobacz, czy to [link] [1] może pomóc: [1]: http://stackoverflow.com/questions/743906/how-to-hide-close-button-i n-wpf-window – Vishal

Odpowiedz

16

Spróbuj tego:

public partial class MainWindow : Window 
{ 

    [DllImport("user32.dll")] 
    static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert); 
    [DllImport("user32.dll")] 
    static extern bool EnableMenuItem(IntPtr hMenu, uint uIDEnableItem, uint uEnable); 



    const uint MF_BYCOMMAND = 0x00000000; 
    const uint MF_GRAYED = 0x00000001; 
    const uint MF_ENABLED = 0x00000000; 

    const uint SC_CLOSE = 0xF060; 

    const int WM_SHOWWINDOW = 0x00000018; 
    const int WM_CLOSE = 0x10; 

    public MainWindow() 
    { 
     InitializeComponent(); 
    } 

    private void Window_Loaded(object sender, RoutedEventArgs e) 
    { 

    } 

    protected override void OnSourceInitialized(EventArgs e) 
    { 
     base.OnSourceInitialized(e); 

     HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource; 

     if (hwndSource != null) 
     { 
      hwndSource.AddHook(new HwndSourceHook(this.hwndSourceHook)); 
     } 
    } 


    IntPtr hwndSourceHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) 
    { 
     if (msg == WM_SHOWWINDOW) 
     { 
      IntPtr hMenu = GetSystemMenu(hwnd, false); 
      if (hMenu != IntPtr.Zero) 
      { 
       EnableMenuItem(hMenu, SC_CLOSE, MF_BYCOMMAND | MF_GRAYED); 
      } 
     } 
     else if (msg == WM_CLOSE) 
     { 
      handled = true; 
     } 
     return IntPtr.Zero; 
    } 
} 

(Taken from tutaj: http://blogs.microsoft.co.il/blogs/tamir/archive/2007/09/26/never-ever-close-me-how-to-disable-close-button-in-wpf.aspx)

upewnij się ustawić ResizeMode do NoResize.

+1

Sugeruję, że" else if (msg == WM_CLOSE) { } zostało pominięte, ponieważ uniemożliwi to zamykanie okna programowo. – Hans

3

Trzeba zastąpić aw OnCLosing przypadku ustawić e.cancel = true

public MyWindow() 
{ 
    InitializeComponent(); 
    this.Closing += new System.ComponentModel.CancelEventHandler(MyWindow_Closing); 
} 

void MyWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e) 
{ 
    e.Cancel = true; 
} 
+2

To nie sprawia, że ​​przycisk wygląda na wyłączony. –

+0

Tak, ale chcę faktycznie wyłączyć przycisk Zamknij, zamiast go anulować zamknięcie. – Bubbled86

+0

Obawiam się, że można tylko ukryć lub przesłonić w WPF, proszę spojrzeć na ten http://stackoverflow.com/questions/743906/how-to-hide-close-button-in-wpf-window – Rohit

1

Prawdopodobnie można to zrobić z win32 hackery.

Zrobiłem to w następujący sposób: Pobierz CustomChromeWindow (który ostatecznie będzie wyglądał dokładnie tak, jak ten na zdjęciu) i po prostu połącz właściwość Command() z viewmodel, a następnie ustaw CanExecuteCommand = false, co spowoduje wyłączenie przycisku (How does one "disable" a button in WPF using the MVVM pattern?).

Może mi to zbyt: How to disable close button on a window in another process with C++?

zasadzie nazwać ten kod z pinvoke. Możesz łatwo uzyskać uchwyt okna WPF.

1

This post które odpowiedź za pomocą Behavior, GetWindowLong i SetWindowLong:

public class HideCloseButtonOnWindow : System.Windows.Interactivity.Behavior<Window> 
{ 
    #region bunch of native methods 

    private const int GWL_STYLE = -16; 
    private const int WS_SYSMENU = 0x80000; 

    [DllImport("user32.dll", SetLastError = true)] 
    private static extern int GetWindowLong(IntPtr hWnd, int nIndex); 

    [DllImport("user32.dll")] 
    private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 

    #endregion 

    protected override void OnAttached() 
    { 
     base.OnAttached(); 
     AssociatedObject.Loaded += OnLoaded; 
    } 

    protected override void OnDetaching() 
    { 
     AssociatedObject.Loaded -= OnLoaded; 
     base.OnDetaching(); 
    } 

    private void OnLoaded(object sender, RoutedEventArgs e) 
    { 
     var hwnd = new System.Windows.Interop.WindowInteropHelper(AssociatedObject).Handle; 
     SetWindowLong(hwnd, GWL_STYLE, GetWindowLong(hwnd, GWL_STYLE) & ~WS_SYSMENU); 
    } 
} 

Jak go używać:

<Window x:Class="WpfApplication2.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
    xmlns:w="clr-namespace:WpfApplication2"> 

<i:Interaction.Behaviors> 
    <w:HideCloseButtonOnWindow /> 
</i:Interaction.Behaviors> 

</Window> 
Powiązane problemy