Hey guys,

Recently I had a requirement in which I had to run an application directly from a DVD. So I made a demo application so that I can share it with you guys.

So first of all we need to create a player inside which the video will play. So add the following code to your xaml file of the window

 

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

Now we need to add the event handler for these events in our code behind. These can easily be converted to Commands but I did not bother as I wanted to keep it simple. So we add the following code to the code behind of our window.

 

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

Now we make sure we have the media already present in our application. So we add a Media folder into our application and we add the a video named Lake.wmv to it. And sure it has properties set as shown.

 

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

Now you can build and run the application.

To run it from the DVD we need to create an ISO file which we can write to a DVD. So create an ISO file I have used Magic ISO Maker which looks something like below.

 

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

I then browse to the bin folder of my application and drag all the files and folder in it on to the blank area above and create the ISO.

Now we can write this ISO to a DVD or just run it using a virtual drive.

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

We can browse the DVD and run the application.

 

WPF Video APP from CD / DVD

WPF Video APP from CD / DVD

The source and ISO can be downloaded here.

 

Any questions, comments or feedback is highly appreciated.

Hey guys,

I am sharing my latest application which is a address book developed in WPF and C# using MVVM pattern.

Address Book in WPF C# MVVM

Address Book in WPF C# MVVM

Following are the features of the application.

  1. You can save data to the data base local to application. None of the other applications are required.
  2. Functionality to import or export excel database
  3. Needs only .NET Framework to run.

You can download the Source code and the executeable below.

Download

Feel free post any questions, comments or feedback.

 

Hey Guys,

A couple of times a faced an issue while working on code. I wanted to check which files I edited since yesterday or since a specific date as in a folder, so i developed an application which which will find the modified files in the selected folder after the specified date and time.

 

Below you can find the installer. Source Code and Executeable for the

 

Download the source code for ModifiedFilesFinder 1.0 @OneDrive or @Google Drive
Download the installer for ModifiedFilesFinder 1.0 @OneDrive or @Google Drive

To run the application you need to download the files above.

  • Go to the folder
ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

  • Run the Setup.exe
ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

  • Click on install and after the installtion completes the application will open.
ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

 

ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

  • Once the application scans the selected folder it will prompt you to select the location for the text files which has th elist of all the modified files.
ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

  • To remove the application you need to uninstall the application form control Panel. So go to Uninstall programs and find the program by name FindModifiedFiles and uninstall it.
ModifiedFilesFinder 1.0

ModifiedFilesFinder 1.0

 

Any feedback, comments or questions are most welcomw.

 

What do we do and what does IDE do

The C# program can do whatever you want it to but it will take much more than just dragging and dropping the controls in visual studio. Visual studio does provide you with the code but that is just the start point for our application and we need to write C# code for our application to perform the task we want it to do. Let’s see what Visual Studio can do for us.

  • When we create a new Windows Forms Project in visual Studio, it adds an empty form to the project. What IDE does here is creates the files and folder for the project which are created from a predefined template that contains the basic code to create and display a form.
  • When we drag and drop a button onto the form we see some of the C# code for the button property is automatically added by visual studio. What IDE does here is add the code to Form1.Designer.cs which adds the button to the form. When we double click the button then the code to handle the click of the button is added to this file.
  • Visual Studio presents us with the property window in the IDE that we can use to set the various properties of the button. What IDE does here is update these values into the Form1.Designer.cs.

Where programs come from

Actually a C# program just starts out as a few lines of code in a couple of files and finally it runs on a computer. The following steps if how we do it.

  • Program start out as source code – IDE is nothing more just a program that helps us creating, editing and managing our code files and resources. It bundles all these files into a solution by creating a .sln file and a folder that contain other files. The solution file has a list of project files (i.e. .csproj) in the project. The project file contains the files associated with the program.
  • .NET Framework Tools for programming – All the controls that we use in our code are provided as provided as part of the .NET framework. Also there is lot of underlying code for default functionalities like the maximize and the minimize buttons in a button. So C# is just a language using which we access all these controls and their existing functionalities and write new functionalities. The .NET framework is divided into the namespaces which we have already seen. We include the part of .NET framework we need in our application by specifying the using keyword at the top of our code file. Ex – using System.Windows
  • Build the program to create an assembly – When we build our application by using the Build Solution or Build Project option then the IDE runs the C# compiler which compiles our project into a exe or a dll depending on the project type we have selected (class library compiles into a dll and applications compile into exe). We can use this exe to run the program and when press F5 to debug our application then IDE runs this exe and provides additional options to debug the program i.e. pause or add breakpoint to a program.
  • CLR runs the program inside itself – When we run the exe then CLR (Common Language Runtime) kicks in and acts as the layer between our program and Windows. Before C# in your program itself we had to deal with the complexity of communicating with the hardware and low level stuff but with .NET this taken care by CLR. CLR does the translation of our code to machine code so that computer understands it.

