2011/10/11

[C#] WPF App의 System Menu에 사용자 메뉴 추가하기

C#의 WPF Application 에서 System 메뉴에 사용자 메뉴를 추가하는 방법이다. 이 방법은 Add System Menu Items to WPF Window using Win32 API에서 알게된 방법으로 Form Application과 다소 다르다. Win32 API를 사용하는 것은 동일하게 보이나 메세지를 핸들링하는 부분이 다른 것 같다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Windows.Interop;

// For Custom menu on System menu
//using System;
//using System.Windows;
//using System.Runtime.InteropServices;
//using System.Windows.Interop;

namespace MyWPFApp
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : 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 = 0x000;
#endregion

// The constants we'll use to identify our custom system menu items
public const Int32 _SettingsSysMenuID = 1000;
public const Int32 _AboutSysMenuID = 1001;

/// <summary>
/// This is the Win32 Interop Handle for this Window
/// </summary>
public IntPtr Handle
{
get
{
return new WindowInteropHelper(this).Handle;
}
}

public MainWindow()
{
InitializeComponent();

/// 아래 코드는 필요 없어 보임. 이벤트 핸들러가 아닌
/// 임의의 함수를 할당할 때 필요한 것으로 보임
//this.Loaded += new RoutedEventHandler(wndMain_Loaded);
}

private void wndMain_Loaded(object sender, RoutedEventArgs e)
{
/// Get the Handle for the Forms System Menu
IntPtr systemMenuHandle = GetSystemMenu(this.Handle, false);

/// Create our new System Menu items just before the Close menu item
// Add a menu seperator
InsertMenu(systemMenuHandle, 5, MF_BYPOSITION | MF_SEPARATOR,
0, string.Empty);
// Add Setting Menu
InsertMenu(systemMenuHandle, 6, MF_BYPOSITION,
_SettingsSysMenuID, "Settings...");
// Add About Menu
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)
{
// Check if a System Command has been executed
if (msg == WM_SYSCOMMAND)
{
/// Execute the appropriate code for
/// the System Menu item that was clicked
switch (wParam.ToInt32())
{
case _SettingsSysMenuID:
MessageBox.Show("\"Settings\" was clicked");
handled = true;
break;
case _AboutSysMenuID:
MessageBox.Show("\"About\" was clicked");
handled = true;
break;
}
}

return IntPtr.Zero;
}
}
}



실행 결과 화면

실행 결과 화면

&n

Original Post : http://neodreamer-dev.tistory.com/588

No comments :

Post a Comment