You are on page 1of 11

Introduction to Windows Programming:creating windows forms, windows controls, menus and dialogue

boxes, overview of xml.Window programming vs.Window presentation foundation,main features of


WPF 4.5, WPF 4.5architecture, WPF 4.5class hierarchy, types of WPF applications, WPF4.5designer
interface, Using XAML in WPF 4.5 applications, WPF properties, WPF events,working with dialog boxes in
WPF application, compiling and running WPF 4.5 applications, WPF 4.5controls, resources, styles,
templates, commands

Windows Forms Basics

A Windows forms application is one that runs on the desktop computer. A Windows forms application
will normally have a collection of controls such as labels, textboxes, list boxes, etc.

Below is an example of a simple Windows form application. It shows a simple Login screen, which is
accessible by the user. The user will enter the required credentials and then will click the Login button to
proceed.

So an example of the controls available in the above application

1.This is a collection of label controls which are normally used to describe adjacent controls. So in our
case, we have 2 textboxes, and the labels are used to tell the user that one textbox is for entering the
user name and the other for the password.

2. The 2 textboxes are used to hold the username and password which will be entered by the user.

3. Finally, we have the button control. The button control will normally have some code attached to
perform a certain set of actions. So for example in the above case, we could have the button perform an
action of validating the user name and password which is entered by the user.

C# Hello World
Now let's look at an example of how we can implement a simple 'hello world' application in Visual
Studio. For this, we would need to implement the below-mentioned steps

Step 1) The first step involves the creation of a new project in Visual Studio. After launching Visual
Studio, you need to choose the menu option New->Project.

Step 2) The next step is to choose the project type as a Windows Forms application. Here we also need
to mention the name and location of our project.

In the project dialog box, we can see various options for creating different types of projects in Visual
Studio. Click the Windows option on the left-hand side.

When we click the Windows options in the previous step, we will be able to see an option for Windows
Forms Application. Click this option.
We then give a name for the application which in our case is DemoApplication. We also need to provide
a location to store our application.

Finally, we click the 'OK' button to let Visual Studio to create our project.

If the above steps are followed, you will get the below output in Visual Studio.

You will actually see a Form Designer displayed in Visual Studio. It's on this Form Designer that you will
start building your Windows Forms application.

In the Solution explorer, you will also be able to see the DemoApplication Solution. This solution
will contain the below 2 project files

A Form application called Forms1.cs. This file will contain all of the code for the Windows Form
application. The Main program called Program.cs is default code file which is created when a new
application is created in Visual Studio. This code will contain the startup code for the application as a
whole. On the right-hand side of Visual Studio, you will also see a ToolBox. The toolbox contains all the
controls which can be added to a Windows Forms. Controls like a text box or a label are just some of the
controls which can be added to a Windows Forms.Below is a screenshot of how the Toolbox looks like.

Step 3) In this step, we will now add a label to the Form which will display "Hello World."
From the toolbox , you will need to choose the Label control and simply drag it onto the Form.
Once you drag the label to the form, you can actually see the label embedded on the form as

shown below.

Step 4) The next step is to actually go to the properties of the control and Change the text to
'Hello World'.To go to the properties of control, you need to right-click the control and choose the
Properties menu option.
The properties panel also shows up in Visual Studio. So for the label control, in the properties
control, go to the Text section and enter "Hello World".Each Control has a set of properties which
describe the control.

If you follow all of the above steps and run your program in Visual Studio, you will get the
following output
Windows Presentation Foundation
WPF stands for Windows Presentation Foundation. It is a powerful framework for building Windows
applications. Windows Presentation Framework is a next generation UI framework to create applications
with a rich user experience. It is part of the .NET framework 3.0 and higher. It includes application UI, 2D
graphics, 3D graphics and multimedia. It takes advantage of hardware acceleration of modern graphic
cards. WPF makes the UI faster, scalable and resolution independent.

Features of WPF

Resolution Independence: WPF is resolution independence since all measures in WPF are logical units
not pixels. A logical unit is a 1/96 of an inch. So, with changing the screen resolution setting in WPF each
control will look same for each resolution. It is not based on Dots per inch (DPI) setting of the device.

Separation of appearance and behaviors: WPF separates the appearance of an UI from its behaviour.
The appearance is specified by XAML and behaviour is specified by a managed programming language
like C# or VB.

Built-In support for graphics and animation: WPF applications run within DirectX environment, hence it
has major support of graphics and animation capabilities. WPF has a separate set of classes that are
specifically deal with animation effects and graphics.

Supports for Audio and Video: WPF has support for playing any audio or video file supported by
Windows Media Player. It also gives you the tools to integrate video content into your rich UI such as
placing a video window on a spinning 3-D cube.

Highly customizable WPF supports separation of appearance and behaviours; hence you can easily
change the look of a control or a set of controls.