How Does the IDE helps us

  • Solution Explorer – It shows us everything that is present in our project. This shows us all the files, folder, references and resources in our project.
Visual Studio Solution Explorer

Visual Studio Solution Explorer

  • Tabbed View – The IDE provides a tabbed view for accessing and editing the various files simultaneously. We can use Ctrl + Tab to switch between the opened tabs in Visual Studio.
Visual Studio Tabbed View

Visual Studio Tabbed View

  • IDE helps write code – The Visual Studio IDE has a feature known as IntelliSence that helps developers writing the code. While we are typing code in the VS IDE we can see small Windows popping up. It shows us the various options we can select.
Visual Studio Intellisence

Visual Studio Intellisence

It also provides the various inbuilt code snippets to auto type the code for us. For example if we want to add the code for a MessageBox we can type mbox and press tab twice and we will be able to see something like below.

Visual Studio Code Snippets

Visual Studio Code Snippets

 

  • IDE helps in tracing errors – When we build our code using the IDE we see the list of compiler errors along with the details and the location of the error.  Also when we debug our program the IDE firsts saves the changes in the project and then shows the compile time error if any.

Changing in IDE means changing the Code

Whenever we are changing anything in the properties window or the design the application all these changes are also reflected in the code. It might seem easier to drag drop controls onto our form but the IDE generates the code in the background.

Dissecting a Program

All the C# programs consist of namespaces, classes and methods which makes our life easier. Whenever we are writing a new class or a form the namespace need to be specified so that the code is separated from the .NET framework libraries. A Class is some code which consists of methods and properties to get the work done.

  • The first part of our program is the one that specifies the .NET classes we need in our program. This is done by using statements as shown below. We can also refer other classes as well which are present in other namespaces. If we do not want to use the using statements we can use the FullyQualified namespace for any class to use it.
Visual Studio Using Statements

Visual Studio Using Statements

  • The C# program is divided into classes. Generally most classes to one thing to keep them logically separated on the basis of what they do. So everything inside the first and last curly braces is part of Chapter02 namespace and the content between the second and second last curly braces is part of the class Form1. So to look for the content of a particular class or namespace we look for the matching curly braces. This class was created by the IDE when we asked it to create a Windows Form application for us.
Visual Studio Classes

Visual Studio Classes

  • Classes contain methods that perform the actions as specified. A method takes some input and performs some action and might produce some output called as the return value. The class we saw makes call to a method called InitializeComponent().
  • A statement is a single line of code. Like in the image above we can see we have added a statement as MessageBox.Show(“Test”); When programs calls a method then statements in that method are executed in sequence. Then method returns to where the method was called from when it either encounters a return or no more statements are available to execute.
  • All programs in C# start at one and only Main method. When we created the Windows Forms application the IDE automatically added the Program.cs file which contains the Main method. This is the entry point of this application and is the first method that will run.
C# Main Class

C# Main Class

It’s not necessary that we keep this Main method as the entry point for our application. We write another Main method in a differ class and make it as the entry point for our application.

  • We can have multiple classes in the same namespace. When a class is public all the other classes in the program can access it. A namespace can span multiple files. So can a class but we need to use the partial keyword for a class to split across multiple files.
  • A variable is something in which our program stores all the data whether it is a document, image or a message. We can declare the variable as shown below. When we declare a variable we mention its type and name. When the program is compiled the compiler check if have assigned value of correct type to the variable. The values of the variable might vary at different times in the program.
C# Variables

C# Variables

The assignment of values to the variable is done in the following way.

C# Variables

C# Variables

  • The C# program is familiar with the maths operators like +, -, *, /, etc. Let’s see what different combination of these means
    • = means assignment of a value to a variable
    • += means adding the value to the variable and then assigning it to the same variable. -=, *= and /= have similar meanings as you might guess.
    • ++ means incrementing the variable by 1 and – means decrementing the variable by 1.
    • != means not equal to.
    • == means comparison
    • We can use the debugger to see the values of the various variables at runtime by placing a watch on the variable on whose value we are interested in. So if you want see the value of variable sin a line of code while debugging the code we can do that by placing a breakpoint at that location by pressing F9 and remove it by pressing F9 again.
    • Loop will perform an action over and over. Different loop in C# are
      • For – As we can see in the example below the action inside the for loop will be performed till the evaluation of i<10. We need to be really careful while using loops because if the condition becomes false the loop might run indefinitely.

