Message (Estructura)
Actualización: noviembre 2007
Implementa un mensaje de Windows.
Ensamblado: System.Windows.Forms (en System.Windows.Forms.dll)
La estructura Message ajusta los mensajes que envía Windows. Puede utilizar esta estructura para ajustar un mensaje y asignarlo al procedimiento de ventana a fin de ser enviado. También puede utilizar esta estructura para obtener información sobre un mensaje que el sistema envía a la aplicación o los controles.
No puede crear directamente la estructura Message. En su lugar, utilice el método Create. Por motivos de eficacia, la estructura Message utiliza su grupo de objetos Message existentes en lugar de crear instancias de uno nuevo, si esto es posible. Sin embargo, si no hay ninguna estructura Message disponible en el grupo, se crean instancias de una nueva estructura.
En el siguiente ejemplo de código se muestra cómo se reemplaza el método WndProc para controlar los mensajes del sistema operativo identificados en la estructura Message. El mensaje del sistema operativo WM_ACTIVATEAPP se controla en este ejemplo para saber cuándo se activa otra aplicación. Consulte la documentación existente sobre Platform SDK que se encuentra en MSDN Library para conocer los valores disponibles de Message.Msg, Message.LParam y Message.WParam. Los valores constantes reales se pueden encontrar en el archivo de encabezado Windows.h incluido en la descarga de Platform SDK (sección Core SDK), que también está disponible en MSDN.
using System; using System.Drawing; using System.Windows.Forms; namespace csTempWindowsApplication1 { public class Form1 : System.Windows.Forms.Form { // Constant value was found in the "windows.h" header file. private const int WM_ACTIVATEAPP = 0x001C; private bool appActive = true; [STAThread] static void Main() { Application.Run(new Form1()); } public Form1() { this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; this.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); } protected override void OnPaint(PaintEventArgs e) { // Paint a string in different styles depending on whether the // application is active. if (appActive) { e.Graphics.FillRectangle(SystemBrushes.ActiveCaption,20,20,260,50); e.Graphics.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20,20); } else { e.Graphics.FillRectangle(SystemBrushes.InactiveCaption,20,20,260,50); e.Graphics.DrawString("Application is Inactive", this.Font, SystemBrushes.ActiveCaptionText, 20,20); } } [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")] protected override void WndProc(ref Message m) { // Listen for operating system messages. switch (m.Msg) { // The WM_ACTIVATEAPP message occurs when the application // becomes the active application or becomes inactive. case WM_ACTIVATEAPP: // The WParam value identifies what is occurring. appActive = (((int)m.WParam != 0)); // Invalidate to get new text painted. this.Invalidate(); break; } base.WndProc(ref m); } } }
package JSLTempWindowsApplication1;
import System.*;
import System.Drawing.*;
import System.Windows.Forms.*;
import System.Security.Permissions.*;
public class Form1 extends System.Windows.Forms.Form
{
// Constant value was found in the "windows.h" header file.
private final int WM_ACTIVATEAPP = 0x1C;
private boolean appActive = true;
/** @attribute STAThread()
*/
public static void main(String[] args)
{
Application.Run(new Form1());
} //main
public Form1()
{
this.set_Size(new System.Drawing.Size(300, 300));
this.set_Text("Form1");
this.set_Font(new System.Drawing.Font("Microsoft Sans Serif", 18,
System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point,
(ubyte)0));
} //Form1
protected void OnPaint(PaintEventArgs e)
{
// Paint a string in different styles depending on whether the
// application is active.
if (appActive) {
e.get_Graphics().FillRectangle(SystemBrushes.get_ActiveCaption(),
20, 20, 260, 50);
e.get_Graphics().DrawString("Application is active",
this.get_Font(), SystemBrushes.get_ActiveCaptionText(),
20, 20);
}
else {
e.get_Graphics().FillRectangle(SystemBrushes.get_InactiveCaption(),
20, 20, 260, 50);
e.get_Graphics().DrawString("Application is Inactive",
this.get_Font(), SystemBrushes.get_ActiveCaptionText(),
20, 20);
}
} //OnPaint
/** @attribute SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.UnmanagedCode)
*/
protected void WndProc(/** @ref */Message m)
{
// Listen for operating system messages.
switch (m.get_Msg()) {
// The WM_ACTIVATEAPP message occurs when the application
// becomes the active application or becomes inactive.
case WM_ACTIVATEAPP:
// The WParam value identifies what is occurring.
appActive = m.get_WParam().ToInt32() != 0;
// Invalidate to get new text painted.
this.Invalidate();
break;
}
super.WndProc(m);
} //WndProc
} //Form1
Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98
.NET Framework y .NET Compact Framework no admiten todas las versiones de cada plataforma. Para obtener una lista de las versiones compatibles, vea Requisitos de sistema de .NET Framework.