Yes, mobile is the future of client applications, cross-platform is cool, web applications are amazing; but out there in the real world, there are still a ton of people who work all day with a Windows PC, and businesses that want PC applications in order to get their work done.
Both MFC and WinForms provide similar complete support for printing, print preview, print settings, and page settings. OLE/Active Documents. Object Linking and Embedding is a technology for exposing and consuming data and the UI from one application into another. WinForms has long been a popular way to develop form and GUI based applications, generally for Windows based devices. Part of the.NET framework, and originally seen as a replacement for Microsoft Foundation Class Library (MFC), WinForms applications are ‘event driven’.
So when a business comes to you and says, we want a new Windows application to do this or that, and presuming they do not care about mobile or Macs or access over the internet but just want something that runs on their internal network, what framework do you choose?
Let us even assume that they all run Windows 10 so that UWP (Universal Windows Platform) is a realistic option.
If you want to code in .NET (which is a great choice for a Windows-only application, and with the possibility of migrating code to cross-platform via Xamarin’s compiler later), then you have three obvious choices:
Windows Forms
This is the framework for Windows desktop applications that was introduced at the same time as .NET itself, back in 2002. Of course it has been revised many times since. There was a big update in 2006 with .NET 2.0. That said, Microsoft intended it to be replaced by Windows Presentation Foundation (WPF, see below), so it has not been a focus of attention. In 2014, High DPI support was improved, with .NET 4.5.2, reflecting the fact that this ancient framework is still widely used.
Windows Forms is a nice wrapper around the Windows API, and easy to use in that it uses essentially X Y layout. In other words, you can think of your form as a grid of pixels with the position of your controls determined at design time by its size and coordinates. This is great if you are designing and running on the same PC, but not so good when you deploy to other PCs with different display settings. It does kind-of scale if you follow certain rules, but successful scaling in a Windows Forms application is often difficult to achieve, so users may suffer chopped-off controls and text, or just ugly screens. Read this carefully if you use Windows Forms. And then read about High DPI support, which was improved again in .NET Framework 4.7.
If you are writing a database application, you can generate datasets by drag and drop from the Server Explorer in Visual Studio and bind them to controls. I am not a fan of this database framework, which quickly gets convoluted, but you do not have to use it. However the ability to bind list and grid controls to any kind of .NET collection is fantastically useful.
Why is Windows Forms still in use? It is partly legacy and the fact that it is easier to maintain and enhance an existing application than to start again. It is also because, scaling issues aside, Windows Forms is reliable, well supported by both built-in and third-party controls, and easy to learn.
Windows Presentation Foundation
This was Microsoft’s second go at a GUI framework for .NET and in many respects a great improvement. It was introduced with .NET Framework 3.0 in 2006, part of the Vista wave of technology. Unlike Windows Forms, it is based on the DirectX graphics API, so great for multimedia and special effects. Scaling is built-in and based on layout managers. The underlying presentation language is based on XAML, an XML language. As with Windows Forms, there is deep support for binding data to controls.
Why would you not always use WPF rather than Windows Forms? The main issue is that the time you save on figuring out scaling is more than consumed by the time you spend on design. WPF is a designer-centric framework. It will repay your efforts, but if you just want to slap a couple of grids and a few buttons on a form to get a working business application, Windows Forms remains tempting.
Universal Windows Platform
Both Windows Forms and WPF are old, and Microsoft is pointing developers towards its Universal Windows Platform (UWP) instead. UWP is an evolution of the new application platform introduced in Windows 8 in 2012. If WPF was all about scaling and multimedia, the Windows 8 modern app platform is about touch support and Store-based deployment. The application model was also service based, the idea being that your app consumes services published over the internet. Until the Windows 10 Fall Creators Update, you could not use the .NET SQLClient to connect directly to a SQL Server database (you can now). The app platform became UWP with the launch of Windows 10 in 2015. UWP can use XAML for layout design, but it is not compatible with WPF.
Personally I have mixed feelings about UWP. Unfortunately it has suffered from Microsoft’s ever-changing development strategy. The Windows 8 app platform made sense to me as a way of bringing Windows into the tablet era and enabling applications that were more secure and more easily deployed, even if it tended to result in applications that were blocky and ugly. Microsoft then changed its mind about full-screen touch applications and came up with the UWP for Windows 10, where applications again run in a window, but with a new selling point: you could run your application on Windows Phone as well as desktop. Then the company canned Windows Phone, before UWP had properly launched, in effect deleting the “Universal” part of the platform.
UWP still offers Store delivery and isolation from other applications, better for security and stability. However there are a few things against it. First, users require Windows 10. Second, like WPF it is a designer-centric platform and not so good for running up quick business applications. Third, UWP apps behave differently from standard desktop applications, sometimes not in a good way.
I was using Microsoft’s bundled Photos application recently. I work a lot with images so this often pops up, as the default image viewer on Windows 10. I was not stressing it, but it crashed which, as is typical for a UWP app, means it just disappeared without any message or warning.
UWP will be three years old this summer, but I am not convinced that the platform is quite there yet. I find it hard to think of UWP apps that I love. The apps I know best are the built-in ones, Mail, Photos, Groove Music, Calculator, and I do not love any of them. Paint 3D is amazing but not my thing.
At the same time I do see the merits of UWP versus traditional Windows application deployment. The existence of the Desktop Bridge (formerly Project Centennial) means you can get many of those benefits while still using WPF or Windows Forms.
Closing thoughts
Perhaps something like Power Apps will render this discussion irrelevant before long. There are also other options for the desktop, such as Xamarin Forms if you still want to use .NET, or Electron for using web technologies for desktop applications.
Still, while it may seem surprising, even in 2018 I can think of reasons why you might use any of the above frameworks, even Windows Forms, for a business app targeting Windows.
An example of creation of dialog window in MS Visual Studio 2010 – C++
Task
1. Develop a dialog window, which contains two buttons OK and Cancel (Fig. 1).
Fig. 1. Form, which you need to create
Show the dialog window from main window of application. A corresponding message must be showed, if the one of two buttons is pressed: OK or Cancel.
Instructions
- Run MS Visual Studio 2010.
- Create the project on C++ as Dialog Based Application.
Creating a project as Dialog Based Application is described here in details.
In the window of new project creating yon need to set the name of application as “MFCApp“. In the future we will use this name.
Application folder in our case:
When you create a new project, in wizard select Dialog Based Application. All others settings leave by default. All others settings leave by default.
As a result, two classes named CMFCAppApp and CMFCAppDlg will be generated.
The window MS Visual Studio after executed actions, will has the view as shown at Figure 2.
Fig. 2. Application form after creating “Dialog Based Application“
- Changing of appearance of main form.
Let us delete the OK button. But button “Cancel” rename to “Exit“.
To delete “OK” button, first of all you need to select it, then press the button Delete.
Same way we delete element in which is written:
To rename the button Cancel, first select it, then, in window Properties in property Caption, enter word “Exit” (Fig. 3). The application is already configured to press on the “Exit” button, so that there is a way to exit of the application.
Fig. 3. Renaming of button name from “Cancel” on “Exit“
Currently, we have two elements from panel “Properties“:
- IDD_MFCAPP_DIALOG – the main window of application;
- IDCANCEL – button “Exit” (бывшая “Cancel“).
Now, you can run the application and test it.
Place on the form other button. In the window of panel “Properties” there is one more element IDC_BUTTON1.
The application form will have view, as shown in figure 4.
Fig. 4. Placing the button on the form without event handlers
Change the property “Caption” of button IDC_BUTTON1 from value “Caption” to value “Form2“. After pressing on this button the dialog window will be called.
- The stages of designing of the dialog box.
In Visual C ++, any design of dialog box consists three stages:
- forming the resources of dialog box;
- creating the class of dialog box;
- using the class of dialog box.
- Designing the class and resources of dialog box.
In MS Visual Studio, resources of dialog window are created after creating of window class automatically. So, when you created a window class, you automatically create the resource of window.
How to open aae photos on pc. To work with classes we need to use “Class Wizard“. To call “Class Wizard” we need to click the right mouse button and in the context menu select command “Class Wizard…” (Fig. 5).
Fig. 5. Calling “Class Wizard…” to creating the class and resource of dialog box
As a result, the window shown on Figure 6 will be opened.
Fig. 6. Window “MFC Class Wizard“
Window includes the following fields:
1. Field “Project:” – shows projects in solution (Solution).
2. Field “Class Name:” – it defines the classes of project. In our case there are three classes with following names:
- CMFCAppApp – class of application in general;
- CMFCAppDlg – class of dialog window of application;
- CAboutDlg – class of dialog box “About“, which were formed by wizard AppWizard.
3. Field “Base Class:” – points to the base class, from which is inherited the class, which is displayed in the field “Class Name:“.
4. Field “Resource:” defines the name of resource, which corresponds the class from field “Class Name:“.
5. Field “Class declaration:” defines the name of header file for class, which is displayed in field “Class Name:“.
6. Field “Class implementation:” defines the name of file, which realizes methods of class, which is displayed in field “Class Name:“.
Also, window consists five tabs, which for class from field “Class Name:” describes:
- commands of messages map;
- the Windows messages, which are generating in class and can be processed;
- virtual methods (functions);
- internal variables, that are described in the class;
- methods, which are in the class.
To add new class, you need to call the command “Add Class” (Fig. 7).
Fig. 7. Command to add the new class
As a result, the window “MFC Add Class Wizard” will be opened. In this window set the fields to values as shown in Figure 8. To set values of fields, you need enter text “CForm2” in the field “Class Name:“. Automatically all the other fields will be filled with the exception of field “Base class:“.
In the field “Base class:” is specified base class CDialog.
The name of resource is set to IDD_FORM2.
Header file and implementation file are named “Form2.h” and “Form2.cpp“.
Fig. 8. Creating a new class CForm2 and new resource IDD_FORM2
We do the click on button “Finish“. In the previously window you can see, that to three previously classes is added class CForm2.
We do the click on “OK” again.
- Window MS Visual Studio and panel Solution Explorer.
After performing steps, the window of panel “Solution Explorer” has the view as shown in Figure 9.
Fig. 9. Window Solution Explorer
As you see in Figure 9, there are two files “Form2.h” and “Form2.cpp” in the list of files. Accordingly, it is the header and implementation files for the newly formed class CForm2.
- Calling the dialog box of class CForm2 as resource.
To begin forming new dialog box (class CForm2) you need to call it as a resource. To do it, in Solution Explorer, you need do the double mouse click on the file “MFCApp.rc” of tab “Resources” (Fig. 10).
Fig. 10. Calling the resources names list of application
As result, we get the list of available resources:
- IDD_ABOUTBOX – resource of dialog box of About window;
- IDD_FORM2 – resource of newly created dialog box;
- IDD_MFC_APP_DIALOG – resource of main window of application.
To call a needed resource, you need do the double click by mouse. Do the double click on the resource IDD_FORM2. As a result, the newly created dialog box will be opened (Fig. 11). As seen in Figure 11, it is similar to the initial application window of type “Dialog Based Application“.
Fig. 11. The window of class CForm2, which is inherited from class CDialog
You can change the size of form and placement of buttons “OK” and “Cancel“.
- The event programming of click at the button “Form2” of main window of application.
Using “Solution Explorer” and resource file “MFCApp.rc” (see p.7) we are proceeding to resource IDD_MFCAPP_DIALOG of main window of application (Fig.12).
Fig. 12. Main window of application
In the “Properties” window we select the button with identifier IDC_BUTTON1. Then we are moving to the tab “Control Events” (Fig. 13).
Fig. 13. List of events from tab “Control Events” of control IDC_BUTTON1
Making MFC Controls Available For WinForms Through MFC ..
In the list of events we find the event named BN_CLICKED. Then we select the button of selection of drop-down menu and select “<Add> OnBnClickedButton1” (Fig. 14).
Fig. 14. The selection of event BN_CLICKED and calling the handling event OnBnClickedButton1
As a result, the window of file “MFCAppDlg.cpp” with the selected code of handling event OnBnClickedButton1 will be opened.
Between the braces { } you need to insert your own event-handling code.
The program code of function of event handling of button IDC_BUTTON1 is the following:
Web 前端开发和 Windows 桌面 Qt 开发哪个前景更好?
Mfc Winforms
Now you can run and test the application.