You are on page 1of 14

Computer System

and Multimedia (Week 7):


Windows GDI: Pixel Output
Textbook: Programming Windows / Charles Petzold. -- 5th ed.
Chapter 7. The Mouse
Chapter 8. Timers

1
Mouse Input
• Windows supports mice with up to five buttons: left,
middle, and right, plus two additional buttons called
XBUTTON1 and XBUTTON2.

2
Responding to Mouse Clicks
• If the user clicks a mouse button while the cursor is over the
client area of a window, the window receives one of the
following messages.
Message Meaning
WM_LBUTTONDOWN Left button down
WM_LBUTTONUP Left button up
WM_MBUTTONDOWN Middle button down
WM_MBUTTONUP Middle button up
WM_RBUTTONDOWN Right button down
WM_RBUTTONUP Right button up
WM_XBUTTONDOWN XBUTTON1 or XBUTTON2 down
WM_XBUTTONUP XBUTTON1 or XBUTTON2 up

3
Mouse Coordinates
• In all of these messages, the lParam parameter contains
the x- and y-coordinates of the mouse pointer.
• The lowest 16 bits of lParam contain the x-coordinate,
and the next 16 bits contain the y-coordinate. Use the
GET_X_LPARAM and GET_Y_LPARAM macros to
unpack the coordinates from lParam.
#include <Windowsx.h>

int xPos = GET_X_LPARAM(lParam);


int yPos = GET_Y_LPARAM(lParam);

4
Additional Flags
• The wParam parameter contains a bitwise OR of flags, indicating the
state of the other mouse buttons plus the SHIFT and CTRL keys.

Flag Meaning
MK_CONTROL The CTRL key is down.
MK_LBUTTON The left mouse button is down.
MK_MBUTTON The middle mouse button is down.
MK_RBUTTON The right mouse button is down.
MK_SHIFT The SHIFT key is down.
MK_XBUTTON1 The XBUTTON1 button is down.
MK_XBUTTON2 The XBUTTON2 button is down.

5
Double Clicks
• A window does not receive double-click notifications by default.
• To receive double clicks, set the CS_DBLCLKS flag in the WNDCLASS
structure when you register the window class.
Flag Meaning
MK_CONTROL The CTRL key is down.
MK_LBUTTON The left mouse button is down.
MK_MBUTTON The middle mouse button is down.
MK_RBUTTON The right mouse button is down.
MK_SHIFT The SHIFT key is down.
MK_XBUTTON1 The XBUTTON1 button is down.
MK_XBUTTON2 The XBUTTON2 button is down.

A double click on the left mouse button produces the following sequence of
messages:
WM_LBUTTONDOWN
WM_LBUTTONUP
WM_LBUTTONDBLCLK
6
WM_LBUTTONUP
Computer System
and Multimedia (Week 7):
Windows GDI: Pixel Output
Textbook: Programming Windows / Charles Petzold. -- 5th ed.
Chapter 8. Timers

7
Timer Basics
• You can allocate a timer for your Windows program by
calling the SetTimer function.
• SetTimer includes an unsigned integer argument
specifying a time-out interval that can range (in theory)
from 1 msec (millisecond) to 4,294,967,295 msec, which
is nearly 50 days.
• The value indicates the rate at which Windows sends
your program WM_TIMER messages.
• For instance, an interval of 1000 msec causes Windows
to send your program a WM_TIMER message every
second.
8
The System and the Timer
• Windows maintains a counter value that it decrements on every
hardware timer tick.
• When this counter reaches 0, Windows places a WM_TIMER message
in the appropriate application's message queue and resets the counter
to its original value.
• Because a Windows application receives WM_TIMER messages
through the normal message queue, you never have to worry about
your program being "interrupted" by a sudden WM_TIMER message
while doing other processing.
• A Windows application cannot receive WM_TIMER messages at a rate
faster than this resolution—about 18.2 times per second under
Windows 98 and about 100 times per second under Windows NT.
• Windows rounds down the time-out interval you specify in the
SetTimer call to an integral multiple of clock ticks. For instance, a
1000-msec interval divided by 54.925 msec is 18.207 clock ticks, which
is rounded down to 18 clock ticks, which is really a 989-msec interval.
• For intervals shorter than 55 msec, each clock tick generates a single 9
High-Resolution Timer
• A counter is a general term used in programming to refer to an incrementing
variable. Some systems include a high-resolution performance counter that
provides high-resolution elapsed times.
• If a high-resolution performance counter exists on the system, you can use
the QueryPerformanceFrequency function to express the frequency, in
counts per second. The value of the count is processor dependent. On some
processors, for example, the count might be the cycle rate of the processor
clock.
• The QueryPerformanceCounter function retrieves the current value of the
high-resolution performance counter. By calling this function at the
beginning and end of a section of code, an application essentially uses the
counter as a high-resolution timer. For example, suppose that
QueryPerformanceFrequency indicates that the frequency of the high-
resolution performance counter is 50,000 counts per second. If the
application calls QueryPerformanceCounter immediately before and
immediately after the section of code to be timed, the counter values might
be 1500 counts and 3500 counts, respectively. These values would indicate
that .04 seconds (2000 counts) elapsed while the code executed. 10
QueryPerformanceFrequency function
Retrieves the frequency of the high-resolution performance counter, if one exists. The
frequency cannot change while the system is running.
Syntax

BOOL WINAPI QueryPerformanceFrequency( __out LARGE_INTEGER *lpFrequency );


Parameters
lpFrequency [out]
Type: LARGE_INTEGER*
A pointer to a variable that receives the current performance-counter frequency, in counts
per second. If the installed hardware does not support a high-resolution performance
counter, this parameter can be zero.
Return value
Type: BOOL
If the installed hardware supports a high-resolution performance counter, the return value is
nonzero.
If the function fails, the return value is zero. To get extended error information, call
GetLastError. For example, if the installed hardware does not support a high-resolution
performance counter, the function fails.

11
Creating a Timer
The following example uses the SetTimer function to create two
timers. The first timer is set for every 10 seconds, the second for every
five minutes. The can be created under WM_CREATE message
processing.

#define IDT_TIMER1 100


#define IDT_TIMER2 101
// Set two timers.

SetTimer(hwnd, // handle to main window


IDT_TIMER1, // timer identifier
10000, // 10-second interval
(TIMERPROC) NULL); // no timer callback

SetTimer(hwnd, // handle to main window


IDT_TIMER2, // timer identifier
300000, // five-minute interval
12
(TIMERPROC) NULL); // no timer callback
Processing The Timer
To process the WM_TIMER messages generated by these timers, add a
WM_TIMER case statement to the window procedure for the hwnd parameter.
case WM_TIMER:

switch (wParam)
{
case IDT_TIMER1:
// process the 10-second timer

return 0;

case IDT_TIMER2:
// process the five-minute timer

return 0;
}
13
Week 7 Programming Exercise.CPP
Click here to open file. Lecturer click here.
• Program will start with Count Up at center of display
• Press Left Mouse Button to Count Up at click location
and Press Right Mouse Button to Count Down at click
location\n\
• This Program uses TIMER, Mouse button click and
SendMessage.
• The program will erase background every time the left
or right mouse button is clicked.
• You will need to put up your own question on the
working of the program as an exercise to understand
program built by other people.
• Use debug to help yourself

14

You might also like