c#.net

c#.netcf winproc 사용

우유빛 2009. 10. 16. 16:33

http://heart4u.co.kr/tblog/191

 

 

.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);
        }
    }