블로그 이미지
Every unexpected event is a path to learning for you.

카테고리

분류 전체보기 (2737)
Unity3D (817)
Programming (474)
Server (33)
Unreal (4)
Gamebryo (56)
Tip & Tech (228)
협업 (58)
3DS Max (3)
Game (12)
Utility (136)
Etc (96)
Link (32)
Portfolio (19)
Subject (90)
iOS,OSX (53)
Android (14)
Linux (5)
잉여 프로젝트 (2)
게임이야기 (3)
Memories (20)
Interest (38)
Thinking (38)
한글 (30)
PaperCraft (5)
Animation (408)
Wallpaper (2)
재테크 (18)
Exercise (3)
나만의 맛집 (3)
냥이 (10)
육아 (16)
Total
Today
Yesterday
04-19 21:33

.NET 기반에서 C#으로 윈도우메세지를 핸들링하기 위해서는 WndProc를 오버라이드하여 사용하는 것으로 많이 알려져 있습니다. 하지만 .Net CF 에서는 Form클래스에 WndProc메소드가 기본적으로 존재하지 않습니다. 대신에 MessageWindow클래스를 이용하여 WndProc를 오버라이드하면 윈도우 메세지를 핸들링할 수 있도록 지원해줍니다.

 

그럼, 간단하게 폼 위에서 마우스가 이동하면, 이때 마우스 좌표값을 윈도우 메세지 형태로 날려서

이 값을 Form의 Text속성에 표시하는 예제를 함께 구현해 보도록 하지요.

 

우선 MessageWindow 클래스 사용을 위해서는 다음과 같이 참조추가를 해주어야 합니다.

 

 

 

사용하기 원하는 코드에서

 

using Microsoft.WindowsCE.Forms;

 

이제 MessageWindow를 사용할 수 있게 됩니다.

 

아래는 기본적으로 프로젝트 생성시에 생성되는 폼(frmMain)에서

MessageWindow를 상속받아 WndProc를 오버라이드한 클래스를 이용하는 코드입니다.

굵은 부분을 참고하면 어떤식으로 윈도우메세지를 핸들링하는지 흐름을 파악할 수 있을 겁니다.

 

frmMain 코드 - 프로젝트 생성시 생성되는 기본 폼

 

    public partial class frmMain : Form
    {
        MsgWindow MsgWin = null;

 

        public frmMain()
        {
            InitializeComponent();

            this.MsgWin = new MsgWindow(this);
        }

 

        //마우스 이동시에 메세지를 생성하여 날려주는 작업을 해주겠습니다.

        protected override void OnMouseMove(MouseEventArgs e)
        {
            Message msg = Message.Create(MsgWin.Hwnd, MsgWindow.WM_CUSTOMMSG,

                                                            (IntPtr)e.X, (IntPtr)e.Y);
            MessageWindow.SendMessage(ref msg);
            base.OnMouseMove(e);
        }

       

        //MsgWin을 통해 현재 폼에 접근할 수 있도록 메소드를 하나 정의합니다.

        public void RespondToMessage(int x, int y)
        {
            this.Text = "X = " + x.ToString() + ", Y= " + y.ToString();
        }
    }

 

 

 

 MesssageWindow를 상속받아 WndProc를 오버라이드

 

    public class MsgWindow : MessageWindow
    {
        public const int WM_CUSTOMMSG = 0x0400;

        private frmMain msgform;

 

        public MsgWindow(frmMain msgform)
        {
            this.msgform = msgform;
        }

 

        protected override void WndProc(ref Message msg)
        {
            switch (msg.Msg)
            {
                case WM_CUSTOMMSG:
                    this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam);
                    break;
            }
           
            base.WndProc(ref msg);
        }
    }

 

반응형
Posted by blueasa
, |