our application with clicks

23
Spring 2003 CS 325 Class Notes Page 1 Our Application with Clicks What do we need to display the locations of our clicks 1. An application to act as main and instantiate the window 1. Derive and instantiate a class from CWinApp 2. Some mechanism of associating a mouse click message (or event) to a method (message or event handler) 1. We’ll need another Message Map to do this 3. A way to display the contents of our window 1. Some way to represent get the coordinates of our click 2. Clean way to convert these numerical points to a character array

Upload: lakia

Post on 12-Jan-2016

39 views

Category:

Documents


0 download

DESCRIPTION

Our Application with Clicks. What do we need to display the locations of our clicks An application to act as main and instantiate the window Derive and instantiate a class from CWinApp Some mechanism of associating a mouse click message (or event) to a method (message or event handler) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 1

Our Application with ClicksWhat do we need to display the locations of our clicks

1. An application to act as main and instantiate the window1. Derive and instantiate a class from CWinApp

2. Some mechanism of associating a mouse click message (or event) to a method (message or event handler)

1. We’ll need another Message Map to do this

3. A way to display the contents of our window1. Some way to represent get the coordinates of our click

2. Clean way to convert these numerical points to a character array

Page 2: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 2

Capturing Mouse Clicks

• System has built-in routines to capture mouse clicks– OnLButtonDown, OnLButtonUp– OnRButtonDown, OnRButtonUp

• All four routines take two parameters– UINT flags, a 32-bit unsigned integer– CPoint point, a class for points, x and y

coordinates are available (can retrieve), as well as other operations

Page 3: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 3

Program captures mouse clicks#include <afxwin.h>

#include <string>

using namespace std;

class CMouseWin : public CFrameWnd {

public:CMouseWin();afx_msg void OnLButtonDown(UINT, CPoint);afx_msg void OnRButtonDown(UINT, CPoint);afx_msg void OnLButtonUp(UINT, CPoint);afx_msg void OnRButtonUp(UINT, CPoint);

private:void showUp(CPoint);void showDown(CPoint);DECLARE_MESSAGE_MAP( )

};

• Our class will provide new methods for processing the various mouse click events

• Utilizes two private methods to process these mouse clicks (showUp and showDown)

Page 4: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 4

Program Body (part one)#include <strstrea.h>

#include "example.h"

CMouseWin::CMouseWin() {

Create(NULL, "Mouse Click Example", WS_OVERLAPPEDWINDOW,

CRect(100,100,500,500)); }

afx_msg void CMouseWin::OnLButtonDown(UINT uFlags, CPoint point){ showDown(point); }

afx_msg void CMouseWin::OnLButtonUp(UINT uFlags, CPoint point) { showUp(point); }

afx_msg void CMouseWin::OnRButtonDown(UINT uFlags, CPoint point) { showDown(point); }

afx_msg void CMouseWin::OnRButtonUp(UINT uFlags, CPoint point) { showUp(point); }

• One new header file (will use it shortly)

• Constructor creates a standard window

• Four methods defined to capture each of the four mouse actions– Two methods call the

showDown method (press mouse button)

– Two methods call the showUp method (release mouse button)

Page 5: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 5

Program Body (part two)void CMouseWin::showDown(CPoint point) {

CClientDC dc(this);

ostrstream s;

s << "(" << point.x << ",“<< point.y << ")";

dc.TextOut(50,50,s.str(), s.pcount());

}

void CMouseWin::showUp(CPoint point) {

CClientDC dc(this);

char carText[32];

ostrstream s(carText, sizeof(carText));

s << "(" << point.x << ",“<< point.y << ")";

dc.TextOut(200,200,carText, s.pcount());

}

• New concept here (writing to a string)– Already seen writing to

files and stdout– Can also write to an

array of chars

• ProcessDeclare arrayDeclare output streamWrite to it (s.pcount has

count of chars written)Take this array (with the

output that was written to it) and display it in the window

Page 6: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 6

Program Body (part three)BEGIN_MESSAGE_MAP

( CMouseWin, CFrameWnd )ON_WM_LBUTTONDOWN( )ON_WM_RBUTTONDOWN( )ON_WM_LBUTTONUP( )ON_WM_RBUTTONUP( )

END_MESSAGE_MAP( )class CMouseApp : public CWinApp {public:

BOOL InitInstance( ) {m_pMainWnd = new CMouseWin; m_pMainWnd ->ShowWindow

(m_nCmdShow);m_pMainWnd->UpdateWindow();return TRUE; }

} mouseApp;

