How Windows Works Event-Driven Programming.

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. It’s 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.

This message is generated by the operating system. the message data type is public. • The window handle of the window that is destined to receive this message. perhaps because it was resized. The message identifier is an unsigned integer which tells what kind of event this message is about. A message is a certain small structure type. Usually a window handle is referred to by a variable named hwnd. The client area of a window is the area of the window that is not in the border or title bar. • WM_KEYDOWN. The application message queue. Thus function keys and arrow keys cause a WM_KEYDOWN but no WM_CHAR. These fields are usually called wParam and lParam. so we know exactly what its fields are. is sent when a key is depressed. not used by programmers) • a message identifier • two unsigned longs for message-specific information. Messages. but instead are referred to by constants. follows the WM_KEYDOWN message. This is a linked list of messages destined for windows owned by that application. Important examples of Window messages: • WM_LBUTTONDOWN.The border of a window is a line (or sometimes a double line) around the window. or because it was partially obscured by another window which has been moved. These integers are never written as integers. Windows places the newly-constructed message in the correct application message queue. Each application has an application message queue. is sent when the left mouse button is depressed. Unlike the data structure for a window. It can compute which . when a portion of a window needs its appearance “refreshed”. • WM_CHAR. • WM_PAINT. They include: • a time stamp (used only internally by Windows. when the key corresponds to a character with an ASCII code number.

message_identifier. which (slightly simplified) looks like this: while (GetMessage(&msg)) DispatchMessage(&msg) Here GetMessage removes a message from the application message queue. This loop executes until there are no more messages in the message queue. which is a function whose job it is to respond to messages. Each Windows application must have a function called WinMain. It performs some initialization tasks and then enters the main message loop. case WM_CHAR: /* code to respond to this message */ . lParam) The parameters correspond exactly to the fields of a message. This function is called when the application is first started by the operating system. Normally a window procedure will look like this: switch(message_identifier) { case WM_LBUTTONDOWN: /* code to respond to this message */ break.application owns the window. Each window must have an associated window procedure. in the order they arrived. Window procedures. and sending them to the destination window. wParam. Once the Message are placed in the application queue the application keeps taking them out. This function has the form MyWindowProc(hwnd. to send a message to a window means to call its window procedure The call will pass as parameters the fields of the message. because each window is owned by a unique application and the application’s handle is recorded in the window data structure. and then it terminates. and DispatchMessage sends it to the destination window. The main message loop.

. . Messages which are not processed fall through the switch and at the end there is a call to the default window procedure. Messages for which there is a case in this procedure are said to be “processed” by the window procedure.. } . case WM_PAINT: /* code to respond to this message */ break.break. ...

Sign up to vote on this title
UsefulNot useful