Architecture of WPF
Managed Layer

Managed layer has two main components Presentation Framework and Presentation Core.

Presentation Framework provides the required functionalities that we need to build the WPF applications
such as controls, data bindings, styling, shapes, media, documents, annotations, animation and more.
PresentationFamework.dll is responsible for this purpose.

Presentation Core acts as a managed wrapper around MILCore and provides public interface for MIL.
Presentation Core is the home for WPF Visual System and provides classes for creating application visual
tree. The Visual System creates visual tree which contains applications Visual Elements and rendering
instructions. PresentationCore.dll is responsible for this purpose.

Unmanaged Layer
This layer is also called milcore or Media Integration Library Core. MilCore is written in unmanaged code
in order to enable tight integration with DirectX. DirectX engine is underlying technology used in WPF to
display all graphics, allowing for efficient hardware and software rendering. MIL has Composition System
that receives rendering instructions from Visual System and translates into data that can be understood
by DirectX to render user interface.

Core API Layer

This layer has OS core components like Kernel, User32, GDI, Device Drivers, Graphic cards etc. These
components are used by the application to access low level APIs. User32 manages memory and process
separation.

WPFs classes have a very deep inheritance hierarchy, so it can be hard to get your head wrapped
around the significance of various classes and their relationships.

These 12 classes have the following significance:

1. ObjectThe base class for all .NET classes and the only class in the figure that isnt WPF specific.

2. DispatcherObjectThe base class meant for any object that wishes to be accessed only on the thread
that created it. Most WPF classes derive from DispatcherObject and are therefore inherently thread-
unsafe.

3.DependencyObjectThe base class for any object that can support dependency properties.

4.FreezableThe base class for objects that can be frozen into a read-only state for performance
reasons. Freezables, once frozen, can be safely shared among multiple threads, unlike all other
DispatcherObjects. Frozen objects can never be unfrozen, but you can clone them to create unfrozen
copies. Most Freezables are graphics primitives such as brushes, pens, and geometries or animation
classes.

5.VisualThe base class for all objects that have their own 2D visual representation. Visuals are discussed
in depth in Chapter 15, 2D Graphics.

6.UIElementThe base class for all 2D visual objects with support for routed events, command binding,
layout, and focus. These features are discussed in Chapter 5, Layout with Panels, and Chapter 6, Input
Events: Keyboard, Mouse, Stylus, and Touch.

7.Visual3DThe base class for all objects that have their own 3D visual representation. Visual3Ds are
discussed in depth in Chapter 16, 3D Graphics.

8.UIElement3DThe base class for all 3D visual objects with support for routed events, command
binding, and focus.
9.ContentElementA base class similar to UIElement but for document-related pieces of content that
dont have rendering behavior on their own. Instead, ContentElements are hosted in a Visual-derived class
to be rendered on the screen.

10.FrameworkElementThe base class that adds support for styles, data binding, resources, and a few
common mechanisms for controls, such as tooltips and context menus.

11.FrameworkContentElementThe analog to FrameworkElement for content. Chapter 11, Images,


Text, and Other Controls, examines the FrameworkContentElements in WPF.

12.Control The base class for familiar controls such as Button, ListBox, and StatusBar. Control adds many
properties to its FrameworkElement base class, such as Foreground, Background, and FontSize, as well as
the ability to be completely restyled. Part III, Controls, examines WPFs controls in depth.

What is the Dialog box explain diff dialog boxes?

Popup child windows created by Windows Used for special-purpose input & output A principal I/O
mechanism in Windows Contain several child window controls Layout & what it does is are predefined
In .NET theyre just another Form Derived from class Form We can design our own dialog boxes Five
predefined Common Dialog Boxes

Types of Dialog Boxes

1.Modal 2. Modeless

A dialog box that temporarily halts the application and the user cannot continue until the dialog has
been closed is called modal dialog box. The application may require some additional information before
it can continue or may simply wish to confirm that the user wants to proceed with a potentially
dangerous course of action. The application continues to execute only after the dialog box is closed;

Another type of dialog box, which is used is a modeless dialog box. It is used when the requested
information is not essential to continue, so the Window can be left open, while work continues
somewhere else. For example, when working in a text editor, the user wants to find and replace a
particular word.

Common Dialog Box

The dialog boxes that are used, which are common to all Windows Application. It performs the common
tasks like saving a file, choosing a font etc. This provides a standard way to the Application interface.

The examples are given below.

FontDialog ,ColorDialog ,OpenDialog,,SaveDialog


What is the XAML

XAML stands for Extensible Application Markup Language. Its a simple and declarative language based
on XML.

In XAML, it very easy to create, initialize, and set properties of objects with hierarchical relations.

It is mainly used for designing GUIs, however it can be used for other purposes as well, e.g., to declare
workflow in Workflow Foundation.

You might also like