• Message map section defines four events that this program is watching out for (all related to mouse buttons). All four are built into the system, you don’t need to define further

• Standard clause at the end of the program to get things started (register mouseApp with WinMain and have it invoked when the program starts)

Page 7: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 7

Class Exercises

• Modify the code from the previous “catching mouse clicks example” so that:– The right mouse button down and up locations are

printed at the top of the window (in the left and right corners)

– The left mouse button down and up locations are printed at the bottom of the window (in the left and right corners)

– Files• MouseWin.h• MouseWin.cpp

Page 8: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 8

Drawing on the window

• Similar to writing text on the windowCClientDC dc(this); // get window context

dc.TextOut( … ) // write out some text

• Routines exist to do almost anything

• We will focus on routines to:– Move to a given location– Draw a line from where you are to another location– Create a filled rectangle of some color

Page 9: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 9

Sample drawing program• Want to be able to write text on the screen

– Already seen this (Menu example)

• New capabilities– Want to be able to draw lines on the screen– Want to be able to draw shapes on the screen

• Example– Extremely “simplistic” version of tic-tac-toe– Menu with two options (“new” and “quit”)– Draw a simple board, click to indicate moves

Use red and blue blocks instead of X’s and O’s

Page 10: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 10

Header file for program#include <afxwin.h>

class CTTTWin : public CFrameWnd {

public:CTTTWin();~CTTTWin();afx_msg void OnExit();afx_msg void OnNew();afx_msg void OnLButtonDown (UINT uFlags, CPoint point);

private:void DrawBoard();int m_iToggle;DECLARE_MESSAGE_MAP( )

};

• Standard class header• Handle two “menu events”

(new and quit)• Handle the left mouse down

event (processing occurs as soon as you press the mouse button)

• Private routine DrawBoard is used to draw an image on the window

Page 11: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 11

Program body (part one)#include “TTTWin.h"

#include "menus.h“

CTTTWin::CTTTWin() {

Create (NULL, "TicTacToe", WS_OVERLAPPEDWINDOW,

CRect (100,100,410,450), NULL, "MyMenus");

m_iToggle = 0;

}

CTTTWin::~CTTTWin() { }

afx_msg void CTTTWin::OnNew()

{ m_iToggle = 0; DrawBoard(); }

afx_msg void CTTTWin::OnExit()

{ SendMessage(WM_CLOSE); }

• Constructor builds basic window– Our board is 300x300– Need a little extra height

as window header stuff counts in overall size

• Destructor does nothing• When “new” menu item

is selected, draw a new board

• Quit program when “quit” option is selected

Page 12: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 12

Program body (part two)void CTTTWin::DrawBoard() {

CClientDC dc(this);

CBrush *pBrush = new CBrush();

pBrush->CreateSolidBrush(RGB(255,255,255));

dc.FillRect(CRect(0,0,300,300), pBrush);

CPen *pPen = new CPen;

pPen->CreatePen(PS_SOLID, 1, RGB(0,0,0) );

dc.MoveTo(0,100); dc.LineTo(300,100);

dc.MoveTo(0,200); dc.LineTo(300,200);

dc.MoveTo(100,0); dc.LineTo(100,300);

dc.MoveTo(200,0); dc. LineTo(200,300);

}

• Use CBrush and FillRect to paint full device (window) white

• Use Cpen, MoveTo, and LineTo to draw lines for TicTacToe Board

Page 13: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 13

Program body (part three)afx_msg void CTTTWin::OnLButtonDown

(UINT uFlags, CPoint point) { CClientDC dc(this); CBrush *pBrush = new CBrush(); if (m_iToggle) {

pBrush->CreateSolidBrush(RGB(255,0,0));dc.FillRect(CRect(point.x-20,point.y-20,

point.x+20,point.y+20), pBrush); m_iToggle = 0; } else {

pBrush->CreateSolidBrush(RGB(0,0,255));dc.FillRect(CRect(point.x-20, point.y-20,

point.x+20,point.y+20), pBrush);m_iToggle = 1;

}}

• To process a mouse click, we draw a rectangle (using FillRect) around where you clicked and then change colors (assuming the next move is from the other player)

• No checking of location of click is done

Page 14: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 14

Program body (part four)BEGIN_MESSAGE_MAP

(CTTTWin, CFrameWnd)ON_COMMAND(IDM_NEW, OnNew)ON_COMMAND (IDM_EXIT, OnExit)ON_WM_LBUTTONDOWN( )

