C# Windows Forms – Form Examples

Learn how to create Forms in C# using simple examples.

A Form is a representation of any window displayed in your application. The Form class can be used to create standard, tool, borderless, and floating windows. You can also use the Form class to create modal windows such as a dialog box.

The Form class represents a window or dialog box that makes up an application's user interface.

public class Form : System.Windows.Forms.ContainerControl

Using the properties available in the Form class, you can determine the appearance, size, color, and window management features of the window or dialog box you are creating. The Text property allows you to specify the caption of the window in the title bar. The Size and DesktopLocation properties allow you to define the size and position of the window when it is displayed. You can use the ForeColor color property to change the default foreground color of all controls placed on the form. The FormBorderStyle, MinimizeBox, and MaximizeBox properties allow you to control whether the form can be minimized, maximized, or resized at run time.

In addition to properties, you can use the methods of the class to manipulate a form. For example, you can use the ShowDialog method to show a form as a modal dialog box. You can use the SetDesktopLocation method to position the form on the desktop.

The events of the Form class allow you to respond to actions performed on the form. You can use the Activated event to perform operations such as updating the data displayed in the controls of the form when the form is activated.

You can use a form as the starting class in your application by placing a method called Main in the class. In the Main method add code to create and show the form. You will also need to add the STAThread attribute to the Main method in order for the form to run. When the starting form is closed, the application is also closed.

If you set the Enabled property to false before the Form is visible (for example, setting Enabled to false in the Microsoft Visual Studio designer), the minimize, maximize, close, and system buttons remain enabled. If you set Enabled to false after the Form is visible (for example, when the Load event occurs), the buttons are disabled.

Here are some examples:

Example 0 - Create Form

The following example creates a new instance of a Form and calls the ShowDialog method to display the form as a dialog box. The example sets the FormBorderStyle, AcceptButton, CancelButton, MinimizeBox, MaximizeBox, and StartPosition properties to change the appearance and functionality of the form to a dialog box.

The example also uses the Add method of the form's Controls collection to add two Button controls. The example uses the HelpButton property to display a help button in the caption bar of the dialog box.

Step 1: Create Project

Start by creating your C# windows forms project.

Step 2: Write Code

Create a method to create our Form:

public void CreateMyForm()
{

Create a new instance of the form:

   Form form1 = new Form();

Create two buttons to use as the accept and cancel buttons:

   Button button1 = new Button ();
   Button button2 = new Button ();

Set the text of button1 to "OK":

   button1.Text = "OK";

Set the position of the button on the form:

   button1.Location = new Point (10, 10);

Set the text of button2 to "Cancel":

   button2.Text = "Cancel";

Set the position of the button based on the location of button1:

   button2.Location
      = new Point (button1.Left, button1.Height + button1.Top + 10);

Set the caption bar text of the form:

   form1.Text = "My Dialog Box";

Display a help button on the form:

   form1.HelpButton = true;

Define the border style of the form to a dialog box:

   form1.FormBorderStyle = FormBorderStyle.FixedDialog;

Set the MaximizeBox to false to remove the maximize box.

   form1.MaximizeBox = false;

Set the MinimizeBox to false to remove the minimize box:

   form1.MinimizeBox = false;

Set the accept button of the form to button1:

   form1.AcceptButton = button1;

Set the cancel button of the form to button2:

   form1.CancelButton = button2;

Set the start position of the form to the center of the screen:

   form1.StartPosition = FormStartPosition.CenterScreen;

Add button1 to the form:

   form1.Controls.Add(button1);

Add button2 to the form:

   form1.Controls.Add(button2);

Display the form as a modal dialog box:

   form1.ShowDialog();
}

Here is the full code:

public void CreateMyForm()
{
   Form form1 = new Form();
   Button button1 = new Button ();
   Button button2 = new Button ();

   button1.Text = "OK";
   button1.Location = new Point (10, 10);
   button2.Text = "Cancel";
   button2.Location
      = new Point (button1.Left, button1.Height + button1.Top + 10);
   form1.Text = "My Dialog Box";
   form1.HelpButton = true;

   form1.FormBorderStyle = FormBorderStyle.FixedDialog;
   form1.MaximizeBox = false;
   form1.MinimizeBox = false;
   form1.AcceptButton = button1;
   form1.CancelButton = button2;
   form1.StartPosition = FormStartPosition.CenterScreen;

   form1.Controls.Add(button1);
   form1.Controls.Add(button2);
   form1.ShowDialog();
}

Example 1: HelloWorldForm

