Developing Windows Applications in C++: Taking the next steps
Once you know how to structure a Windows application, you have a tremendous amount of functionality waiting to be discovered and brought into your application. Some is available from Windows itself, others as platform-independent libraries. In this chapter, a wide variety of capabilities are presented, along with links to the documentation so that you can start to add those capabilities to your application.
DirectX, Direct2D, DirectWrite, and Direct3D
As you saw in Chapter 4, Typical Windows Tasks, you can build your user interface by drawing directly on the window area, by adding controls, or by combining the two. Hilo is a sample application that builds a complex user interface from simple starting points. The code and documentation are available from https://code.msdn.microsoft.com/Hilo.
Developing Windows Applications in C++
Reading Hilo’s code will show you how to build a complex user interface and leverage high-powered graphics capabilities in Windows 7. It might inspire your own user interface – and supply some of the code you need to implement it.
To learn more about DirectX technologies, try these links:
- Introducing Direct2D by Kenny Kerr in MSDN Magazine, Geometries Overview on MSDN
- DirectWrite on MSDN
- Getting Started with Direct3D on MSDN
Hilo also features animation – there is a carousel effect for the folders at the top of the screenshot above, for example. The Windows Animation Manager is the technology that provides easy access to smooth animation in your applications. It’s exposed through COM. You can learn more about it, including how it is used in Hilo, in Chapter 7: Using Windows Animation Manager.
Web Services are a cross-platform standard. They can be built in Java, C#, VB.NET, PHP, or many other languages, and installed on almost any server platform. On Windows, the first toolset to make it easy to write web services was for ASP.NET. Later, WCF implemented a variety of standards-based Web Services. These are managed-code technologies, suitable for use from C# or VB.NET. From native C++, you should use Windows Web Services, an all-native implementation of the Web Services technology. The Windows Web Services API is part of Windows 7 (and Windows Server 2008 R2) so there’s nothing extra for you to deploy or install when you use it. To learn more about WWS, start at Windows Web Services API. This material covers both creating a client (something that calls a web service) and a service (that is called by other code.) For an example of code that consumes a web service, you can also check Hilo, which uses a web service to publish photos to Flickr.
The File System
C++ developers have a variety of techniques available to work with files. You could use the CRT functions like fopen, fwrite, and fclose. Or, you could use C++ streams and the >> and << operators. These techniques are the same on any platform and rely on platform-specific implementations of the libraries that ship with your compiler. A Windows application will be able to read and write Windows files using these techniques.
But there is more to working with the file system than reading from or writing to a particular file. How can you find that file to open it? You might want to use a File Open or File Save dialog like so many applications do:
These dialogs are actually generated by Windows, and you can interact with them using COM. One call shows the dialog, and returns an interface pointer to an IShellItem or IShellItemArray representing thefile or files the user selected with the dialog. You then call methods of that interface pointer to get the file name, full path, file type and so on. For more details, check Common Item Dialog.
Sometimes, you don’t want to ask the user what file to read from or write to. You may need to explore the hierarchy of folders, subfolders, and files on the user’s hard drive. The Windows Shell exposes a number of useful COM interfaces that will help you explore and navigate the file system; you can learn more by reading the IShellFolder Interface topic on MSDN. Starting in Windows 7, you can also take advantage of Libraries, collections of folders that can be searched or traversed easily. A good MSDN magazine article, Introducing Libraries, includes both managed and native code snippets.
Concurrency and Parallel Programming
One of major developments of this century, for software developers, has been the rise of importance of concurrency and parallel development techniques. Since CPU clock speed essentially cannot rise any further, application performance is now being improved by taking advantage of multicore machines. The problem is that traditional parallel programming, with explicit threads, shared memory, and locks, is very difficult even for experienced programmers and leads to indeterminate bugs. A number of technologies have been developed for C++ developers to help move away from explicit threading and towards a richer concurrency approach.
The Concurrency Runtime, ConcRT, is a set of components that are partly in your applications and partly on the operating systems where you deploy your applications. The Parallel Patterns Library (PPL), the Asynchronous Agents Library, the Task Scheduler, and the Resource Manager work together, like this:
For a C++ developer, it’s as simple as including ppl.h and then using parallel constructs (for example, replacing a traditional for loop with a call to Concurrency::parallel_for) in your code. In this way you express your intention – this loop can be parallelized, for example – and you rely on ConcRT to make that intention reality, adjusting for the actual number of cores on the machine where your application is executing. To learn more, start at Overview of the Concurrency Runtime.
XML, eXtensible Markup Language, is a core of many modern applications. A large variety of languages and platforms can generate and parse it, and because it’s text-based and whitespace independent, it travels well from platform to platform. It’s at the heart of Web Services, and many file formats are based on it. On Windows, you can use the MSXML COM components, Microsoft XML Core Services, to generate XML or to find what you want in XML generated by another application. Both the Document Object Model (DOM) approach, where you load the entire document into a tree and work with nodes, and the SAX (Simple API for XML) approach, where you stream through the document from start to finish, are supported by MSXML. Alternatively, you could look at XmlLite, which provides a high performance parser for simple tasks.
Sensors and Hardware
Before Windows, writing to screens of different resolution or capability was very difficult, as was writing to various different printers. Windows brought a standard driver model that allowed your code to just write to a general screen, and the driver to work out exactly how a particular screen needed to be given the information. With Windows 7, a similar benefit comes to hardware devices that tell your computer about the world around you. Temperature or ambient light sensors, GPS location-aware devices, and accelerometers (that sense position and movement) can now all be accessed through an abstraction layer provided by Windows itself. For native developers, that means a collection of COM interfaces such as ISensor, which represents a particular sensor you might connect to. You can get started learning how to work with these hardware devices at the Sensor API topic.
A Windows service can run independently of any logged on user. These background services often monitor some aspect of the computer and react to changes, or ensure that certain tasks are completed on a schedule. For example, you might write a service to monitor a particular folder for new files, and when one is found, to read it and save the information in some other repository. Or you might write a service to back up information over night, to send emails reminding users of events or tasks coming due, or to print reports of daily or weekly activity.
A service is simply an EXE, written in the language of your choice, which has the required entry point for the Service Control Manager to start it. You can find all the details and some sample code starting at Services on MSDN.
Windows 7 brought support for multi-touch to Windows. This enables natural user interfaces that can be more intuitive than typing and clicking. Some touch gestures can be used even with applications that are not touch-aware, because Windows translates them into existing Windows messages such as clicks or scrolls. Others require you to adjust your application to make it touch aware. You can then enable users to zoom, pan, and manipulate the UI of your application with pinches, swipes, and other multi-point touch gestures. The Touch Developer Center contains resources for both native and managed code. There are also a number of native and managed samples at Windows Touch: Developer Resources.
The more you dig into capabilities that Windows, especially Windows 7, offers to your application, the more you realize the skills you need to access these capabilities stay consistent. You’ve learned the basics of Windows development in C++ using as few libraries as possible. You can put your building blocks together to create useful applications, large or small, with or without a user interface, touch-based, keyboard-based, mouse-based, connected to hardware, or connected to the Internet. Anything’s possible, so why not get started?