This is a course material I developed for Windows Application programming. The goal of the course was to teach students Win32-level programming to gain understnading of "Native" Windows programming platform. This course does not use Microsoft Foundation Classes (MFC) at all because it tends to hinder students from seeing how Windows actually work.
Windowless Windows Application
Windowless Windows application program is an
application program that does not have any graphical user interface element
(GUI element). Because Windows is an operating system with a rich set of graphical
interface features, such as buttons and mouse pointers, it is normally more
appropriate to utilize them. However, it is possible to write a program that
does not have any GUI element. In a way, these are the simplest applications
that run on Windows operating system. We start our study of Windows
applications with Windowless Windows applications, then gradually add code that
utilizes Windows GUI features.
SkeletonC
SkeletonC is a command-line program written in C language.
Visual C++ AppWizard automatically generated all files for this project. If you
look at the source code, you will notice that it is just plain C program that
uses main() as the entry point and displays some texts to the command-line
standard output. The program doesn't use any GUI element for interacting with
the user. The entire program is reproduced below.
// SkeletonC.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}
Like any other C program, it has a mandatory entry point
function, main(). The program starts execution from this function, and when
this function returns, the entire program ends. SkeletonC interacts with the
user by means of a standard I/O function, printf. It could use other functions
like scanf to read user input from the command-line.
An interesting thing about SkeletonC is that it is not
really the simplest kind of program that runs on Windows operating system. On
the other hand, it is more complicated than the program that will be discussed
next, SkeletonNW. We will come back to this topic after we look at SkeletonNW.
SkeletonNW
This program is a pure Windows application that just
doesn't display a window or any other graphical user interface element such as
buttons. Visual C++ AppWizard automatically generated all files for this
project.
// SkeletonNW.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
return 0;
}
Entry point of Windows application: WinMain
WinMain is the entry point of the SkeletonNW program. It is
equivalent of main() function in C program. When Windows operating system loads
SkeletonNW, it starts executing this function (in fact, it's the only function
in SkeletonNW), which doesn't do much in its body.
Sequential execution model
Comparing the program behavior of SkeletonNW with SkeletonC,
you notice that they both execute sequentially in top-down manner. When the
execution reaches the end of main() or WinMain(), that's the end of the programs.
This sequential behavior is what many C programmers are used to. In fact, there
is absolutely nothing wrong with writing a Windows application that simply runs
code sequentially and exits. On the other hand, typical Windows applications
that we know, like Word and Internet Explorer, are all event-driven. In the
next chapter, we will see "Event-driven execution model" in detail.
Revisiting SkeletonC
SkeletonNW, discussed in the previous section, is a truly
minimal Windows application – Windows operating system loads it into the memory
and transfer control to WinMain(), which is the starting point of your
program. SkeletonC, on the other hand, actually involves a helper code: it
actually runs on top of a layer of code called "C Run-time (CRT) Startup code".
CRT Startup code has WinMain() function, which Windows calls when loaded, and
it sets up the environment where a plain C program like SkeletonC can run. CRT
Startup code emulates a command-line execution environment. This is to say that
CRT Startup graphically draws any text printed out in the command-line window.
Subsequent sample programs will not be based on SkeletonC
because of the extra layer of CRT Startup code. To understand structures of
Windows applications from ground up, we will start with SkeletonNW instead.
Regular Windows Application
Regular Windows
application program is a typical application program that has some graphical
user interface element (GUI element), especially an application window that
sets a graphical boundary on the screen while running. Most Windows
applications that we know fall into this category: Microsoft Word, Netscape, Internet Explorer, Notepad, etc. A vast majority
of Windows applications are of this type.
Please note that the term "regular Windows application" is
not an industry-standard way to refer to those belonging to this category. It
is usually referred to as simply "Windows application". However, in order to
distinguish it more clearly from other types (Windowless Window application and
Dialog-based Windows application), I added the word
"regular".
Windows applications that do not belong to this category
include Calculator, Volume Control Panel, and many Control Panel Applet (small applications that are in Control Panel),
categorized under "Dialog-Based Windows applications". In addition, Service (NT
Service) applications also do not belong to "Regular Windows Application", but
we will not discuss service in this course.
SimpleApp
I wrote this program as an example, but usually you would
use Visual C++ AppWizard to generate similar
bare-minimum application, which we will discuss later in this section.
The functionality of SimpleApp
program is very minimal: it shows a window with a menu, from which you can tell
it to show "About dialog box" and exit from the application. In contrast to SkeledonNW application that we saw in the previous section,
this program shows a window. This simple difference is significant when you see
the code.
SkeletonApp
This program is a pure
Windows application that displays a window. Visual C++ AppWizard
automatically generated all files for this project.
File generated by AppWizard
As you can see, it generates several source files for you.
All files are complete and you can build and run the program without any
modification. It is written in C, and does not use C++ nor
any MFC feature. This AppWizard generated application
is a good starting point for your own programs.
Event-driven execution model
Comparing the with sequential execution mode we discussed in
the previous section. Unlike a typical C program, in which a program execution
is top down, event-driven execution model heavily relies on "event handler". In
essence, the program sets up a loop to listen
to an event. When the event is of your interest, it invokes a corresponding event handler. Windows programming is
all about "event handling". This is another reason why beginner Windows
programmers get easily confused: it is not like typical C program they have
been writing.
Dialog-Based Windows Application
Dialog-Based Windows application program is an
application program that uses single diglog box window as it main application area.
It is suitable for applications with relatively straightforward functionalities.
It is much somewhat easier to write than Regular Windows applications. You find
alot of utility applications written as Dialog-Based Windows applications.
SimpleDlg
SimpleDlg demonstrates how to write a Dialog-Based Windows applications.
Dialog-Based Windows application is somewhat different from the other types of Windows application,
so you don't need to learn the concepts discussed for other types of Windows. Perhaps, it is even
easier to start with Dialog-Based ones, then study other types of Windows applications.
Additoinal sample Projects for other concepts
Followings are additional projects that demonstrate other fundamental concepts in Windows programming. Be sure to check out Control and Dynamic Library (DLL) samples since these are the primary means of organizaing your applications. Examples for Process and Thread are also important as any Windows application executes using one or more threads within a dedicated process.