Msg Property
Collapse the table of content
Expand the table of content

Message.Msg Property


Gets or sets the ID number for the message.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public int Msg { get; set; }

Property Value

Type: System.Int32

The ID number for the message.

The following code example demonstrates overriding the WndProc method to handle operating system messages identified in the Message. The WM_ACTIVATEAPP operating system message is handled in this example to know when another application is becoming active. For information about the available Message.Msg, Message.LParam, and Message.WParam values, see the MSG Structure documentation. For information about the actual constant values, see Message Constants.

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;

        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.DrawString("Application is active", this.Font, SystemBrushes.ActiveCaptionText, 20,20);
                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.

            base.WndProc(ref m);

.NET Framework
Available since 1.1
Return to top
© 2015 Microsoft