CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    How Do I Detect Mouse Click on a CStatic Object?

    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.


  2. #2
    Join Date
    May 1999
    Posts
    28

    Re: How Do I Detect Mouse Click on a CStatic Object?

    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
    [email protected]

  3. #3
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Re: How Do I Detect Mouse Click on a CStatic Object?

    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!


  4. #4
    Join Date
    May 1999
    Posts
    82

    Re: How Do I Detect Mouse Click on a CStatic Object?

    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.


  5. #5
    Join Date
    May 1999
    Location
    SF Bay
    Posts
    51

    Re: How Do I Detect Mouse Click on a CStatic Object?

    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!


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured