첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
0.01MB

▶ 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="ComponentDispatcher 클래스 : ThreadFilterMessage 정적 이벤트를 사용해 프로세스 간 메시지 송수신하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Button Name="sendButton"
            Width="150"
            Height="30"
            Content="메시지 송신" />
    </Grid>
</Window>

 

728x90

 

▶ MainWindow.xaml.cs

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 메시지 등록하기 - RegisterWindowMessage(messageName)

        /// <summary>
        /// 윈도우 메시지 등록하기
        /// </summary>
        /// <param name="messageName">메시지명</param>
        /// <returns>메시지 ID</returns>
        [DllImport("user32.dll", CharSet = CharSet.Unicode)]
        private static extern uint RegisterWindowMessage(string messageName);

        #endregion
        #region 메시지 게시하기 - PostMessage(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", CharSet = CharSet.Unicode)]
        private static extern bool PostMessage(IntPtr windowHandle, uint message, uint wordParameter, uint longParameter);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 윈도우 핸들
        /// </summary>
        private IntPtr windowHandle;

        /// <summary>
        /// 메시지 ID
        /// </summary>
        private uint messageID;

        /// <summary>
        /// HWND_BROADCAST
        /// </summary>
        private const uint HWND_BROADCAST = 0xffff;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.sendButton.Click += sendButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 소스 초기화시 처리하기 - OnSourceInitialized(e)

        /// <summary>
        /// 소스 초기화시 처리하기
        /// </summary>
        /// <param name="e">이벤트 인자</param>
        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            this.windowHandle = new WindowInteropHelper(this).Handle;
            
            this.messageID = RegisterWindowMessage("User Message");
            
            ComponentDispatcher.ThreadFilterMessage += ComponentDispatcher_ThreadFilterMessage;
        }

        #endregion

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

        #region 컴포넌트 디스패처 스레드 필터 메시지 처리하기 - ComponentDispatcher_ThreadFilterMessage(message, handled)

        /// <summary>
        /// 컴포넌트 디스패처 스레드 필터 메시지 처리하기
        /// </summary>
        /// <param name="message">메시지</param>
        /// <param name="handled">처리 여부</param>
        private void ComponentDispatcher_ThreadFilterMessage(ref MSG message, ref bool handled)
        {
          //if(message.message == messageID && message.wParam != windowHandle)
            if(message.message == messageID)
            {
                MessageBox.Show("수신 메시지 : " + message.lParam.ToString());
            }
        }

        #endregion
        #region 메시지 송신 버튼 클릭시 처리하기 - sendButton_Click(sender, e)

        /// <summary>
        /// 메시지 송신 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void sendButton_Click(object sender, RoutedEventArgs e)
        {
            PostMessage((IntPtr)HWND_BROADCAST, messageID, (uint)windowHandle, 100);
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요