1

I am using a MFC dialog based application and have a subclassed CStatic control. I would like to receive WM_MOUSEWHEEL and other messages inside my subclassed control but somehow those messages never arrive.

Here is how my Dialog looks like:

enter image description here

I'm only doing some really simple drawing and want to be able to move my list up and down by scrolling.

I did already:

  • Change the Tab-Order to ensure focus on the subclassed CStatic control first
  • Overwrote OnNcHitTest to give focus to the subclassed CStatic all the time
  • Added a scroll bar to the side
  • Wrote message handler for WM_MOUSEWHEEL, WM_LBUTTONDOWN, WM_KEYDOWN and WM_VSCROLL
  • Tried catching the messages in PreTranslateMessage

Sadly nothing ever gets called when I'm scrolling inside the Dialog / Pressing a key or clicking with my mouse. The messages just don't arrive.

Here is my Mousewheel handler for example:

class CFolderView : public CStatic
{
   ...
   afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
   DECLARE_MESSAGE_MAP()
   ...
}

BEGIN_MESSAGE_MAP(CFolderView, CStatic)
    ON_WM_MOUSEWHEEL()
    ON_WM_KEYDOWN()
    ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

BOOL CFolderView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    MessageBox("Mouse Wheel moved!", "Debug", MB_OK);

    return CStatic::OnMouseWheel(nFlags, zDelta, pt);
}

I fail to understand why no input messages are being sent to my subclassed control. Is there some switch that enables input for a subclassed control?

Vinz
  • 3,030
  • 4
  • 31
  • 52
  • Andrew already gave you a good answer, but I'd like to ask why you don't use a listbox to derive from instead? They can be heavily customized including icons and text together. – Ulrich Eckhardt Oct 25 '15 at 08:47

1 Answers1

2

You cannot handle WM_MOUSEWHEEL in CStatic because it cannot get focus by design.

From MSDN:

The WM_MOUSEWHEEL message is sent to the focus window when the mouse wheel is rotated

By looking at your screenshot I'd suggest subclassing CListBox instead.

Andrew Komiagin
  • 6,446
  • 1
  • 13
  • 23
  • Why CButton? I'd have used CWnd by default. – Ulrich Eckhardt Oct 25 '15 at 08:46
  • Yes, just by looking at his screenshot I assume he needs `CListBox` derived class – Andrew Komiagin Oct 25 '15 at 08:59
  • Unfortunately, things aren't that simple: [What's with this MSH_MOUSEWHEEL message?](http://blogs.msdn.com/b/oldnewthing/archive/2008/08/06/8835316.aspx) – IInspectable Oct 25 '15 at 10:32
  • First of all, I believe and almost 100% sure that his task can be easily solved by using `CListBox`. The problem with `MSH_MOUSEWHEEL` is that it was originally designed like a workaround/hack solution for older OSs. So I would not suggest relying on that stuff. – Andrew Komiagin Oct 25 '15 at 10:42
  • I was actually referring to the second point the blog entry made, and that you omitted from the [documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/ms645617.aspx): *"Sent to the focus window when the mouse wheel is rotated. **The [DefWindowProc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms633572.aspx) function propagates the message to the window's parent.**"* – IInspectable Oct 25 '15 at 13:35
  • To IInspectable: Yes I absolutely agree that this approach that you've mentioned indeed can be used. On the other hand there is more simple and right approach for the problem. – Andrew Komiagin Oct 25 '15 at 13:45
  • Thank you all a lot for your input. Subclassing CListBox really solved the issues. My custom control is getting the WM_MOUSEWHEEL message together with the other ones I mentioned. There has to be a property for CStatic controls that makes them ignore inputs. Marked this as answer as subclassing CListBox solved my issue – Vinz Oct 25 '15 at 14:34