|
Microsoft Windows is an operating system that helps a
person interact with a personal computer (PC). Programmers who want to
create applications that people can use on MS Windows base them on a library called
Win32.
Win32 is a library of data types, constants,
functions, and classes (mainly structures) used to create applications for
the Microsoft Windows operating system.
To create a basic application, you will first need a
compiler that runs on a Microsoft Windows operating system. Although you
can apply Win32 on various languages, including Pascal (namely Borland
Delphi), we will use only one language. In reality the Win32 library is
written in C, which is also the primary language of the Microsoft Windows operating systems.
All of our programs will be written in C++. You will not see a
difference between C and C++ in our programs. Although all of the structures of
Win32 are mostly C objects, we will use Win32 as if it were a C++ library.
This simply means that, whenever needed, we will apply C++ rules. |
|
All Win32 programs primarily look the same and behave
the same but, just like C++ programs, there are small differences in
terms of creating a program, depending on the compiler you are using. For
my part, I will be testing our programs on Borland C++ Builder, Microsoft
Visual C++ 6, Dev-C++, and Microsoft Visual C++ .NET.
For a basic Win32 program, the contents of a Win32
program is the same. You will feel a difference only when you start adding some
objects called resources. |
|
Using Borland C++ Builder |
|
To create a Win32 program using Borland C++ Builder,
you must create a console application using the Console Wizard. You must
make sure you don't select any option from the Console Wizard dialog box.
After clicking OK, you are presented with a semi-empty file that contains
only the inclusion of the windows.h library and the WinMain() function
declaration. From there, you are ready.
From most environments used, Borland C++ builder is
the only one that provides the easiest, but also unfortunately the
emptiest template to create a Win32 application. It doesn't provide any
real template nor any help on what to do with the provided file. In
defense of the Borland C++ Builder, as you will see with Microsoft Visual
C++ and Dev-C++, the other environments may fill your file with statements
you don't need, you don't like, or you don't want. Therefore, Borland C++
Builder provides this empty file so you can freely decide how you want to
create you program and what you want to include in your program. This
means that I agree with Borland C++ Builder providing you with an empty
file because at least the syntax of the WinMain() function is provided to
you.
|
Practical Learning: Introducing Windows Programming
|
|
- Start Borland C++ Builder (5 or 6 I don't care and it doesn't
matter).
- On the main menu, click File -> New... or File -> New ->
Other...
- In the New Items dialog box, click Console Wizard and click OK
- In the Console Wizard, make sure that only the C++ radio button is selected:
- Click OK.
You are presented with a file as follows:
//---------------------------------------------------------------------------
#include <windows.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
return 0;
}
//---------------------------------------------------------------------------
|
- Save the application in a new folder named Win32A
- Save the first file as Main.cpp and save the project as SimpleWindow
|
Using Microsoft Visual C++ |
|
To create a Win32 application using Microsoft Visual
C++, display the New (5 and 6 versions) or New Project (.Net version)
dialog box and select Win32 application (5 and 6) or Win32 Project (.Net)
item.
Microsoft Visual C++ provides the fastest and fairly
most complete means of creating a Win32 application. For example it
provides a skeleton application with all of the code a basic application
would need. Because we are learning Win32, we will go the hard way, which
consists of creating an application from scratch. If fact, this allows me
to give almost (but not exactly) the same instructions as Borland C++
Builder.
- Start Start the Microsoft Development Environment.
- On the main menu, click File -> New... or File -> New ->
Project...
- In the New or New Project dialog box, click either Win32 Application
or click Visual C++ Projects and Win32 Project:
- In the location, type the path where the application should be
stored, such as C:\Programs\MSVC
- In the Name edit box, type the name of the application as Win32A
and click OK
- In the next dialog box of the wizard, if you are using MSVC 5 or 6,
click the An Empty Project radio button:
 |
If you are using MSVC .Net, click Application
Settings, then click the Console Application radio button, then
click the Empty Project check box:
 |
- Click Finish. If you are using MSVC 6, you will be presented with
another dialog box; in this case click OK
- To create the first needed file of the program, if you are using
MSVC 5 or 6, on the main menu, click File -> New. If you are using
MSVC .Net, on the main menu, click Project -> Add New Item...
- If you are using MSVC .Net, make sure that Visual C++ is selected in
the Categories tree view.
In both cases click either C++ Source File or C++ File (.cpp)
- In the Name edit box, replace the contents with a name for a file.
In the case, replace it with Main and press Enter
|
About Microsoft Windows Messages |
|
A computer application is equipped with Windows
controls that allow the user to interact with the computer. Each control
creates messages and sends them to the operating system. To manage these messages, they are
handled by a function pointer called a Windows procedure. This function
can appear as follows:
LRESULT CALLBACK MessageProcedure(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
This function uses a switch control to list
all necessary messages and process each one in turn. This processes only
the messages that you ask it to. If you have left-over messages, and you
will always have un-processed messages, you can call the DefWindowProc()
function at the end to take over.
The most basic message you can process is to make sure
a user can close a window after using it. This can be done with a function
called PostQuitMessage(). Its syntax is:
VOID PostQuitMessage(int nExitCode)
This function takes one argument which is the value of
the LPARAM argument. To close a window, you can pass the argument as
WM_QUIT.
Based on this, a simple Windows procedure can be
defined as follows:
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
return 0;
}
|
|