Windows NT Systems Programming

[ Home | Syllabus |Course Notes]


Hot Spots


How do Buttons Work?

What do we know?

Questions: How does a CButton object

Let's simulate a CButton object a little bit by making hot spots on the parent window which sends message to the button message handlers.


HotSpots

A HotSpot is a section of a window which responds to a mouse click.

  1. We need to know how to identify (visually) a hot spot on the window
  2. Respond to a mouse click in this section of the window.

A little about Drawing in a Window: (Chapter 2)

Problem: Draw colored rectangles under the buttons in the "Edit Message Demo" to identify the "hot spots"

// Define Rectangular Hot Spots Under the normal buttons
CRect saveHotSpot(CPoint(0,650),CSize(100,50));
CRect loadHotSpot(CPoint(200,650),CSize(100,50));
CRect clearHotSpot(CPoint(400,650),CSize(100,50));
	// in main.h in the public functions of CMainWindow among the other message handlers
	afx_msg void OnPaint();	// paints hot spots

// in main.cpp in the MESSAGE_MAP entries
		ON_WM_PAINT()
// Repaint the hot spots
void CMainWindow::OnPaint()
{
	CPaintDC dc(this);
	CBrush* pOldBrush; // to restore current brush
	CBrush redBrush (RGB(255,0,0));
	CBrush greenBrush(RGB(0,255,0));
	CBrush blueBrush(RGB(0,0,255));
	
	// Now identify the "hot buttons" areas of the window underneath the real buttons
	pOldBrush = dc.SelectObject(&redBrush);
	dc.Rectangle(saveHotSpot);
	dc.SelectObject(&greenBrush);
	dc.Rectangle(loadHotSpot);
	dc.SelectObject(&blueBrush);
	dc.Rectangle(clearHotSpot);
	dc.SelectObject(pOldBrush);
}

A little about Mouse Handling (Chapter 3)

// in main.h, in the message handler public section of CMainWindow

afx_msg void OnLButtonUp(UINT, CPoint); // handle hot spot clicks

// in main.cpp, add appropriate MESSAGE_MAP entries and define handler

// Check to see if mouse click is in one of the Hot Spots
// If so, then send message to appropriate message handler
// THis simulates the action of pressing the appropriate buttons
void CMainWindow::OnLButtonUp(UINT nFlags, CPoint point)
{
	if(saveHotSpot.PtInRect(point))
		SendMessage(WM_COMMAND,IDC_SAVE, NULL);
	if(loadHotSpot.PtInRect(point))
		SendMessage(WM_COMMAND,IDC_LOAD, NULL);
	if(clearHotSpot.PtInRect(point))
		SendMessage(WM_COMMAND,IDC_CLEAR, NULL);
}

Drawing rectangles over the "hot spots" wasn't necessary to the logic.
just helps the user identify where a mouse click will have impact.

 

The entire example is available here.

HotSpot.gif (9898 bytes)

 


Copyright chris wild 1997.
For problems or questions regarding this web contact [Dr. Wild].
Last updated: September 29, 1997.