Programming Windows 2
Here we meet again in the second tutorial of Programming Windows,
In tutorial#1 we talked about some history of Windows and some definitions of the APIs.
In this tutorial we will try to write a simple Windows program that only displays a hello message Using APIs functions only.
Writing a program using APIs only is not a feature in it self (it is a little faster but this is not the idea), it helps us understanding more things about Windows programming, most of the code we will write in this tutorial and the next one is generated by RAPID tools (Visual C++, C++ Builder, Delphi, Visual basic and others).
First lets remember when we first learned C the first program was some thing like this:
#include <stdio.h>
int main ()
{
printf (“hello, world”\n);
return 0;
}
It contained an include statement and one function only main called the Program Entry Point contained a display message statement and a return statement.
Every program must have an entry point however the language it is written in, this entry point is the first the first thing that runs of our program as in C the entry point is main, the entry point of a Windows program is WinMain so lets write the previous program again but for windows:
#include <windows.h>
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,PSTR szCmdLine, int iCmdShow)
{
MessageBox (NULL, "Hello, Windows 98!", HelloMsg", MB_OK) ;
return 0 ;
}
The header file STDIO.H has been replaced with WINDOWS.H which includes other headers that contain most of the APIs functions, data types and constant identifier (we have to tell that the APIs was first designed to be used within C language, although it can be called using any other language that support Dynamic linking).
The entry point main has been replaced with WinMain, and the C run-time library function printf has been replaced with the Windows API function MessageBox.
To try this program you may use Visual C++ and start a new project of type Win32 Application and create a new c++ source file and write that code.
We can see the Winmain and MessageBox have many parameters, like most of the APIs you will see they take also many parameters and this is one of the disadvantages of APIs, so we will only explain the important parameters of each function only, you can read the others at MSDN help referenced at the end of the tutorial.
For now you can know that the second parameter of MessageBox is a null terminating string for the message body and the third is also a string for the dialog box title and the fourth takes a constant that let you choose which buttons you wish to appear on you dialog box you can set this argument to many values at one time using the C OR (|) for example the line:
MessageBox (NULL, Are You Sure you want to delete this file"," Confirm delete", MB_YESNOCANCEL|MB_ICONQUESTION);
Will display a Message box with Yes, No and Cancel buttons and an Question icon
MB_YESNOCANCEL and MB_ICONQUESTION are defined in winuser.h (included automatically by windows.h) as macros like this:
#define MB_ICONEXCLAMATION 0x00000030L #define MB_YESNOCANCEL 0x00000003L
MessageBox returns you the result of the button the user clicked as a constant to like IDYES, IDNO, and IDCANCEL.
You can see that calling one function MessageBox has created a window (dialog box) with a titles bar and 3 button, the user can move this window and interact with it as he likes, Windows handles all the interaction between the user and the Message Box for you.
Next step we shall Create a real window with sizing borders and close, minimize, maximize buttons and a system menu, Creating a window is easy as calling the CreateWindow function, but that functions seems to take 11 parameters on of them is a struct of 10 members which seems a little complicated, but you will notice that most of these parameters a routine work and is written every time, but before writing the code for our window let talk about Windows Architecture.
When programming for Windows, you're really engaged
in a type of object-oriented programming. (Although the APIs was designed to be
called in C language “which is not object-oriented” but the object mechanism
can be obtained using some tricks), the main object in Windows is a “Window”, a
window is any rectangle on the screen that receives user input and display
output in the form of text and graphics, so any Windows Application is a window,
the Message Box is a window, the button is a window, even radio button, check
box, list box, menu, scroll bar and text box, is a special type of a window
called child window or child window control.
When the user interacts with these windows or child controls
Windows sends a message to the window that the user interacted with (these
messages is the same like events when working with RAPID tools), for example
when the user sizes the border of you application window, your window will
receive a resize message and your application should respond correctly to that
message, but how these message are sent?
First you should look at Windows as a black box that
looks after the mouse, the keyboard and other hardware and running applications
and generates events due to user interaction with each of the running
applications and the mouse and the keyboard.
For
each application Windows creates a “message queue”, and when ever an event
occurres related to some program/application (such a mouse click or key press)
Windows pushes a message for that event in the program message queue and so on,
the program has to fetch these messages (events) from the message queue using a
“message loop” (a small loop that get messages from the message queue) and then
dispatch them to a function called “Window procedure” usually named in code winporc
(In conversation among Windows programmers, it's called the "win
prock."), the window procedure is responsible for handling the
message/events and processing them then it returns control to Windows .
So let’s put all of this together and write a
program that creates a window and handle events using APIs only, but time is
up, we (Åä ÔÇÁ Çááå) will do this next tutorial, so stay tuned!
References:
1. The famous classic book: Programming Windows Fifth
Edition by Charles petzold
2. The MSDN (Microsoft Developer Network) www.microsoft.com/msdn/ (Select
Platform SDK/User Interface services)
Any comments and/or questions don’t hesitate mail me
at: