Event-Driven Programming. Windows Is An Event Driven Operating System
Event-Driven Programming. Windows Is An Event Driven Operating System
Windows is an event driven operating system ,the programs respond to user-initiated events: mouse clicks and key presses. Windows architecture that makes event-driven programming possible. The three main elements of Windows are: Applications windows messages An application is a program meant to be used by people.(not OS) Windows is a multi-tasking operating system, which means that more than one application can be running at once. Each application owns one or more windows. Events are initiated by the user or by the operating system, and information about these events must be communicated by the operating system to the applications, and by each application to its windows. This communication is done using messages. Applications. An application consists of executable code. Several types of files can contain executable code, and many files may be used to supply the executable code for a single application. There will be one and only one .exe file, and there may in addition be .dll files (dynamic link libraries) and ActiveX controls (a special kind of dynamic link library). The use of dynamic link libraries permits several applications to share some common executable code. Windows. The visual appearance of a window on the screen: a rectangular portion of the screen, that is used either to present information to the user or collect information from the user, or both. Windows have an internal object, or data structure that is not public. Its like a class containing on the order of a hundred data fields. One of the fields is a number (called a handle) that identifies the application that owns the window. The title bar is the narrow band at the top of some windows, containing the title of the window.
The border of a window is a line (or sometimes a double line) around the window. The client area of a window is the area of the window that is not in the border or title bar. Messages. A message is a certain small structure type. Unlike the data structure for a window, the message data type is public, so we know exactly what its fields are. They include: a time stamp (used only internally by Windows, not used by programmers) a message identifier two unsigned longs for message-specific information. These fields are usually called wParam and lParam. The window handle of the window that is destined to receive this message. Usually a window handle is referred to by a variable named hwnd. The message identifier is an unsigned integer which tells what kind of event this message is about. These integers are never written as integers, but instead are referred to by constants. Important examples of Window messages: WM_LBUTTONDOWN. is sent when the left mouse button is depressed. WM_KEYDOWN. is sent when a key is depressed. WM_CHAR. follows the WM_KEYDOWN message, when the key corresponds to a character with an ASCII code number. Thus function keys and arrow keys cause a WM_KEYDOWN but no WM_CHAR. WM_PAINT. This message is generated by the operating system, when a portion of a window needs its appearance refreshed, perhaps because it was resized, or because it was partially obscured by another window which has been moved. The application message queue. Each application has an application message queue. This is a linked list of messages destined for windows owned by that application. Windows places the newly-constructed message in the correct application message queue. It can compute which
application owns the window, because each window is owned by a unique application and the applications handle is recorded in the window data structure. The main message loop. Once the Message are placed in the application queue the application keeps taking them out, in the order they arrived, and sending them to the destination window. Each Windows application must have a function called WinMain. This function is called when the application is first started by the operating system. It performs some initialization tasks and then enters the main message loop, which (slightly simplified) looks like this: while (GetMessage(&msg)) DispatchMessage(&msg) Here GetMessage removes a message from the application message queue, and DispatchMessage sends it to the destination window. This loop executes until there are no more messages in the message queue, and then it terminates. Window procedures. Each window must have an associated window procedure, which is a function whose job it is to respond to messages. This function has the form MyWindowProc(hwnd, message_identifier, wParam, lParam) The parameters correspond exactly to the fields of a message. to send a message to a window means to call its window procedure The call will pass as parameters the fields of the message. Normally a window procedure will look like this: switch(message_identifier) { case WM_LBUTTONDOWN: /* code to respond to this message */ break; case WM_CHAR: /* code to respond to this message */
break; case WM_PAINT: /* code to respond to this message */ break; ... } ... Messages for which there is a case in this procedure are said to be processed by the window procedure. Messages which are not processed fall through the switch and at the end there is a call to the default window procedure.