Справочник по C#

    Исходники по языку программирования CSharp

    Добавляем свои кнопки в заголовок окна WPF(WinAPI): Справочник по C#

    /
    /
    /
    436 Views


    using System;
    using System.Windows;
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    namespace WpfApplication3
    {
    // Interaction logic for Window1.xaml
    
    public partial class Window1 : Window
    {
    #region Win32 API Stuff
    
    // Define the Win32 API methods we are going to use
    [DllImport("user32.dll")]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    
    [DllImport("user32.dll")]
    private static extern bool InsertMenu(IntPtr hMenu, Int32 wPosition, Int32 wFlags, Int32 wIDNewItem, string lpNewItem);
    
    /// Define our Constants we will use
    public const Int32 WM_SYSCOMMAND = 0x112;
    public const Int32 MF_SEPARATOR = 0x800;
    public const Int32 MF_BYPOSITION = 0x400;
    public const Int32 MF_STRING = 0x0;
    
    #endregion
    
    public const Int32 _SettingsSysMenuID = 1000;
    public const Int32 _AboutSysMenuID = 1001;
    
    public IntPtr Handle
    {
    get
    {
    return new WindowInteropHelper(this).Handle;
    }
    }
    
    public Window1()
    {
    InitializeComponent();
    
    this.Loaded += new RoutedEventHandler(Window1_Loaded);
    }
    
    private void Window1_Loaded(object sender, RoutedEventArgs e)
    {
    
    IntPtr systemMenuHandle = GetSystemMenu(this.Handle, false);
    
    
    InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR, 0, string.Empty);
    InsertMenu(systemMenuHandle, 6, MF_BYPOSITION, _SettingsSysMenuID, "Settings...");
    InsertMenu(systemMenuHandle, 7, MF_BYPOSITION, _AboutSysMenuID, "About...");
    
    // Attach our WndProc handler to this Window
    HwndSource source = HwndSource.FromHwnd(this.Handle);
    source.AddHook(new HwndSourceHook(WndProc));
    }
    
    private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
    if (msg == WM_SYSCOMMAND)
    {
    // Execute the appropriate code for the System Menu item that was clicked
    switch (wParam.ToInt32())
    {
    case _SettingsSysMenuID:
    MessageBox.Show("Settings");
    handled = true;
    break;
    case _AboutSysMenuID:
    MessageBox.Show("About");
    handled = true;
    break;
    }
    }
    
    return IntPtr.Zero;
    }
    }
    }
    

    Вот, что получилось у меня:

    Добавляем свои кнопки в заголовок окна WPF(WinAPI): Справочник по C#

    0.00 (0%) 0 votes

    moyadcode13
    • Facebook
    • Twitter
    • Google+
    • Linkedin
    • Pinterest