C# Windows Forms Screen Examples

Let us look at a simple C# Windows Forms Screen example.

Example 1: Screen

This example will comprise the following files:

  • Screen.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). Screen.cs

Create a file named Screen.cs

We will start by instantiating a Rectangle. This will store 4 integers representing the corners of a location

      System.Drawing.Rectangle area = Screen.PrimaryScreen.WorkingArea;

Let us initialize a new instance of Windows Form class

```csharp
      Form form1 = new Form();

Then specify the initial start position of this form, as well as Location, Size, Text and Visibility

      form1.StartPosition = FormStartPosition.Manual;
      form1.Location = area.Location;
      form1.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form1.Text = "screen 1/4";
      form1.Visible = true;

We will then create our second Form:

      Form form2 = new Form();
      form2.StartPosition = FormStartPosition.Manual;
      form2.Location = new System.Drawing.Point(area.Location.X + area.Size.Width / 2, area.Location.Y);
      form2.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form2.Text = "screen 2/4";
      form2.Visible = true;
        //... and so on

Here is the full code

using System;
using System.Windows.Forms;

namespace Examples {
  class Program {
    public static void Main() {
      System.Drawing.Rectangle area = Screen.PrimaryScreen.WorkingArea;

      Form form1 = new Form();
      form1.StartPosition = FormStartPosition.Manual;
      form1.Location = area.Location;
      form1.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form1.Text = "screen 1/4";
      form1.Visible = true;

      Form form2 = new Form();
      form2.StartPosition = FormStartPosition.Manual;
      form2.Location = new System.Drawing.Point(area.Location.X + area.Size.Width / 2, area.Location.Y);
      form2.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form2.Text = "screen 2/4";
      form2.Visible = true;
        //... and so on

      Form form3 = new Form();
      form3.StartPosition = FormStartPosition.Manual;
      form3.Location = new System.Drawing.Point(area.Location.X, area.Location.Y + area.Size.Height / 2);
      form3.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form3.Text = "screen 3/4";
      form3.Visible = true;

      Form form4 = new Form();
      form4.StartPosition = FormStartPosition.Manual;
      form4.Location = new System.Drawing.Point(area.Location.X + area.Size.Width / 2, area.Location.Y + area.Size.Height / 2);
      form4.Size = new System.Drawing.Size(area.Size.Width / 2, area.Size.Height / 2);
      form4.Text = "screen 4/4";
      form4.Visible = true;

      Application.EnableVisualStyles();
      Application.Run(form1);
    }
  }
}

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