Click to See Complete Forum and Search --> : How Do I Detect Mouse Click on a CStatic Object?


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.

Chris Wheeler
May 28th, 1999, 11:40 AM
Your CStatic objects must have the WS_NOTIFY style. I tried to set that programmatically, but I ended up just clicking the Notify checkbox in the properties dialog box for that CStatic object because I couldn't get it to work right programmatically.

Chris R. Wheeler, MCP
Pensacola Christian College
Desktop Programmer
cwheeler@pcci.edu

KEhlar
May 28th, 1999, 12:23 PM
Thanks for the tip! Unfortunately my CStatic objects are not created via the resource manager; the number of objects is determined only after reading a file so I have to create them manually. I will look into setting the WS_NOTIFY style manually. Hopefully someone else here know how to do it. If you have an idea of where to start I'd appreciate it very much if you share it with me.

Thanks again!

Dan Haddix
May 28th, 1999, 01:42 PM
Here ya go...

1) Add the SS_NOTIFY style to each static control like so (not WS_NOTIFY)

m_Static.ModifyStyle(NULL, SS_NOTIFY);



2) Overload the OnCmdMsg function like so

BOOL CYourDlg::OnCmdMsg(UINT nID, int nCode, void* pExtra, AFX_CMDHANDLERINFO* pHandlerInfo)
{
if (nCode == STN_DBLCLK)
{
//Process double click here
}
return CDialog::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);
}




That's all there is to it.

KEhlar
June 1st, 1999, 08:53 AM
Thanks for your help! Boy it took me a while to figure it out. I ended up just adding SS_NOTIFY to the "dwStyle" parameter when calling CStatic::Create() and it worked. I don't understand why this is not specified in the instructions for creating message handlers on MSDN considering it's such a crucial point. Also if you look up CStatic::Create() it makes no mention of SS_NOTIFY whatsoever. I don't know how long it would've been before I get this to work without help from other programmers.

Thanks again!