Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in display of WPF and Windows Forms applications

Tags:

winforms

wpf

Actually, I have started learning WPF. I have few months of experience in developing Windows Forms applications. Though, I am getting the meaning of a WPF application, but still I am not able to differentiate the difference between two, on the basis of their output.

With reference to this Link: Device Independent Pixel (DPI), I have learnt that whenever the operating system render a WPF application it manages its size itself according to its resolution.

So to check this difference, I created two demo applications in both frameworks and changed the resolutions as well.. but I didn't find any satisfactory difference. Which could explain it is a WPF application and this one is a Windows Forms application.

It does not create any scroll bar on maximizing and doesn't make the button big or small on changing the resolution.

I have read somewhere that Visual Studio 2010 has been rewritten in WPF. But in my experimentation I saw that, (on changing the resolution of desktop) it makes text and graphics unreadable/blurry. On re-sizing its window, everything was getting hidden except the menu-bar. And the menu-bar content was shifting its positioning, e.g. far right one menu items were shifting down. Why?

Kindly make me correct & explain a little more bit (this display issue) too.

like image 587
user2804762 Avatar asked Sep 22 '13 18:09

user2804762


People also ask

How do I know if my app is WPF or WinForms?

If it only references namespaces beneath System. Windows other than System. Windows. Forms , then it's WPF.

What is difference between WPF page and window?

Window is the root control that must be used to hold/host other controls (e.g. Button) as container. Page is a control which can be hosted in other container controls like NavigationWindow or Frame. Page control has its own goal to serve like other controls (e.g. Button). Page is to create browser like applications.

Should I use WPF or Windows Forms?

WPF is far more flexible than Windows Forms and it is the long-term in GUI development. It's very close to HTML in how you use an XML-based layout. The best argument for Windows Forms right now is if you have a dev team with major experience using it.


2 Answers

To answer this question properly I should write a whole chapter, but I keep it short:

There are three major differences between a WPF application and a Windows Forms application: Layout, Render, Presentation


Layout:

WPF layout system provides a greater flexibility in arranging the elements on the fly. It is based on the Element Bounding Box (as opposed to precise pixels in WinForms) and Measure and Arrange mechanics (as opposed to UpdateLayout in WinForms) that automatically and dynamically finds the place for each element without any need for a manual update.

Basically, all elements bounding box are measured first and then are arranged using multiple methods such as Measure, MeasureCore, ArrangeCore, MeasureOverride, etc.

Unlike WinForms, where you have a pixel-perfect size for everything, in WPF you have much more options and complexity such as Width, ActualWidth and DesiredSize (and even Transforms as LayoutTransform) for the same element.

This is why

  • As you type in a WPF TextBox, its width might increase and push other elements away or even push some elements into a new row (like the menu bar you've observed)

  • As the size of a control changes, it affects the available space for other elements. So their size and location might change accordingly.

  • When the window is being re-sized or resolution is changed, it immediately updates the layout and changes the size of elements in order to fill or fit the space. Here you'll find out more about Layouts.

  • using Margin alone (without using layout capabilities) to arrange elements is not the best idea in WPF. As it's the WinForms mindset which isn't much helpful while developing WPF.


Render:

WPF uses double data type for Layout (as opposed to pixel-perfect WinForms) and therefore you might see the edges blurry sometimes, but it can be avoided with SnapToDevicePixels=true.

WPF is much more efficient in utilizing the GPU to render a GUI. Try a grid of 30x30 TextBoxes in a Windows Forms application and a WPF application. No matter how messy you write the WPF, it never blinks and it still runs much faster than Windows Forms. Even adding a handful of animations, visual effects and styles on them does not hurt your performance like in Windows Forms.

Remark: To avoid a speed decrease and blinking in a Windows Forms application, you should set DoubleBuffer of the form to "true".

You can use any Transform as RenderTransform to easily implement smooth zoom/rotate, or develop custom GPU-based shader effects, and much more in WPF. (I think everyone agrees that doing such things in WinForms is feasible but real pain and you most likely will give up and move to GDI+ or DX if not out of frustration then because of the bad performance.)


And the last and the most important:

Focus on presentation:

When develping WPF Applications you have to stop thinking in Windows Forms: No more UI events, accessing controls by their names and writing logic in code-behind and start to think in WPF: Binding, Commands, Resources, Styles, Templates, Converters, DependencyProperties and their callbacks.

  • The real power of WPF lies in separation of 'View' and 'Logic', Which can be achieved using the MVVM pattern.

  • It makes the most visually-complicated problems quite simple and easy to develop and easy to write Unit Tests for.

  • Once you got the hang of it, you will realize there's no limit in how you can present the data or show off an awesome GUI looks.

If you've planned to switch to WPF, you've made the right decision. Always stick to MVVM and AVOID CODE-BEHIND AT ALL COSTS! (i.e. unless you are doing a pure UI operation: do not write code in .xaml.cs files, do not access x:Name in cs files and avoid UI events.)

like image 149
Bizhan Avatar answered Oct 11 '22 06:10

Bizhan


Windows Forms (WinForms) and Windows Presentation Foundation (WPF) are two different ways of building the user interface for your application. Windows Forms is the older technology and its controls are found in the System.Windows.Forms namespace. WPF is a newer technology and its controls are found in the System.Windows.Controls namespace.

WPF

Pros:

  • Powerful styling and skinning structure
  • Easy to create your own look and feel
  • Does support Windows Forms
  • The future technology for developing Windows Vista applications
  • The ability to reuse existing code
  • Highly advanced data binding possible

Cons:

  • Declarative vs. procedural code
  • Requires .NET Framework 3.0
  • Compared to Windows Forms, still in development phase
  • Requires Dx9 compatible video card for advanced graphics

Windows Forms

Pros:

  • Extensive documentation to be found on the Internet
  • Plenty of examples
  • Does support WPF

Cons:

  • How long will this be supported? (I've read somewhere that Microsoft is just developing WPF now, only maintenance for Windows Forms).
  • Design your own look and feel in an application is a lot of work.
like image 7
Madhavan Avatar answered Oct 11 '22 07:10

Madhavan