Partager via


MessageWindow, classe

Mise à jour : novembre 2007

Permet d'envoyer et de recevoir des messages Windows.

Espace de noms :  Microsoft.WindowsCE.Forms
Assembly :  Microsoft.WindowsCE.Forms (dans Microsoft.WindowsCE.Forms.dll)

Syntaxe

'Déclaration
Public Class MessageWindow _
    Implements IDisposable
'Utilisation
Dim instance As MessageWindow
public class MessageWindow : IDisposable
public ref class MessageWindow : IDisposable
public class MessageWindow implements IDisposable

Notes

La classe MessageWindow permet d'envoyer et de recevoir des messages Windows. Elle crée un handle de fenêtre en code natif et réalise les appels de code nom managé requis à des fonctions Windows natives.

Pour utiliser MessageWindow dans votre programme, vous devez créer une classe dérivée de MessageWindow et substituer le comportement par défaut WndProc pour surveiller des messages de fenêtres spécifiques. Vous pouvez générer des messages à l'aide de la classe Message. Vous pouvez recevoir uniquement les messages Windows que vous générez à l'aide de MessageWindow ou ceux avec un contrôle natif.

Pour utiliser cette classe, vous devez ajouter une référence dans votre projet Visual Studio 2005 à l'espace de noms Microsoft.WindowsCE.Forms.

Topic Location
Comment : utiliser la classe MessageWindow .NET Compact Framework
Comment : utiliser la classe MessageWindow .NET Compact Framework
Comment : utiliser la classe MessageWindow .NET Compact Framework

Exemples

L'exemple de code suivant montre MessageWindow en commandant au formulaire d'envoyer des messages Windows avec les coordonnées x-y actuelles de la souris à la fenêtre de message, qui appelle la méthode de rappel dans le formulaire pour afficher les coordonnées dans la barre de titre.

Le formulaire contient une classe personnalisée, MsgWindow, dérivée de MessageWindow. La classe MsgWindow examine les messages dans la méthode WndProc substituée pour rechercher ceux qui ont un identificateur WM_CUSTOMMSG. Lorsqu'elle les trouve, elle appelle la méthode de rappel RespondToMessage définie dans le formulaire.

Le formulaire crée une nouvelle instance de MsgWindow. Le constructeur MsgWindow prend un formulaire qui, dans cet exemple, est le formulaire conteneur. Le formulaire génère des messages Windows dans une substitution de la méthode OnMouseMove.

Lorsque le formulaire s'exécute, les déplacements de souris génèrent des messages dans la fenêtre de message. La méthode WndProc de la fenêtre de message appelle la méthode de rappel dans le formulaire, qui répond aux messages.

Notez que vous devez ajouter à votre projet une référence à Microsoft.WindowsCE.Forms.

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

Hiérarchie d'héritage

System.Object
  Microsoft.WindowsCE.Forms.MessageWindow

Sécurité des threads

Tous les membres static (Shared en Visual Basic) publics de ce type sont thread-safe. Il n'est pas garanti que les membres d'instance soient thread-safe.

Plateformes

Windows CE, Windows Mobile pour Smartphone, Windows Mobile pour Pocket PC

Le .NET Framework et le .NET Compact Framework ne prennent pas en charge toutes les versions de chaque plateforme. Pour obtenir la liste des versions prises en charge, consultez Configuration requise du .NET Framework.

Informations de version

.NET Compact Framework

Pris en charge dans : 3.5, 2.0, 1.0

Voir aussi

Référence

Membres MessageWindow

Microsoft.WindowsCE.Forms, espace de noms