Title: Windows and Objects
1Windows and Objects
- Explain the Windows Generic Program
- Discuss the windows objects analogy
- Explain the link between messages event
handlers in event-driven frameworks - Define static and dynamically linked libraries
- Outline the use of the Windows Registry
2Windows program structure
- Single program framework
- Events translated by Windows to messages
- Key press, mouse move, mouse click
- May be hidden by an application framework
- Main program loop
- repeat
- get message
- handle message
- until finished
3Window Procedure
- Examples of Windows
- A main window
- A dialog
- A button
- A text area
- An edit box
- But some applications e.g. Java Swing draw UI
components for themselves uses one main window. - Each window type has a window procedure
- Provides a response to a message
- Windows of the same type use same procedure
- So the procedure has to be given a window
identifier - Windows are identified by a Window Handle
4Window Procedure
- LRESULT CALLBACK WndProc(HWND hWnd, UINT message,
WPARAM wP, LPARAM lP) - switch (message)
- case WM_CREATE ...
- case WM_SIZE ...
- case WM_GETMINMAXINFO ...
- case WM_SETFOCUS ...
- case WM_KILLFOCUS ...
- case WM_COMMAND ...
- // handle menu items
- break
- case WM_LBUTTONDOWN ...
- case WM_NCRBUTTONUP // RtClick on Non Client
area e.g. caption bar - case WM_KEYDOWN
- case WM_PAINT
- case WM_DESTROY
- PostQuitMessage(0) // close the application
- default
- return (DefWindowProc(...))
-
- A typical window procedure
- Select appropriate action depending on message
type - If no specific action needed, call a default
handler - Return 0 if message handled
When are these messages sent? What action should
be taken for each?
5Window Class
- Define the default behaviour of windows in the
class - Unique Name
- Window-procedure address
- Instance handle application registering the
class. - Class cursor Default mouse cursor
- Class icon the large icon.
- Small class icon the small icon
- Class background brush default color and pattern.
- Class menu the default menu
- Class styles Various features e.g. how to
- update the window after moving or resizing it,
- process double-clicks of the mouse,
- Extra class memory - shared by all windows in the
class - (Similar to ?)
6Main Application Outline 1
- // Define register window class type
- wc.style CS_HREDRAW CS_VREDRAW
- wc.lpfnWndProc (WNDPROC)WndProc
- wc.cbClsExtra wc.cbWndExtra 0
- wc.hInstance hInstance
- wc.hIcon LoadIcon (hInstance, szAppName)
- wc.hCursor LoadCursor(NULL, IDC_ARROW)
- wc.hbrBackground (HBRUSH)(COLOR_WINDOW1)
- wc.lpszMenuName szAppName
- wc.lpszClassName szAppName
- RegisterClassEx(wc)
7Main Application Outline 2
- // Create window display
- hWnd CreateWindow(szAppName,
- szTitle, WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT, 0, CW_USEDEFAULT,
- 0, NULL, NULL, hInstance, NULL)
- ShowWindow(hWnd, nCmdShow)
- UpdateWindow(hWnd)
- LoadAccelerators (hInstance, szAppName)
8 Main Application Message Loop
- // Main message loop
- while (GetMessage(msg, NULL, 0, 0))
- if (!TranslateAccelerator (...))
- TranslateMessage(msg)
- DispatchMessage(msg)
-
-
9Windows OO without an OO language?
If not interested in a message, pass the data to
the old window procedure. To modify the response
to a message, take special action and also, if
you want, call the old window procedure, before
or after or in the middle of the special
action. Just like overriding a virtual method
Replaces default window procedure
Default window procedure
Handle messages
10What does an OO approach need?
- Encapsulation
- Data Code
- Properties Methods
- Classes
- Define behaviour of instances
- A way of creating of instances
- Static properties
- Inheritance
- Ability to override methods
- Are Windows windows OO?
- Do they provide the above features?
11General Application Framework
- Modern IDEs provide an event-based framework
- C Builder, .net, Visual C
- Why
- Hide code used in all programs
- E.g. message pump
- Simpler programming interface
- Simpler program structure
- Individual events, not one large window procedure
- Access window facilities through object wrapper
- More uniform
12Converting Messages to Events
Main Set up objects windows
13Libraries
- Libraries
- Shared collections of useful routines
- Static libraries linked to program on compilation
- DLLs (Dynamic Link Libraries)
- Linked only when program runs
- When loaded
- On request by program
- Can be shared by several running programs
- Libraries of objects
- Group routines in coherent fashion
- Groups of routines interact through common data
- Particularly successful for user interface
- Can reduce complexity (e.g manage file handles)
14Registry
- A hierarchical (tree-structured) database
- Keys
- Each node in the tree is a key.
- A key can contain subkeys and data entries
(values). - The presence of a key may be data to an
application - A key may contain any number of named values
- Value
- Has its own storage area for a data value.
- Various types of data string, numerical, binary,
... - Applications can open a key use the values
15Registry Information 1
- Links between document types and applications.
- e.g. actions which the document can perform
- Open, play, edit
- Double-clicking performs the default action
- Links to icons for applications associated
docs. - OLE (Object Linking and Embedding) information
- a link between a friendly name and a unique id
(GUID) - a link between a GUID and an application or dll
16Registry Information 2
- User-specific information in HKEY_CURRENT_USER
key - Non-user-specific data HKEY_CURRENT_MACHINE key
- Application specific data
- settings
- recently used files
- security information
17Summary
- Windows Application
- Windows, message loop and window procedure
- Windows are like Objects
- Encapsulation
- Code data (behaviour (methods) state)
- Inheritance (replacing window procedure)
- Modern IDEs hide standard code
- Take messages call appropriate event handlers
- Wrap windows in IDE-specific object wrapper
- Registry
- Stores information about application types
- Link verbs (seen on right-click) to applications