KEhlar
May 28th, 1999, 10:41 AM
I'm creating a "marquee"-like desktop application. What I have so far is a list of CStatic objects on the main CDialog window. I want to be able to detect mouse click on any of the CStatic objects. I followed the instructions by adding the Windows Message Handler "WMLButtonDown" which created the "OnLButtonDown" function. For some reason it just would not detect the mouse click! Here is the relevant code:
-------- CRecord.h ---------
#if !defined(AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_)
#define AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_
#include "RecordSet.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Record.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CRecord window
class CRecord : public CStatic
{
// Construction
public:
CRecord( LPCTSTR lpszText, const RECT& rect, CWnd* pParentWnd, UINT nID = 0xffff);
// Attributes
public:
COLORREF m_bgColor; // Variable that holds the background color.
COLORREF m_fgColor; // Variable that holds the foreground color.
CBrush m_bgBrush; // Variable that holds the background brush.
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRecord)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CRecord();
// Generated message map functions
protected:
//{{AFX_MSG(CRecord)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_)
--------- CRecord.cpp -----------
// Record.cpp : implementation file
//
#include "stdafx.h"
#include "Ticker.h"
#include "Record.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRecord
CRecord::CRecord( LPCTSTR lpszText, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
// Generate a random color for this label.
int rand_color = rand() % 6;
if (rand_color == 1)
m_fgColor = RGB(255,0,0);
else if (rand_color == 2)
m_fgColor = RGB(0,255,0);
else if (rand_color == 3)
m_fgColor = RGB(255,255,0);
else if (rand_color == 4)
m_fgColor = RGB(255,0,255);
else if (rand_color == 5)
m_fgColor = RGB(0,255,255);
else if (rand_color == 6)
m_fgColor = RGB(255,255,255);
m_bgColor = RGB(0,0,0);
m_bgBrush.CreateSolidBrush(m_bgColor);
Create(lpszText, WS_CHILD | WS_VISIBLE, rect, pParentWnd, nID);
}
CRecord::~CRecord()
{
}
BEGIN_MESSAGE_MAP(CRecord, CStatic)
//{{AFX_MSG_MAP(CRecord)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRecord message handlers
HBRUSH CRecord::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
pDC->SetTextColor(m_fgColor);
pDC->SetBkColor(m_bgColor);
// Return the brush handle
return m_bgBrush;
}
void CRecord::OnLButtonDown(UINT nFlags, CPoint point)
{
m_fgColor = RGB(0,0,255);
m_bgColor = RGB(255,0,0);
m_bgBrush.DeleteObject();
m_bgBrush.CreateSolidBrush(m_bgColor);
// Redraw the control
Invalidate();
CStatic::OnLButtonDown(nFlags, point);
}
---------
Thanks for any input.
-------- CRecord.h ---------
#if !defined(AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_)
#define AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_
#include "RecordSet.h" // Added by ClassView
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// Record.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CRecord window
class CRecord : public CStatic
{
// Construction
public:
CRecord( LPCTSTR lpszText, const RECT& rect, CWnd* pParentWnd, UINT nID = 0xffff);
// Attributes
public:
COLORREF m_bgColor; // Variable that holds the background color.
COLORREF m_fgColor; // Variable that holds the foreground color.
CBrush m_bgBrush; // Variable that holds the background brush.
// Operations
public:
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CRecord)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CRecord();
// Generated message map functions
protected:
//{{AFX_MSG(CRecord)
afx_msg HBRUSH CtlColor(CDC* pDC, UINT nCtlColor);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_RECORD_H__26B5FD80_14DF_11D3_BFE4_0000F8784E54__INCLUDED_)
--------- CRecord.cpp -----------
// Record.cpp : implementation file
//
#include "stdafx.h"
#include "Ticker.h"
#include "Record.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CRecord
CRecord::CRecord( LPCTSTR lpszText, const RECT& rect, CWnd* pParentWnd, UINT nID)
{
// Generate a random color for this label.
int rand_color = rand() % 6;
if (rand_color == 1)
m_fgColor = RGB(255,0,0);
else if (rand_color == 2)
m_fgColor = RGB(0,255,0);
else if (rand_color == 3)
m_fgColor = RGB(255,255,0);
else if (rand_color == 4)
m_fgColor = RGB(255,0,255);
else if (rand_color == 5)
m_fgColor = RGB(0,255,255);
else if (rand_color == 6)
m_fgColor = RGB(255,255,255);
m_bgColor = RGB(0,0,0);
m_bgBrush.CreateSolidBrush(m_bgColor);
Create(lpszText, WS_CHILD | WS_VISIBLE, rect, pParentWnd, nID);
}
CRecord::~CRecord()
{
}
BEGIN_MESSAGE_MAP(CRecord, CStatic)
//{{AFX_MSG_MAP(CRecord)
ON_WM_CTLCOLOR_REFLECT()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRecord message handlers
HBRUSH CRecord::CtlColor(CDC* pDC, UINT /*nCtlColor*/)
{
pDC->SetTextColor(m_fgColor);
pDC->SetBkColor(m_bgColor);
// Return the brush handle
return m_bgBrush;
}
void CRecord::OnLButtonDown(UINT nFlags, CPoint point)
{
m_fgColor = RGB(0,0,255);
m_bgColor = RGB(255,0,0);
m_bgBrush.DeleteObject();
m_bgBrush.CreateSolidBrush(m_bgColor);
// Redraw the control
Invalidate();
CStatic::OnLButtonDown(nFlags, point);
}
---------
Thanks for any input.