Vali Nicolae
Vali Nicolae

Reputation: 21

OnLButtonDown() is not fired if I click an item in my dialog (ListBox, CheckBox etc) but it fires ok if I click background or Static Text

If I click anywhere on the dialog window, like in the "background", or on a Static Text, the function OnLButtonDown() is fired. But if I click a ListBox/EditBox/Combo/Calendar/Checkbox etc is not fired. I thought that because these have control variables atached to it, and the Static Text don't. But adding a new Listbox to my Dialog and testing, I see that it doesn't fire either, so now I am confused...

I added OnLButtonDown() with the Class Wizard, it appears in:

BEGIN_MESSAGE_MAP(CMFCTesting2Dlg, CDialogEx)
    ON_WM_SYSCOMMAND()
// other handlers, etc
    ON_WM_LBUTTONDOWN()
    ON_WM_LBUTTONUP()
END_MESSAGE_MAP()

My function:

void CMFCTesting2Dlg::OnLButtonDown(UINT nFlags, CPoint point)
{
    AfxMessageBox(CString("BUTTON DOWN"));
    CDialogEx::OnLButtonDown(nFlags, point);
}

I tried calling CDialogEx:: ... before AfxMessageBox... but same result.

The CListBox item, has the Notify option set to True, as I seen some advices in other posts.

I suspect that the WM notification message is captured somehow by the ListBox, and not sent "forward" to my CMFCTesting2Dlg, but I understood that this should happen with the Notify option set to True, on the ListBox, no? (well, obviously, not...)

Sorry, I am new to dealing with WM in MFC.

BTW, I use Visual Studio 2010 Ultimate, and this is a Visual C++ - MFC- MFC Application - Dialog Based project.

How can I capture this mouse down event if clicked on a listbox / combo / etc?

On the LONG STORY, I am actually trying to accomplish this issue: I have two listboxes (lets say), and I want to scroll them synchronously, when user scrolls one of them, others must scroll the same (or update in the next moment). And I thought of using on mouse down to track the position of every Listbox (with the GetTopIndex), and on mouse up, to GetTopIndex again and compare with the previous ones. If a change was made, then a listbox was scrolled and then update all listboxes with SetTopIndex. The unfriendly solution for the user, but simpler for me, would be clicking a button that does this verification / update, but its not elegant at all, and it can only set them at the same level, but can't determine which one was scrolled last time. This automatically scrolling of the listboxes should be made just for displaying, it is not important the selections in the listboxes. I tried using the Message Type in the Add Event Handler for the Listbox, but none that are displayed work for my problem, KillFocus and SetFocus, are not fired if the scroll-bar is dragged, only if an item in the listbox is clicked... and I didn't succeed on the others message type either, from the Add Event Handler.

Upvotes: 2

Views: 6259

Answers (2)

TheSteve
TheSteve

Reputation: 1168

As far as getting the List Controls to sync scrolling goes, your best bet would be to intercept WM_VSCROLL by subclassing CListCtrl or perhaps by PreTranslateMessage, and then calling SetScrollInfo on the other list. If the number of items in the lists are the same, it should be fairly simple.

Upvotes: 1

Mark Ransom
Mark Ransom

Reputation: 308452

Once a window handles a message, it stops being sent to the other windows beneath it. A static window doesn't handle a mouse down, so it goes to the dialog. The other controls all handle it in some fashion (setting focus at least) so it never gets to the dialog.

You can override the OnLButtonDown handler in each of the controls to do something else once they've finished their default handling in the base class. You might also get to see the message in the PreTranslateMessage method of the dialog.

Upvotes: 1

Related Questions