방법: MessageWindow 클래스 사용
업데이트: 2007년 11월
.NET Compact Framework에서는 MessageWindow 및 Message 클래스를 제공하여 Windows 기반 메시지를 생성하고 받습니다. MessageWindow 클래스는 폼에 대한 핸들을 포함하는 창을 네이티브 코드에서 만들고 네이티브 Windows 함수에 대한 P/Invoke 호출을 수행합니다. MessageWindow 클래스는 .NET Compact Framework에서만 사용할 수 있습니다.
메시지 창에 대한 창 핸들(Hwnd)을 사용하여 메시지 창에 Windows 메시지를 보낼 수 있습니다. 관리되는 코드에서 Create를 사용하거나 응용 프로그램에서 네이티브 컨트롤을 사용하여 메시지를 생성할 수 있습니다. 사용자는 자신이 만든 메시지만 받을 수 있으며 운영 체제 메시지를 모니터링하는 데 MessageWindow를 사용할 수 없습니다.
메시지 창에서 특정 메시지가 검색되면 WndProc 메서드를 사용하여 해당 메시지가 폼에 전달되므로 사용자는 Windows 기반 메시지에 대한 응답으로 코드를 제공할 수 있습니다.
![]() |
---|
.NET Compact Framework에서는 대리자를 함수 포인터로 마샬링하고 관리되는 함수를 네이티브 코드에서 직접 호출할 수 있는 기능을 제공하여 MessageWindow 대신 사용합니다. 자세한 내용은 .NET Compact Framework의 상호 운용성을 참조하십시오. |
예제
이 예제에서는 네이티브 구성 요소를 사용하지 않고 MessageWindow의 기능을 보여 줍니다. 이 클래스는 마우스 포인터가 사용자 지정 컨트롤의 Rectangle 또는 Panel 컨트롤 내에 있을 때 현재 포인터의 x 및 y 좌표가 포함된 Windows 메시지를 폼으로 보냅니다. 사용자 지정 컨트롤은 폼에서 상자 형태로 나타납니다. 두 컨트롤 모두 OnMouseMove 메서드를 사용하여 메시지를 보냅니다. 이러한 메시지에는 x, y 좌표가 포함되어 있습니다. 그러면 폼은 현재 x, y 좌표 및 메시지를 보낸 컨트롤로 레이블을 업데이트하여 사용자 정의 WM_BOXUPDATE 및 WM_PNLUPDATE 메시지에 응답합니다.
마우스가 상자와 패널의 외부에 있는 경우 폼의 OnMouseMove 메서드는 폼 컨텍스트에서 x 및 y 좌표로 레이블을 업데이트합니다.
Imports System
Imports System.Windows.Forms
Imports Microsoft.WindowsCE.Forms
Public Class MessageWindowForm
Inherits System.Windows.Forms.Form
Private mainMenu1 As System.Windows.Forms.MainMenu
' Create an instance of MsgWindow, a derived MessageWindow class.
Private MsgWin As MsgWindow
Public Sub New()
InitializeComponent()
' Create the message window using this form for its constructor.
Me.MsgWin = New MsgWindow(Me)
End Sub
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
MyBase.Dispose(disposing)
End Sub
#Region "Windows Form Designer generated code"
Private Sub InitializeComponent()
Me.mainMenu1 = New System.Windows.Forms.MainMenu
'
' MessageWindowForm
'
Me.Menu = Me.mainMenu1
Me.Text = "Message Window Test"
End Sub
#End Region
Shared Sub Main()
Application.Run(New MessageWindowForm)
End Sub
' Process taps to generate messages
' with the WParam and LParam parameters
' using the X and Y mouse coordinates.
Protected Overrides Sub OnMouseMove(ByVal e As MouseEventArgs)
Dim msg As Microsoft.WindowsCE.Forms.Message = _
Microsoft.WindowsCE.Forms.Message.Create(MsgWin.Hwnd, _
MsgWindow.WM_CUSTOMMSG, New IntPtr(e.X), New IntPtr(e.Y))
MessageWindow.SendMessage(msg)
MyBase.OnMouseMove(e)
End Sub
' This callback method responds to the Windows-based message.
Public Sub RespondToMessage(ByVal x As Integer, ByVal y As Integer)
Me.Text = "X = " + x.ToString() + ", Y= " + y.ToString()
End Sub
End Class
' Derive MessageWindow to respond to
' Windows messages and to notify the
' form when they are received.
Public Class MsgWindow
Inherits MessageWindow
' Assign integers to messages.
' Note that custom Window messages start at WM_USER = 0x400.
Public Const WM_CUSTOMMSG As Integer = &H400
' Create an instance of the form.
Private msgform As MessageWindowForm
' Save a reference to the form so it can
' be notified when messages are received.
Public Sub New(ByVal msgform As MessageWindowForm)
Me.msgform = msgform
End Sub
' Override the default WndProc behavior to examine messages.
Protected Overrides Sub WndProc(ByRef msg As Microsoft.WindowsCE.Forms.Message)
Select Case msg.Msg
' If message is of interest, invoke the method on the form that
' functions as a callback to perform actions in response to the message.
Case WM_CUSTOMMSG
Me.msgform.RespondToMessage(Fix(msg.WParam.ToInt32), Fix(msg.LParam.ToInt32))
End Select
' Call the base class WndProc method
' to process any messages not handled.
MyBase.WndProc(msg)
End Sub
End Class
using System;
using System.Windows.Forms;
using Microsoft.WindowsCE.Forms;
namespace MsgWindow
{
public class MessageWindowForm : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
// Create an instance of MsgWindow, a derived MessageWindow class.
MsgWindow MsgWin;
public MessageWindowForm()
{
InitializeComponent();
// Create the message window using this form for its constructor.
this.MsgWin = new MsgWindow(this);
}
protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.Menu = this.mainMenu1;
this.Text = "Message Window Test";
}
#endregion
static void Main()
{
Application.Run(new MessageWindowForm());
}
// Process taps to generate messages
// with the WParam and LParam parameters
// using the X and Y mouse coordinates.
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);
}
// This callback method responds to the Windows-based message.
public void RespondToMessage(int x, int y)
{
this.Text = "X = " + x.ToString() + ", Y= " + y.ToString();
}
}
// Derive MessageWindow to respond to
// Windows messages and to notify the
// form when they are received.
public class MsgWindow : MessageWindow
{
// Assign integers to messages.
// Note that custom Window messages start at WM_USER = 0x400.
public const int WM_CUSTOMMSG = 0x0400;
// Create an instance of the form.
private MessageWindowForm msgform;
// Save a reference to the form so it can
// be notified when messages are received.
public MsgWindow(MessageWindowForm msgform)
{
this.msgform = msgform;
}
// Override the default WndProc behavior to examine messages.
protected override void WndProc(ref Message msg)
{
switch(msg.Msg)
{
// If message is of interest, invoke the method on the form that
// functions as a callback to perform actions in response to the message.
case WM_CUSTOMMSG:
this.msgform.RespondToMessage((int)msg.WParam, (int)msg.LParam);
break;
}
// Call the base WndProc method
// to process any messages not handled.
base.WndProc(ref msg);
}
}
}
코드 컴파일
이 예제에는 다음과 같은 네임스페이스에 대한 참조가 필요합니다.