C# WPF without XAML – Window Class


This entry is part 3 of 18 in the series C# WPF

This post discusses creating a Windows program using WPF without using XAML. Instead, you can use C# code only. XAML maps to C#, which of course maps to .NET. Here is the code for a very simple program and the running program (after the window has been resized to a smaller size for easier viewing on this web site).

For this first program, you won’t use Visual Studio’s WPF Application template but will instead use the Console Application template. This will allow you to build a bare-bones WPF program without the distractions of XAML and multiple source files.

namespace MySimpleProgramConsole
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            string myStrMessage = "Hi there!\nGood Day.\n";
            Window myWin = new Window(); // Create the Window object.
            myWin.Title = "My Simple Window"; // Set the title.
            myWin.Content = myStrMessage; // Set the window content.
            
            Application myApp = new Application(); // Create an Application object.
            myApp.Run(myWin); // Start application running.
        }
    }
}

MySimpleWindow
Even if you know your program won’t use COM, you must include the STAThread attribute on the Main method, as shown in the following code. Without it, you’ll get a compile error.
In the Solution Explorer, right-clcik the project and select properties to open the window below. Set the Output type to Windows Application. Don’t worry about the fact that it shows MySimpleProgramConsole2 instead of MySimpleProgramConsole. This is because I went through these steps a second time for the purpose of writing this post.
VSProjectProp
The next step is to add three project references: PresentationCore, PresentationFramework, and WindowsBase. To do this right-click References under your project in the Solution Explorer and then click Add Reference… in the popup to get the following window. Click the three references mentioned above and then OK.
VSReferences
Modify the code in the Program.cs window in the following three ways:
• Replace the three using statements referencing System.Collections.Generic, System.Linq, and System.Text with a using statement for System.Windows. (It wouldn’t actually hurt to leave them in the code, but I want to pare the program down to its essentials.)
• Add the [STAThread] attribute before the Main method.
• Add the above code to the body of method Main (please see the code listing at the top of this post)

The Window Class

Now we take a leap forward with our code and create our own class that inherits from the Window class to set some properties. Here is the new code. I just changed it from the above code.

using System.Windows;

namespace MySimpleProgramConsole2
{
    class MyWindow : Window
    {  // Declare a class that derives from the Window class
        public MyWindow()  { // Constructor
            Width = 300;
            Height = 100;
            Title = "My Simple Window";
            Content = "Hi There!";
        }
    }
    class Program
    {
        [STAThread]
        static void Main(string[] args)  {
            Window myWin = new MyWindow(); // Create the Window object.
            // ToolWindow does not have any max or min buttons.
            myWin.WindowStyle = WindowStyle.ToolWindow;
            myWin.Content += "\nHow are you?"; // add more content
            myWin.Show();
            Application myApp = new Application(); // Create an Application object.
            myApp.Run(); // Start application running.
        }
    }
}

Background Colour

The text is considered the foreground of the window, and the space around it is called the background. Unless you specify otherwise, the foreground is black, and the background of the window is painted white. The foreground and background of a window are each painted by a Brush. Which brush to use in each case is stored in the window’s Foreground and Background properties. A brush is a graphic that can be used to paint an area. There are six types of brushes, which fall into the three categories shown here:
> A SolidColorBrush paints the surface using a single color.
> A GradientBrush starts its stroke painting one color and transitions to another color by the end of the stroke. There are two types of gradient brush—LinearGradientBrush and RadialGradientBrush.
> A TileBrush paints an area with an image. There are three kinds of TileBrushes, which differ in the types of image used. They are ImageBrush, DrawingBrush, and VisualBrush. Below is some of the code to set the background colour to Blue.

using System.Windows.Media;

...
            myWin.Background = Brushes.Blue;
            myWin.Show();
...

Custom Colours

The Brushes class returns preconstructed SolidColorBrush objects that can’t be changed. You might, for example, want a color that isn’t included in the standard 141 colors, or you might want to be able to change the color of the brush. In these cases, you can create your own SolidColorBrush objects.
BrushClasses
• Brushes and SolidColorBrush are both concrete classes. Brush, however, is the abstract class from which SolidColorBrush derives.
• The properties of class Brushes return objects of type SolidColorBrush—not objects of type Brush—since there can be no objects of type Brush. A more accurate name for class Brushes would have been SolidColorBrushes—but that’s not the way it is.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace MySimpleProgramConsole2
{
    class MyWindow : Window
    {  // Declare a class that derives from the Window class
        public MyWindow()  { // Constructor
            Width = 400;
            Height = 100;
            Title = "My Custom Colour in the Background";
            Content = "The hex value of the color:";
        }
    }
    class Program
    {
        [STAThread]
        static void Main(string[] args)  {
            Window myWin = new MyWindow(); // Create the Window object.

            Color MyColor = new Color();
            // Set the properties to values between 0 and 255.
            MyColor.A = 255; // 0xFF  opacity; 0 is transparent
            MyColor.R = 170; // 0x64  red
            MyColor.G = 200; // 0x96  green
            MyColor.B = 250; // 0xC8  blue
            SolidColorBrush scb = new SolidColorBrush(MyColor);
            myWin.Background = scb;
            myWin.Content = myWin.Content + "\n" + scb.ToString();
            myWin.Content = myWin.Content + "\n R=" + MyColor.R.ToString() +
                " G=" + MyColor.G.ToString() + " B=" + MyColor.B.ToString();

            myWin.Show();
            Application myApp = new Application(); // Create an Application object.
            myApp.Run(); // Start application running.
        }
    }
}

MySimpleWindColor

Series Navigation<< C# WPF First ProgramC# WPF First Window Introduction >>