Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
13 views

Week 7 Programming Exercise - CPP

This document contains code for a Windows program that uses timers and mouse input. It registers a window class, creates a window instance, and enters a message loop. The timer callback increments/decrements a counter variable depending on if the left/right mouse button was last clicked. It erases the background and displays the counter text at the clicked location each time. The program demonstrates using timers, mouse input handling, and sending messages to update the window periodically.

Uploaded by

fatah.ozil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Week 7 Programming Exercise - CPP

This document contains code for a Windows program that uses timers and mouse input. It registers a window class, creates a window instance, and enters a message loop. The timer callback increments/decrements a counter variable depending on if the left/right mouse button was last clicked. It erases the background and displays the counter text at the clicked location each time. The program demonstrates using timers, mouse input handling, and sending messages to update the window periodically.

Uploaded by

fatah.ozil
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/*--------------------------------------------------------------------------

Week 7 Programming Exercise.CPP --

-------------------------------------------------------------------------*/

#include <windows.h>
#include <Windowsx.h>
#pragma comment(lib,"winmm.lib")
#define IDT_TIMER1 100
#define IDT_TIMER2 101

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,


PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
//Step 1: Registering Window Class of the program
//define the window's attributes/feature in structure variable of type WNDCLASS
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;

//Register the class named wndclass


if (!RegisterClass (&wndclass))
{
//Display error message if Class registration fails.
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}

//Step 2: Creating the Window


hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters

//Step 3: Display the Window


ShowWindow (hwnd, iCmdShow) ;
//Step 4: Repainting Client Area
UpdateWindow (hwnd) ;
//Step 5: The Message Loop
MSG msg ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}

LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
static int count1;
static int count2;
static int Forward=TRUE;
static int MouseXpos;
static int MouseYpos;

switch (message)
{
case WM_CREATE: //Process WM_CREATE message
{
TCHAR TempText[100];

SetTimer(hwnd, // handle to main window


IDT_TIMER1, // timer identifier
1000, // 1-second interval
(TIMERPROC) NULL); // no timer callback
count1=0;
hdc = GetDC (hwnd) ;
GetClientRect (hwnd, &rect) ; //Get rectange dimensions of
Client Area

ReleaseDC (hwnd, hdc) ;


MouseXpos = (rect.right-rect.left)/2;
MouseYpos = (rect.bottom-rect.top)/2;
}
return 0 ;

case WM_TIMER:

switch (wParam)
{
case IDT_TIMER1:
// process the 1-second timer
{
TCHAR TempText[20];
TCHAR Timer1toDisplay[200]; //Reserve to store up to
200 characters
GetClientRect (hwnd, &rect) ; //Get rectange
dimensions of Client Area
hdc = GetDC (hwnd) ;
SendMessage(hwnd,WM_ERASEBKGND,(WPARAM)hdc,NULL);
TCHAR InstructionText[] = TEXT ("Program will start with
Count Up at center of display\n\
Press Left Mouse Button to Count Up at click location and\nPress Right Mouse Button
to Count Down at click location\n\
This Program uses TIMER, Mouse button click and SendMessage.\nThe program will
erase background every time the \
left or right mouse button is clicked.\nYou will need to put up your own question \
on the working\nof the program as an exercise to understand program built by other
people. \n\
Use debug to help yourself");

GetClientRect (hwnd, &rect) ; //Get rectange


dimensions of Client Area

DrawText (hdc, InstructionText, -1, &rect, //Draw Text on


client area specified by
DT_LEFT ) ;
if (Forward)
count1++;
else
count1--;
_ltow(count1,TempText,10); //Convert Count1 to string
character
TextOut(hdc, MouseXpos,MouseYpos+5,TempText ,
wcslen(TempText));

ReleaseDC (hwnd, hdc) ;

return 0;
}
case WM_LBUTTONDOWN:
Forward=TRUE;
MouseXpos = GET_X_LPARAM(lParam);
MouseYpos = GET_Y_LPARAM(lParam);

break;
case WM_RBUTTONDOWN:
Forward=FALSE;
MouseXpos = GET_X_LPARAM(lParam);
MouseYpos = GET_Y_LPARAM(lParam);
break;
case WM_DESTROY:
PostQuitMessage (0) ; //inserts a WM_QUIT message in the program's
message queue.
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}

You might also like