This example will comprise the following files:

  • HelloWorldForm.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). HelloWorldForm.cs

Create a file named HelloWorldForm.cs

Here is the full code

using System.Windows.Forms;

namespace HelloWorldForm {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      this.label.Text = "Hello, World!";
      this.label.Font = new System.Drawing.Font("Arial", 32, System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic);
      this.label.ForeColor = System.Drawing.Color.Green;
      this.label.Location = new System.Drawing.Point(5, 100);
      this.label.Size = new System.Drawing.Size(290, 100);

      this.Text = "My first application";
      this.StartPosition = FormStartPosition.CenterScreen;
      this.ClientSize = new System.Drawing.Size(300, 300);
      this.Controls.Add(this.label);
    }

    private Label label = new Label();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 2: FormClick

This example will comprise the following files:

  • FormClick.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormClick.cs

Create a file named FormClick.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class FormClick {
    public static void Main() {
      Application.EnableVisualStyles();
      Form form = new Form();
      form.Text = "Click anywhere on the form";
      form.MouseClick += delegate(object sender, MouseEventArgs e) {
        if (e.Button == MouseButtons.Left)
          MessageBox.Show("The form is clicked", "FormClick", MessageBoxButtons.OK);
      };
      Application.Run(form);
    }
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 3: FormIcon

This example will comprise the following files:

  • FormIcon.cs
  • Resources.Designer.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormIcon.cs

Create a file named FormIcon.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class MainForm : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new MainForm());
    }

    public MainForm() {
      this.Text = "Form Icon example";
      this.Icon = Examples.Properties.Resources.Gammasoft;
    }

    private Label label1 = new Label();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 4: FormPaint

This example will comprise the following files:

  • FormPaint.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormPaint.cs

Create a file named FormPaint.cs

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      this.Text = "Paint example";
      this.StartPosition = FormStartPosition.Manual;
      this.Location = new System.Drawing.Point(100, 100);
      this.ClientSize = new System.Drawing.Size(640, 480);

      this.Paint += delegate  (object sender, PaintEventArgs e) {
        System.Drawing.StringFormat stringFormat = new System.Drawing.StringFormat();
        e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.Blue), 0, 0, 640, 480);
        e.Graphics.Clear(System.Drawing.Color.LightYellow);
        e.Graphics.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.LightPink, 10), e.ClipRectangle);
        e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.LightSteelBlue, 5), 20, 60, 260, 60);
        e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.LightBlue), 50, 300, 400, 50);
        e.Graphics.DrawLine(new System.Drawing.Pen(System.Drawing.Color.Red, 1), 0, 0, 1, 1);
        e.Graphics.DrawString("Draw string", new System.Drawing.Font("Arial", 34, System.Drawing.FontStyle.Regular), new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), 20, 0, stringFormat);
        e.Graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.LightSeaGreen), 400, 70, 100, 200);
        e.Graphics.DrawArc(new System.Drawing.Pen(System.Drawing.Color.Black, 10), 400, 70, 100, 200, 45.0f, 270.0f);
        e.Graphics.DrawEllipse(new System.Drawing.Pen(System.Drawing.Color.Red, 10), 100, 80, 200, 200);
        e.Graphics.FillPie(new System.Drawing.SolidBrush(System.Drawing.Color.Green), 120, 100, 160, 160, 45.0f, 270.0f);
        e.Graphics.FillPie(new System.Drawing.SolidBrush(System.Drawing.Color.LightGreen), 120, 100, 160, 160, 270.0f, 180.0f);
        e.Graphics.DrawBezier(new System.Drawing.Pen(System.Drawing.Color.Black), 100, 100, 150, 150, 200, 100, 250, 50);
      };
    }

    private Label label1 = new Label();
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 5: ColoredForms

This example will comprise the following files:

  • ColoredForms.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). ColoredForms.cs

Create a file named ColoredForms.cs

Here is the full code

using System;
using System.Drawing;
using System.Windows.Forms;

