■ 시스템 메뉴 표시하기

------------------------------------------------------------------------------------------------------------------------


TestProject.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Width="800"

    Height="600"

    Title="시스템 메뉴 표시하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <TextBlock

            HorizontalAlignment="Center"

            VerticalAlignment="Center"

            Text="마우스 오른쪽 버튼을 클릭해 주시기 바랍니다." />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System;

using System.Runtime.InteropServices;

using System.Windows;

using System.Windows.Input;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Import

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter)

 

        /// <summary>

        /// 메시지 보내기

        /// </summary>

        /// <param name="windowHandle">윈도우 핸들</param>

        /// <param name="message">메시지</param>

        /// <param name="wordParameter">WORD 매개 변수</param>

        /// <param name="longParameter">LONG 매개 변수</param>

        /// <returns>처리 결과</returns>

        [DllImport("user32.dll")]

        private static extern IntPtr SendMessage(IntPtr windowHandle, int message, IntPtr wordParameter, IntPtr longParameter);

 

        #endregion

        #region 시스템 메뉴 구하기 - GetSystemMenu(windowHandle, revert)

 

        /// <summary>

        /// 시스템 메뉴 구하기

        /// </summary>

        /// <param name="windowHandle">윈도우 핸들</param>

        /// <param name="revert">반전 여부</param>

        /// <returns>메뉴 핸들</returns>

        [DllImport("user32.dll")]

        private static extern IntPtr GetSystemMenu(IntPtr windowHandle, bool revert);

 

        #endregion

        #region 팝업 메뉴 표시하기 - TrackPopupMenu(menuHandle, flag, x, y, reserved, windowHandle, rectangle)

 

        /// <summary>

        /// 팝업 메뉴 표시하기

        /// </summary>

        /// <param name="menuHandle">메뉴 핸들</param>

        /// <param name="flag">플래그</param>

        /// <param name="x">X 좌표</param>

        /// <param name="y">Y 좌표</param>

        /// <param name="reserved">예약</param>

        /// <param name="windowHandle">윈도우 핸들</param>

        /// <param name="rectangle">사각형</param>

        /// <returns>처리 결과</returns>

        [DllImport("user32.dll")]

        private static extern int TrackPopupMenu(IntPtr menuHandle, uint flag, int x, int y, int reserved,

            IntPtr windowHandle, IntPtr rectangle);

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            PreviewMouseDown += Window_PreviewMouseDown;

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

 

        #region 윈도우 마우스 DOWN PRWVIEW 처리하기 - Window_PreviewMouseDown(sender, e)

 

        /// <summary>

        /// 윈도우 마우스 DOWN PREVIEW 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void Window_PreviewMouseDown(object sender, MouseButtonEventArgs e)

        {

            if(e.ChangedButton == MouseButton.Right)

            {

                Point mousePoint = PointToScreen(e.GetPosition(this));

 

                IntPtr windowHandle = new System.Windows.Interop.WindowInteropHelper(this).Handle;

 

                IntPtr menuHandle = GetSystemMenu(windowHandle, false);

 

                int command = TrackPopupMenu(menuHandle, 0x100, (int)mousePoint.X, (int)mousePoint.Y, 0, windowHandle, IntPtr.Zero);

 

                if(command > 0)

                {

                    SendMessage(windowHandle, 0x112, (IntPtr)command, IntPtr.Zero);

                }

            }

        }

 

        #endregion

    }

}

 

------------------------------------------------------------------------------------------------------------------------

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요