About C#, Windows Forms & WPF
I have to admit that my interest in programming has “changed” lately. This doesn’t imply that I was bored with learning new programming techniques; actually, the opposite was true. The “change” means a shift towards a new programming language. As some of you might suspect from the introduction of the CodeEval post series, the new programming language that I am talking about is C#.
I can “hear” you asking: Does this decision implies no more VBA-related posts? Well, the answer is NO! I am using Office every day, and I have developed several custom functions/subs (many of which have not been published on this site), so I see no reason to leave behind VBA development – at least concerning Office applications. However, this site’s “programming side” may be enriched with some posts about C#
Sorry for this “irrelevant” introduction, but I think I had to explain why a post like this exists in this blog. In this post, I have summarized the key differences between Windows Presentation Foundation (WPF) – which provides developers with a unified programming model for building rich Windows smart client user experiences that incorporate UI, media, and documents – and the “old/classic” Windows Forms.
Windows Forms are built on User32 (older: User) and GDI+ (older: GDI) APIs, whereas WPF is built on DirectX API
- DirectX can be used for games (motion), whereas User32 and GDI+ not. Why? Because User32 and GDI+ render via CPU, whereas DirectX renders via GPU (video card).
- 3D graphics, motion, transparency of windows, etc., are some of the characteristics that can be built with WPF, contrary to the old grey-scale windows of Windows Forms.
- WPF provides a more compelling user interface.
- WPF and GPU hardware acceleration: on the newer windows Vista/7/8/10, more can be done than on XP.
- WPF checks the machine’s hardware and software drivers that will be utilized to achieve optimal performance.
- Old computer/less powerful system? No problem! WPF can use the CPU instead. Moreover, WPF can downgrade the motion, transparency, etc., to run in older computers (check RenderCapability.Tier, for example).
WPF uses XAML
- XAML is built on top of the WPF API.
- XAML is an XML type of language and is a familiar paradigm for graphic designers.
- XAML tools: Expression Blend (from 2012, part of VS). It generates XAML and can open a VS project. The project can be opened simultaneously on VS and Expression Blend.
- On WPF, both the graphic designer (front end) and the classic C#/VB.NET developer (back end) can work separately but efficiently.
- With the Expression Design, you can create Assets, which can be orchestrated (motion, etc.) in Expression Blend, and, finally, handle the various events in Visual Studio.
WPF provides a new navigational project type
- Navigate back and forth, similar to a browser (difficult to be done in Windows Forms).
The layout of user interface elements
- Windows Form controls have issues regarding a) dynamic loading, b) localization (i.e. different versions for different languages), and c) their ability to be adjustable for people with disabilities (i.e. magnification of the button font).
- WPF’s default layout is flowing; WPF can reshape, resize and reposition controls relative to other controls (similar to HTML).
- So, WPF provides a new way of thinking about layout control for Windows developers.
WPF can use both audio and video
- In Windows Forms, the implementation of the audio/video was somehow an afterthought, whereas, on WPF, complete implementation of audio and video is feasible.
Windows Forms have no real notion of animation
- In Windows Forms, you could use C++ and GDI+ or DirectX directly, use timer control, etc. The outcome is that with Windows Forms, the animation is quite difficult and clunky.
- Animations in WPF feature Storyboards, like timelines.
Themes and skins
- In Windows Forms, the colors, and, in general, the ability to create custom themes for your forms/buttons, etc., is quite difficult.
- With WPF, you can scheme an application with the use of styles and templates.
- Video resolution independence.
- Better control and usage of text styles.
- New application deployment options.
- The ability to create documents.
- Better printing.
- From WPF, you can create Silverlight applications.
- It requires a lot more time to master (it’s a large API).
- It needs more computing resources than Windows Forms, so in old PCs, there might be issues.
- A professional graphic designer (for an aesthetically pleasing user interface) is probably required.
- Users might not be ready to accept a different user interface (i.e. with different colors etc.).
- There is less third party support (i.e. controls) than Win Forms.
- It cannot run on Windows 98.
As a general conclusion, there is no tremendous difference in respect to functionality. WPF delivers similar functionality with Windows Forms but with more “style”. Usually, the application specifications will somehow drive you to the most appropriate platform. However, this decision might be difficult due to many remarkable GUI toolkits (DevExpress, Telerik, etc.), which work on both platforms.