class Examples {
  public static void Main(string[] args) {
    Application.EnableVisualStyles();

    System.Collections.Generic.List<Form> forms = new System.Collections.Generic.List<Form>();
    KnownColor formColor = KnownColor.AliceBlue;

    Button button = new Button();
    button.Text = "Create";
    button.Location = new Point(10, 10);
    button.Click += delegate (object sender, EventArgs e) {
      Form form = new Form();
      form.BackColor = Color.FromKnownColor(formColor);
      form.Text = string.Format("{0}", formColor);
      form.Visible = true;
      forms.Add(form);
      formColor = formColor != KnownColor.YellowGreen ? (KnownColor)((int)formColor + 1) : KnownColor.AliceBlue;
    };

    Form mainForm = new Form();
    mainForm.Text = "Main Form";
    mainForm.StartPosition = FormStartPosition.Manual;
    mainForm.Location = new System.Drawing.Point(Screen.AllScreens[0].WorkingArea.Width - mainForm.Width - 20, 43);
    mainForm.Controls.Add(button);
    Application.Run(mainForm);
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 6: FormWithAssemblyInfo

This example will comprise the following files:

  • FormWithAssemblyInfo.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormWithAssemblyInfo.cs

Create a file named FormWithAssemblyInfo.cs

Here is the full code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      this.company.Text = "Company name";
      this.company.Location = new Point(10, 12);

      this.product.Text = "Product name";
      this.product.Location = new Point(10, 42);

      this.version.Text = "Product version";
      this.version.Location = new Point(10, 72);

      this.companyName.Text = Application.CompanyName;
      this.companyName.BorderStyle = BorderStyle.Fixed3D;
      this.companyName.Location = new Point(120, 10);
      this.companyName.TextAlign = ContentAlignment.MiddleLeft;
      this.companyName.Width = 170;

      this.productName.Text = Application.ProductName;
      this.productName.BorderStyle = BorderStyle.Fixed3D;
      this.productName.Location = new Point(120, 40);
      this.productName.TextAlign = ContentAlignment.MiddleLeft;
      this.productName.Width = 170;

      this.productVersion.Text = Application.ProductVersion;
      this.productVersion.BorderStyle = BorderStyle.Fixed3D;
      this.productVersion.Location = new Point(120, 70);
      this.productVersion.TextAlign = ContentAlignment.MiddleLeft;
      this.productVersion.Width = 170;

      this.Text = "Form with assembly info";
      this.Controls.AddRange(new Control[] { this.company, this.product, this.version, this.companyName, this.productName, this.productVersion });
      this.ClientSize = new System.Drawing.Size(300, 105);
    }

    private Label company = new Label();
    private Label product = new Label();
    private Label version = new Label();

    private Label companyName = new Label();
    private Label productName = new Label();
    private Label productVersion = new Label();
  };
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 7: FormWithAssemblyInfo

This example will comprise the following files:

  • FormWithAssemblyInfo.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormWithAssemblyInfo.cs

Create a file named FormWithAssemblyInfo.cs