Example

for (int I = 0; i< 10; i++)

{

//Action to be performed

}

  • While – As we can see in the example below the operation between the parenthesis will be performed over and over again till the evaluation of i>5 in true.

Example

While (i>5)

{

I = I -1;

}

  • If/else statements makes the decisions. As we can see in the example below the MessageBox will show the value if the variable yourValue is 3. The == sign that we see is for the evaluation and not assignment.

Example

If( yourValue == 3)

{

MessageBox.Show(“You were right”);

}

 

 

WPF being a fairly new technology might need to communicate with the components that are made in other technologies so in this section we will have a look as to how we can communicate with other systems that are built in Windows Forms or Activex.

Mixed UI Technologies

So if our WPF application needs to use controls that were developed earlier or just merely in different technology or we need to integrate with legacy plug ins or maybe there is some stuff which is available in WPF or we are moving our application to WPF but module by module. In all these cases our application will be hybrid of WPF and other applications.

WPF and HWNDs

The only major issue in WPF is that WPF is designed differently from Win32. The major difference is in the composition of the Window i.e how different elements combine to form a Window. In a classic Win32 app the composition works by partitioning the window into different elements. It UI elements gets the ownership of the window. This portioning is tied to the Window Handle or HWND. HWND is the type that represents Window handle in User 32. So each Win32 control has its own HWND owns all the pixels in some region of the screen, whereas in WPF the composition works in different way. WPF treats the entire window as one big canvas and all the components get to paint on that. So the individual WPF components don’t get to won a region individually. SO WPF creates just one HWND to fill the entire window. If we run the tool designed to explore the window handle then we can see that WPF window has one HWND.

As you see in the image below I have run Microsoft Spy++ and as you could see the WPFInteropSample application has a HWND wrapper. So we can see that WPF has just one big HWND for the application. However Pop ups have their own HWND as they are separate top level windows.

 

WPF and HWNDs

WPF and HWNDs

Interop Not Free

You might think that using the existing components might be cheaper but that’s not the case with interop in the long run.

  • The development cost if more as you are using a mixture of technologies.
  • There might be design compromises due the limitation of using multiple technologies in application.
  • During the runtime the application memory usage will increase by using both Windows Forms and WPF in one application.
  • There can CPU costs as well.
  • Also the WPF and Winforms control only almost look alike and the difference becomes more obvious

So we should use hybrid applications with discretion.

Limitations of Interop

When WPF was being designed it was not planned to be run on older version of windows bu after lot a demand Microsoft changed that and made it available to run on Windows Server 2003 and Windows XP which was great. But to get this accomplished there were changes that were made and those led to a few limitations. The major issues are as below:

Airspace issue

This is the major issue. Just like two aircrafts cannot occupy the same airspace similarly two different technologies cannot occupy the same pixel space. The WPF window needs to partitioned so that each area can be used by WPF, Win32 or Directx. We cannot have more than one technologies rendering on the same pixel.

The directx limitation might seem strange as we know that WPF uses directx but the reason is that WPF does not want to share its directx services. In .NET 3.5 SP1 we can take the output of directx and use it as a brush in WPF. So it is possible to get the directx output into a region of the window. But still the control is with WPF and the HWND control handled by WPF

Example

Transforms

The transforms applied in WPF will not apply to other technologies even though the component of other technology may be a child of WPF content.

Events

Event bubbling does not work across technology boundaries either.

Multi-level hybrid Nesting

There are limitations on how deeply we can nest the content. We cannot host a Windows Forms Controls inside a WPF Control  that is hosted inside a Windows forms controls that is the child of the WPF window.

Airspace Issues in WPF

  • Clipping
  • Z-Order
  • Animated Objects
  • Opacity
  • Pseudo Transparency

Multiple Top Level Windows

A simple solution to all the airspace problem is to use multiple top level windows as all the airspace issues occur when Win32 and WPF components share the same window. Airspace limitations do not apply to the desktop level. So a simple solution to the Z-order problem is to put all the content that needs to go on top in a different window. This is the same way in which pop up works. It shows up on top of all the other windows and content. We can make use of layered windows introduced by windows 2000 as after that it has been possible for multiple windows to share pixel screen. Also the WPF application can make use of transparent windows (Window.AllowsTransparency).

