[ Home | Syllabus |Course Notes]
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.
A HotSpot is a section of a window which responds to a mouse click.
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); }
// 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.