2
votes

I have created a small MFC Document View App in C++ and I am having some trouble receiving messages in a class that inherits from CStatic. I have managed to create the CStatic derivative and it is visible on my View however my message handlers are not being fired.

When using Spy++ it seems that window is only receiving WM_NCHITTEST and it is returning HTTRANSPARENT, which according to MSDN means:

"In a window currently covered by another window in the same thread (the message will be sent to underlying windows in the same thread until one of them returns a code that is not HTTRANSPARENT)."

Here is an exert from Spy++:

<000001> 001D1350 S WM_NCHITTEST xPos:128 yPos:167
<000002> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000003> 001D1350 S WM_NCHITTEST xPos:128 yPos:166
<000004> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000005> 001D1350 S WM_NCHITTEST xPos:128 yPos:165
<000006> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT
<000007> 001D1350 S WM_NCHITTEST xPos:128 yPos:164
<000008> 001D1350 R WM_NCHITTEST nHittest:HTTRANSPARENT

This seems strange because the CStatic derivative is the only child window of my view. I created it like this:

Create(pItem->Value->GetBuffer(), WS_CHILD | WS_VISIBLE | SS_CENTER, Rect, Parent);
ShowWindow(SW_SHOW);

where Parent is a pointer to the CView.

Any help would be really appreciated.

EDIT:

Foo.h

class Foo: public CStatic
{
    DECLARE_DYNAMIC(Foo)
public:
    Foo();
    virtual ~Foo();
    virtual void CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz);

protected:
    DECLARE_MESSAGE_MAP()
public:
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};

Foo.cpp

void Foo::CreateCtrl(CWnd * Parent, POINT TopLeft, SIZE sz)
{
    CRect Rect(TopLeft, sz);
    Create(pItem->Value->GetBuffer(), WS_CHILD | WS_VISIBLE | SS_CENTER, Rect, Parent);
    ShowWindow(SW_SHOW);
}

BEGIN_MESSAGE_MAP(Foo, CStatic)     
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

void Foo::OnLButtonUp(UINT nFlags, CPoint point)
{   
    AfxMessageBox("Hello World!");
    __super::OnLButtonUp(nFlags, point);
}
2
What message handlers are not being fired? Have you searched SO with CStatic?Edward Clements
The main one is OnLButtonUp. I did have a look around SO and though some questions are similar I don't think they are the same as mine.Shane Haw
can you post some code of how you are declaring your CStatic derived class and your OnLButtonUp handler?Edward Clements
Is that what you are looking for?Shane Haw

2 Answers

8
votes

See Microsoft's article "About Static Controls", and in particular this part:

WM_NCHITTEST: Returns HTCLIENT if the control style is SS_NOTIFY; otherwise, returns HTTRANSPARENT.

Once the window returns HTTRANSPARENT from WM_NCHITTEST, all further mouse messages go to the window underneath it in Z-order; in your case, the parent view. The window is "transparent" as far as mouse handling is concerned.

2
votes

After a bit of experimenting, it looks like setting and additional SS_NOTIFY style in Foo::CreateCtrl() gets MFC to call Foo::OnLButtonUp().

I am a bit confused with this style setting, specially after reading this SO post; the MSDN page for SS_NOTIFY just says "Sends the parent window STN_CLICKED, STN_DBLCLK, STN_DISABLE, and STN_ENABLE notification codes when the user clicks or double-clicks the control."

Maybe without the SS_NOTIFY style, it doesn't have to receive messages because they are not relayed to the parent?

Anyway, adding the SS_NOTIFY style seems to make it work!