Input Differences

Another issue in WPF interop is handling the user input as event bubbling and tunneling is tied into WPF visual tree. So if the mouse goes into the other technology elements then events won’t bubble out of it. So the properties like IsMouseOver, IsFocusWithin, etc will not return what we are looking for.

Interop Combinations

The following table show how easy or difficult the various Interop combinations are. The easiest interop to do is with Windows forms. It is slightly harder to interop with Win32 components because we are dealing with C++ code. Hosting an activeX control inside WPF is easy but vice versa is difficult. If we want to host WPF content inside HTML then it’s easy but user restrictions are applied to WPF xbap inside the browser.

Technology WPF   Inside WPF   Outside
Windows   Forms Easy Easy
Win32 Fairly Easy Fairly Easy
ActiveX Hard Easy
HTML Easy (With Limitations) Easy

 

WPF in Win32

The most important thing to do is to host the WPF content inside the Win32 because without this we would not be able to display the WPF content at all. This service is provided by the HwndSource class as it connects the WPF visual tree to an Hwnd. We can do this as shown in the code below. We just need to pass the parameters inside the constructor and then set the WPF visual tree as the root visual.

 

WPF in Win32

WPF in Win32

All the WPF content uses an HwndSource and we can retrieve this by the following code. We can get hold of the Hwdn in which the WPF content is displayed and this is useful as Win32 components require us to know Hwnd.

 

WPF in Win32

WPF in Win32

Win32 in WPF

To host the Win32 content inside the WPF we can use HwnHost and derive from it. Then overriding the methods to create, destroy window and for handling its windows messages.

 

Win32 in WPF

Win32 in WPF

HwndHost Keyboard Handling

HwndHost host has some optional overrides for dealing with keyboard inputs. If we want the keyboard accelerators to work perfectly whether inside WPF content or Win32 content then we need to override these methods.

  • TranslateAccelerator
  • TabInto
  • OnMnemonic

Message Pumps

If we are hosting WPF content in a Win32 application then we need to modify the Message Loop to provide the WPF Dispatcher with the opportunity it needs to manage the window messages. The Class ComponenetDispatcher that offers various static methods that we should call for message handling when a message arrives or when it’s going idle. Also WPF needs to know when you have entered the model state so that WPF can stop responding to UI inputs.

Note that we don’t need to do this when we are integrating Windows forms and WPF. WPF and Windows Forms collaborate to provide our application with message handling.

WPF inside Windows Forms

In the code below we can see WPF content inside a Windows Forms container. There is a class called ElementHost that derives from WindowsFormsControl class so that we can edit any windows forms user interface. Buts cild property is of type FramworkElement that allows us to host any WPF element.

Create a WPF Control Library and add the following code to the UserControl.

 

WPF inside Windows Forms

WPF inside Windows Forms

Now add a Windows Forms application to the solution and refer this control library in that project.

 

WPF inside Windows Forms

WPF inside Windows Forms

When we select the main form we see that theUserControl1 is available for us to use.

 

WPF inside Windows Forms

WPF inside Windows Forms

Just drag it on to the windows form and run the application and we can see the WPF control inside the Windows Forms application.

 

WPF inside Windows Forms

WPF inside Windows Forms

Windows Form inside WPF

Hosting Windows forms content inside a WPF application is almost identical. Instead of ElementHost we have WindowsFormsHost and this derives from framework element and we can add it to any WPF UI and its child property accepts a Windows Forms Control.

 

Windows Form inside WPF

Windows Form inside WPF

Windows Forms and Layout

While using the ElementHost or WindowFormsHost WPF does its best to bridge between the two layout engines and overall it works fine (Docking, Anchoring and Auto-Sizing). Even the scaling transform works for WindowsForm inside WPF.

Ambient Property in Windows Forms

These are like WPF inherited properties. Windows forms uses these to make sure that the foreground color, background color and mouse cursor are propagated to child controls.

WindowsFormHost and ElementHost both have a PropertyMap which they use to manage the transition back and forth between the inherited properties and Ambient properties and it works fine most of times.

If want to modify this then we can register a callback that will be called while propagation of any property across the interop boundary.

 

Any questions, comments or feedback are most welcome.

The code for this post can be found here.