END_MESSAGE_MAP( )class CTTTApp : public CWinApp {public: BOOL InitInstance() {

m_pMainWnd = new CTTTWin;m_pMainWnd->ShowWindow

(m_nCmdShow);m_pMainWnd->UpdateWindow();return TRUE;

}} tttApp;

• Standard message map, we want to handle two unique menu items, don’t need to worry about mouse clicks (built into system)

• Standard clause to invoke/control execution of program (via WinApp)

Page 15: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 15

Extra “menu” files needed• menus.rc

#include <afxres.h>

#include "menus.h"

MyMenus MENU {

POPUP "Tic-Tac-Toe" {

MENUITEM "New", IDM_NEW

MENUITEM "Quit", IDM_EXIT

}

}

• menus.h

#define IDM_EXIT 5000

#define IDM_NEW 5001

Page 16: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 16

Events (messages)• Each message has a unique id associated with it

– Windows-defined• ID’s 0 – 1023

– You-defined• Can name between 1024 –65535

• Naming Convention: – ID’s

• WM_MESSAGE: Windows Message• IDM_MESSAGE: Menu Id

– Handlers (methods)• OnMessage

Page 17: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 17

Event/Message Handlers

• Each unique message ID needs to be associated with message handler (method)– MFC-defined

• Has already been associated• Use pre-defined macro ON_WM_MESSAGE()in

message to signify it is to be handled in the class specified in the BEGIN_MESSAGE_MAP params

– You-defined• You must make the association• Use macro ON_COMMAND(ID, OnMessage)

Page 18: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 18

Macros / Message Map• The macros we use have already been defined.

Before compilation time, there is a text replacement where we use the macro identifier.

• We have been using the Message Map macros(where we associate a message ID to its handler)BEGIN_MESSAGE_MAP(owner-class name, base-class name)

//mappingsON_WM_MESSAGE() // for pre-definedON_COMMAND(ID, OnMessage) //for you-defined

END_MESSAGE_MAP()• For more than you want to know about message maps

– http://msdn.microsoft.com/library/default.asp?URL=/library/devprods/vs6/visualc/vcmfc/_mfcnotes_tn006.htm

Page 19: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 19

Message Boxes

• Often want to pop up a little window to let the user know something– Information– Question– Notification

• MessageBox class simplifies this process– MessageBox has a title– MessageBox displays information– MessageBox might have

• Icons and/or selections for the user to pick

Page 20: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 20

MessageBox example• Four basic types of

message boxes exist– Our example will

generate one of four message boxes depending on which mouse button you click and where you clicked it

• Will see a more complete description of message boxes shortly

#include <afxwin.h>

class CMessageWin : publicCFrameWnd {

public:

CMessageWin();

~CMessageWin();

afx_msg void OnLButtonDown

(UINT uFlags, CPoint point);

afx_msg void OnRButtonDown

(UINT uFlags, CPoint point);

private:

DECLARE_MESSAGE_MAP( )

};

Page 21: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 21

Body of the program (1)#include <afxwin.h>

#include “MessageWin.h"

CMessageWin::CMessageWin() {

Create(NULL, "MessageBox Example",WS_OVERLAPPEDWINDOW,CRect(100,100,500,500));

}

CMessageWin::~CMessageWin() { }

• Standard include files

• Constructor

• Destructor

Page 22: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 22

Body of the program (2)afx_msg void CMessageWin::OnLButtonDown

(UINT uFlags, CPoint point) { // handle left button click if (point.x < 200)

MessageBox("x coordinate is small","Stop Message Box", MB_ICONSTOP);

elseMessageBox("x coordinate is large",

"Question Message Box", MB_ICONQUESTION);}afx_msg void CMessageWin::OnRButtonDown

(UINT uFlags, CPoint point) { // handle right button click if (point.y < 200)

MessageBox("y coordinate is small","Exclamation Message Box", MB_ICONEXCLAMATION);

elseMessageBox("y coordinate is large", "Info Message Box", MB_ICONINFORMATION);

}

Page 23: Our Application with Clicks

Spring 2003 CS 325 Class Notes Page 23

Body of the program (3)BEGIN_MESSAGE_MAP

(CMesssageWin, CFrameWnd)ON_WM_LBUTTONDOWN( )ON_WM_RBUTTONDOWN( )

END_MESSAGE_MAP( )class CMessageApp : public CWinApp{ public: BOOL InitInstance() {

m_pMainWnd = new CMessageWin;m_pMainWnd->ShowWindow

(m_nCmdShow);m_pMainWnd->UpdateWindow();return TRUE;

}} MessageApp;

• Our message map section indicates we are interested in capturing two types of events– Left button down– Right button down

• Standard main portion