Here is the full code

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Examples {
  class Form1 : Form {
    // The main entry point for the application.
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    public Form1() {
      this.company.Text = "Company name";
      this.company.Location = new Point(10, 12);

      this.product.Text = "Product name";
      this.product.Location = new Point(10, 42);

      this.version.Text = "Product version";
      this.version.Location = new Point(10, 72);

      this.companyName.Text = Application.CompanyName;
      this.companyName.BorderStyle = BorderStyle.Fixed3D;
      this.companyName.Location = new Point(120, 10);
      this.companyName.TextAlign = ContentAlignment.MiddleLeft;
      this.companyName.Width = 170;

      this.productName.Text = Application.ProductName;
      this.productName.BorderStyle = BorderStyle.Fixed3D;
      this.productName.Location = new Point(120, 40);
      this.productName.TextAlign = ContentAlignment.MiddleLeft;
      this.productName.Width = 170;

      this.productVersion.Text = Application.ProductVersion;
      this.productVersion.BorderStyle = BorderStyle.Fixed3D;
      this.productVersion.Location = new Point(120, 70);
      this.productVersion.TextAlign = ContentAlignment.MiddleLeft;
      this.productVersion.Width = 170;

      this.Text = "Form with assembly info";
      this.Controls.AddRange(new Control[] { this.company, this.product, this.version, this.companyName, this.productName, this.productVersion });
      this.ClientSize = new System.Drawing.Size(300, 105);
    }

    private Label company = new Label();
    private Label product = new Label();
    private Label version = new Label();

    private Label companyName = new Label();
    private Label productName = new Label();
    private Label productVersion = new Label();
  };
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Example 8: FormAndMessages

This example will comprise the following files:

  • FormAndMessages.cs

Step 1: Create Project

  1. The first step is to create a C# Project.
  2. Go to FILE-->New-->Project to create a new project.

Step 2: Write Code

Write Code as follows:

*(a). FormAndMessages.cs

Create a file named FormAndMessages.cs

Here is the full code

using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace Examples {
  class Form1 : Form {
    [DllImport("user32.dll")]
    public static extern int SendMessageW(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam);

    private const int WM_ACTIVATEAPP = 0x001C;
    private const int WM_CANCELMODE = 0x001F;
    private const int WM_CLOSE = 0x0010;
    private const int WM_COMMAND = 0x0111;
    private const int WM_CREATE = 0x0001;
    private const int WM_DESTROY = 0x0002;
    private const int WM_ENTERSIZEMOVE = 0x0231;
    private const int WM_EXITSIZEMOVE = 0x0232;
    private const int WM_GETTEXT = 0x000D;
    private const int WM_GETTEXTLENGTH = 0x000E;
    private const int WM_KEYDOWN = 0x0100;
    private const int WM_KEYUP = 0x0101;
    private const int WM_CHAR = 0x0102;
    private const int WM_KILLFOCUS = 0x0008;
    private const int WM_LBUTTONDBLCLK = 0x0203;
    private const int WM_LBUTTONDOWN = 0x0201;
    private const int WM_LBUTTONUP = 0x0202;
    private const int WM_MBUTTONDOWN = 0x0207;
    private const int WM_MBUTTONUP = 0x0208;
    private const int WM_MBUTTONDBLCLK = 0x0209;
    private const int WM_MOUSEMOVE = 0x0200;
    private const int WM_MOUSEHWHEEL = 0x020E;
    private const int WM_MOUSELEAVE = 0x02A3;
    private const int WM_MOUSEWHEEL = 0x020A;
    private const int WM_MOVE = 0x0003;
    private const int WM_RBUTTONDOWN = 0x0204;
    private const int WM_RBUTTONUP = 0x0205;
    private const int WM_RBUTTONDBLCLK = 0x0206;
    private const int WM_SETFOCUS = 0x0007;
    private const int WM_SETTEXT = 0x000C;
    private const int WM_SHOWWINDOW = 0x0018;
    private const int WM_SIZE = 0x0005;
    private const int WM_XBUTTONDOWN = 0x020B;
    private const int WM_XBUTTONUP = 0x020C;
    private const int WM_XBUTTONDBLCLK = 0x020D;

    private const int WM_USER = 0x0400;
    private const int WM_MOUSEENTER = WM_USER + 0x0001;

    public Form1() {
      Text = "Form and Messages";
      Enabled = false;
      StartPosition = FormStartPosition.CenterScreen;

      LocationChanged += delegate {
        System.Diagnostics.Debug.WriteLine(string.Format("LocationChange Location = {0}", Location));
      };

      SizeChanged += delegate {
        System.Diagnostics.Debug.WriteLine(string.Format("SizeChanged Size = {0}", Size));
      };
    }

    private int HIWORD(IntPtr l) {
      return (short)(((int)l >> 16) & 0xffff);
    }

    private int LOWORD(IntPtr l) {
      return (short)((int)l & 0xffff);
    }

    // The main entry point for the application.
    [STAThread]
    public static void Main() {
      Application.EnableVisualStyles();
      Application.Run(new Form1());
    }

    protected override void OnCreateControl() {
      //base.OnCreateControl();
      System.Diagnostics.Debug.WriteLine("OnCreateControl()");
    }

    protected override void OnHandleCreated(EventArgs e) {
      base.OnHandleCreated(e);
    }

    protected override void WndProc(ref Message message) {
      base.WndProc(ref message);

      switch(message.Msg) {
        case WM_ACTIVATEAPP: System.Diagnostics.Debug.WriteLine(string.Format("WM_ACTIVATEAPP [activate={0}, threat={1}]", message.WParam, message.LParam)); break;
        case WM_CANCELMODE: System.Diagnostics.Debug.WriteLine("WM_CANCELMODE"); break;
        case WM_CHAR: System.Diagnostics.Debug.WriteLine(string.Format("WM_CHAR [char='{0}', repeat={1}]", (char)message.WParam, message.LParam)); break;
        case WM_CLOSE: System.Diagnostics.Debug.WriteLine("WM_CLOSE"); break;
        case WM_COMMAND: System.Diagnostics.Debug.WriteLine(string.Format("WM_COMMAND [type=0x{0:X8}, Control={1}]", message.WParam, message.LParam)); break;
        case WM_CREATE: System.Diagnostics.Debug.WriteLine(string.Format("WM_CREATE [CREATESTRUCT={0}]", message.LParam)); break;
        case WM_DESTROY: System.Diagnostics.Debug.WriteLine("WM_DESTROY"); break;
        case WM_ENTERSIZEMOVE: System.Diagnostics.Debug.WriteLine("WM_ENTERSIZEMOVE"); break;
        case WM_EXITSIZEMOVE: System.Diagnostics.Debug.WriteLine("WM_EXITSIZEMOVE"); break;
        case WM_GETTEXT: System.Diagnostics.Debug.WriteLine(string.Format("WM_GETTEXT [size={0}, buffer={1}]", message.WParam, message.LParam)); break;
        case WM_GETTEXTLENGTH: System.Diagnostics.Debug.WriteLine("WM_GETTEXTLENGTH"); break;
        case WM_KEYDOWN: System.Diagnostics.Debug.WriteLine(string.Format("WM_KEYDOWN [Key={0}, repeat={1}]", (Keys)message.WParam, message.LParam)); break;
        case WM_KEYUP: System.Diagnostics.Debug.WriteLine(string.Format("WM_KEYUP [Key={0}, repeat={1}]", (Keys)message.WParam, message.LParam)); break;
        case WM_KILLFOCUS: System.Diagnostics.Debug.WriteLine("WM_KILLFOCUS"); break;
        case WM_LBUTTONDBLCLK: System.Diagnostics.Debug.WriteLine(string.Format("WM_LBUTTONDBLCLK [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_LBUTTONDOWN: System.Diagnostics.Debug.WriteLine(string.Format("WM_LBUTTONDOWN [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_LBUTTONUP: System.Diagnostics.Debug.WriteLine(string.Format("WM_LBUTTONUP [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MBUTTONDBLCLK: System.Diagnostics.Debug.WriteLine(string.Format("WM_MBUTTONDBLCLK [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MBUTTONDOWN: System.Diagnostics.Debug.WriteLine(string.Format("WM_MBUTTONDOWN [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MBUTTONUP: System.Diagnostics.Debug.WriteLine(string.Format("WM_MBUTTONUP [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MOUSEENTER: System.Diagnostics.Debug.WriteLine("WM_MOUSEENTER"); break;
        case WM_MOUSEHWHEEL: System.Diagnostics.Debug.WriteLine(string.Format("WM_MOUSEHWHEEL [Keys={0}, Delta={1} x={2}, y={3}", LOWORD(message.WParam), HIWORD(message.WParam), LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MOUSELEAVE: System.Diagnostics.Debug.WriteLine("WM_MOUSELEAVE"); break;
        case WM_MOUSEMOVE: System.Diagnostics.Debug.WriteLine(string.Format("WM_MOUSEMOVE [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MOUSEWHEEL: System.Diagnostics.Debug.WriteLine(string.Format("WM_MOUSEWHEEL [Keys={0}, Delta={1} x={2}, y={3}", LOWORD(message.WParam), HIWORD(message.WParam), LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_MOVE: System.Diagnostics.Debug.WriteLine(string.Format("WM_MOVE x = {0}, y = {1}", LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_RBUTTONDBLCLK: System.Diagnostics.Debug.WriteLine(string.Format("WM_RBUTTONDBLCLK [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_RBUTTONDOWN: System.Diagnostics.Debug.WriteLine(string.Format("WM_RBUTTONDOWN [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_RBUTTONUP: System.Diagnostics.Debug.WriteLine(string.Format("WM_RBUTTONUP [Keys={0}, x={1}, y={2}]", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        case WM_SETFOCUS: System.Diagnostics.Debug.WriteLine("WM_SETFOCUS"); break;
        case WM_SETTEXT: System.Diagnostics.Debug.WriteLine(string.Format("WM_SETTEXT text = {0}", Marshal.PtrToStringAuto(message.LParam))); break;
        case WM_SHOWWINDOW: System.Diagnostics.Debug.WriteLine(string.Format("WM_SHOWWINDOW shown = {0}", message.WParam)); break;
        case WM_SIZE: System.Diagnostics.Debug.WriteLine(string.Format("WM_SIZE type = {0}, width = {1}, heignt = {2}", message.WParam, LOWORD(message.LParam), HIWORD(message.LParam))); break;
        default: System.Diagnostics.Debug.WriteLine(string.Format("[Msg=0x{0:X4}, HWnd=0x{1:X}, WParam=0x{2:X}, LParam=0x{3:X}, Result=0x{4:X}]", message.Msg, (long)message.HWnd, (long)message.WParam, (long)message.LParam, (long)message.Result)); break;
      }
    }
  }
}

Run

Simply copy the source code into your C# Project,Build and Run. Alternatively download the code using the links provided below, then open the .csproj project, build and run.

Reference

Download the code using the below links:

Number Link
1. Download Example
2. Follow code author

Related Posts