MFC CListCtrl 吃鼠标向上事件吗?

发布于 2024-12-09 06:50:45 字数 767 浏览 1 评论 0原文

除非双击,否则 CListCtrl 似乎不会发送鼠标释放事件。

我尝试从鼠标按下处理程序发送丢失的消息来进行补偿,但这会导致其他不良行为。然后我想我可以通过检查状态在鼠标移动处理程序中发送消息,以便更加准确。然而,这些都是可怕的黑客行为,除了丑陋之外,它们可能无法正常工作于派生控件的每种可能的实现。

如果有人知道为什么没有收到鼠标松开事件,我会很好奇。更重要的是如何使用LVS_OWNERDATA样式获取CListCtrl来像其他控件一样发送鼠标松开消息?

编辑:我知道< code>LVN_BEGINDRAG、LVN_BEGINRDRAG 等,但是为了使用这些,我需要阻止 WM_LBUTTONDOWNWM_RBUTTONDOWNWM_MOUSEMOVE 转到父窗口或挂接到 CWinAppEx/CMDIFrameWndExDragDropManager code> 这样我就可以为该控件制作一个特殊的一次性案例,以便与现有系统一起使用。

这是因为我有一个中央拖放管理器,它可以通知各种类型的控件何时开始拖动操作、何时结束、取消、更改动画、在自定义消息中传递源和目标的显示对象等。它需要足够灵活,可以根据控制、输入、所选或目标项目的类型、包括 3D 在内的不同控制类型,甚至不同的应用程序等,具有不同的启动方式和不同的操作。

It seems that CListCtrl doesn't send mouse up events unless there is a double click.

I've attempted sending the missing message from the mouse down handlers to compensate but that causes other undesirable behavior. Then I thought I could send the message within the mouse move handler to be a bit more accurate by checking state. However these are horrible hacks and apart from being ugly they may not work properly for every possible implementation of derived controls.

If anyone knows why mouse up events are not received I'd be curious. More importantly how do I get the CListCtrl using the LVS_OWNERDATA style to send mouse up messages like every other control?

EDIT: I am aware of LVN_BEGINDRAG, LVN_BEGINRDRAG, etc however in order to use these I would need to prevent WM_LBUTTONDOWN, WM_RBUTTONDOWN, and WM_MOUSEMOVE from going to the parent window or DragDropManager hooked into CWinAppEx/CMDIFrameWndEx so I could make a special one-off case for this control to work with the existing system.

This is because I have a central drag and drop manager which can notify various types of controls when to begin a drag operation, when to end, cancel, change animation, pass display objects for source and target in custom messages, etc. It needs to be flexible enough to have differing way of initiating as well as differing actions depending on the control, the input, the type of items selected or targeted, differing control types including 3D, or even different applications, etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

勿忘初心 2024-12-16 06:50:45

作为参考,这是我所拥有的有效方法,但这是一个可耻的黑客行为。如果没有人能想出比这更好的东西,那就太可悲了。

标题:

#pragma once

// CListCtrlEx
class CListCtrlEx : public CListCtrl
{
    DECLARE_DYNAMIC(CListCtrlEx)

public:
    CListCtrlEx();
    virtual ~CListCtrlEx();

    bool IsSelected(int index);
    BOOL SelectDropTarget(int item);

protected:
    DECLARE_MESSAGE_MAP()

    afx_msg void OnStateChanged(NMHDR* pNMHDR, LRESULT* pResult);

    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);

private:
    bool m_lbDown;
    bool m_rbDown;
};

实施:

#include "stdafx.h"
#include "ListCtrlEx.h"

// CListCtrlEx
IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)

CListCtrlEx::CListCtrlEx() : m_lbDown(false), m_rbDown(false)
{
}

CListCtrlEx::~CListCtrlEx()
{
}

BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
    ON_WM_LBUTTONDOWN()
    ON_WM_RBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_RBUTTONUP()
    ON_WM_MOUSEMOVE()

    ON_NOTIFY_REFLECT(LVN_ODSTATECHANGED, &CListCtrlEx::OnStateChanged)
END_MESSAGE_MAP()

// CListCtrlEx message handlers
void CListCtrlEx::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_lbDown = true;
    CListCtrl::OnLButtonDown(nFlags, point);
}

void CListCtrlEx::OnRButtonDown(UINT nFlags, CPoint point)
{
    m_rbDown = true;
    CListCtrl::OnRButtonDown(nFlags, point);
}

void CListCtrlEx::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_lbDown = false;
    CListCtrl::OnLButtonUp(nFlags, point);
}

void CListCtrlEx::OnRButtonUp(UINT nFlags, CPoint point)
{
    m_rbDown = false;
    CListCtrl::OnRButtonUp(nFlags, point);
}

void CListCtrlEx::OnMouseMove(UINT nFlags, CPoint point)
{
    if (m_lbDown && ((nFlags & MK_LBUTTON) == 0))
    {
        PostMessage(WM_LBUTTONUP,
            MAKEWPARAM(LOWORD(nFlags), HIWORD(nFlags)),
            MAKELPARAM(point.x, point.y));
    }

    if (m_rbDown && ((nFlags & MK_RBUTTON) == 0))
    {
        PostMessage(WM_RBUTTONUP,
            MAKEWPARAM(LOWORD(nFlags), HIWORD(nFlags)),
            MAKELPARAM(point.x, point.y));
    }

    CListCtrl::OnMouseMove(nFlags, point);
}

bool CListCtrlEx::IsSelected(int index)
{
    return (GetItemState(index, LVIS_SELECTED) & LVIS_SELECTED) != 0;
}

// highlight drop targets sort of like CTreeCtrl
BOOL CListCtrlEx::SelectDropTarget(int item)
{
    static int prevHighlight(-1);
    if (item >= 0 && item < GetItemCount())
    {
        if (item != prevHighlight)
        {
            if (prevHighlight >= 0)
            {
                SetItemState(prevHighlight, 0, LVIS_DROPHILITED); // remove highlight from previous target
                RedrawItems(prevHighlight, prevHighlight);
            }

            prevHighlight = item;
            SetItemState(item, LVIS_DROPHILITED, LVIS_DROPHILITED); // highlight target
            RedrawItems(item, item);

            UpdateWindow();
            return TRUE;
        }
    }
    else
    {
        for (int i(0); i < GetItemCount(); ++i)
            SetItemState(i, 0, LVIS_DROPHILITED); // un-highlight all
        prevHighlight = -1;
    }

    return FALSE;
}

void CListCtrlEx::OnStateChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
// MSDN:
// If a list-view control has the LVS_OWNERDATA style,
// and the user selects a range of items by holding down the SHIFT key and clicking the mouse,
// LVN_ITEMCHANGED notification codes are not sent for each selected or deselected item.
// Instead, you will receive a single LVN_ODSTATECHANGED notification code,
// indicating that a range of items has changed state.

    NMLVODSTATECHANGE* pStateChanged = (NMLVODSTATECHANGE*)pNMHDR;

    // redraw newly selected items
    if (pStateChanged->uNewState == LVIS_SELECTED)
        RedrawItems(pStateChanged->iFrom, pStateChanged->iTo);
}

For reference here's what I have that works but it's a shameful hack. If no one can come up with anything better than this that's really sad.

Header:

#pragma once

// CListCtrlEx
class CListCtrlEx : public CListCtrl
{
    DECLARE_DYNAMIC(CListCtrlEx)

public:
    CListCtrlEx();
    virtual ~CListCtrlEx();

    bool IsSelected(int index);
    BOOL SelectDropTarget(int item);

protected:
    DECLARE_MESSAGE_MAP()

    afx_msg void OnStateChanged(NMHDR* pNMHDR, LRESULT* pResult);

    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    afx_msg void OnMouseMove(UINT nFlags, CPoint point);

private:
    bool m_lbDown;
    bool m_rbDown;
};

Implementation:

#include "stdafx.h"
#include "ListCtrlEx.h"

// CListCtrlEx
IMPLEMENT_DYNAMIC(CListCtrlEx, CListCtrl)

CListCtrlEx::CListCtrlEx() : m_lbDown(false), m_rbDown(false)
{
}

CListCtrlEx::~CListCtrlEx()
{
}

BEGIN_MESSAGE_MAP(CListCtrlEx, CListCtrl)
    ON_WM_LBUTTONDOWN()
    ON_WM_RBUTTONDOWN()
    ON_WM_LBUTTONUP()
    ON_WM_RBUTTONUP()
    ON_WM_MOUSEMOVE()

    ON_NOTIFY_REFLECT(LVN_ODSTATECHANGED, &CListCtrlEx::OnStateChanged)
END_MESSAGE_MAP()

// CListCtrlEx message handlers
void CListCtrlEx::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_lbDown = true;
    CListCtrl::OnLButtonDown(nFlags, point);
}

void CListCtrlEx::OnRButtonDown(UINT nFlags, CPoint point)
{
    m_rbDown = true;
    CListCtrl::OnRButtonDown(nFlags, point);
}

void CListCtrlEx::OnLButtonUp(UINT nFlags, CPoint point)
{
    m_lbDown = false;
    CListCtrl::OnLButtonUp(nFlags, point);
}

void CListCtrlEx::OnRButtonUp(UINT nFlags, CPoint point)
{
    m_rbDown = false;
    CListCtrl::OnRButtonUp(nFlags, point);
}

void CListCtrlEx::OnMouseMove(UINT nFlags, CPoint point)
{
    if (m_lbDown && ((nFlags & MK_LBUTTON) == 0))
    {
        PostMessage(WM_LBUTTONUP,
            MAKEWPARAM(LOWORD(nFlags), HIWORD(nFlags)),
            MAKELPARAM(point.x, point.y));
    }

    if (m_rbDown && ((nFlags & MK_RBUTTON) == 0))
    {
        PostMessage(WM_RBUTTONUP,
            MAKEWPARAM(LOWORD(nFlags), HIWORD(nFlags)),
            MAKELPARAM(point.x, point.y));
    }

    CListCtrl::OnMouseMove(nFlags, point);
}

bool CListCtrlEx::IsSelected(int index)
{
    return (GetItemState(index, LVIS_SELECTED) & LVIS_SELECTED) != 0;
}

// highlight drop targets sort of like CTreeCtrl
BOOL CListCtrlEx::SelectDropTarget(int item)
{
    static int prevHighlight(-1);
    if (item >= 0 && item < GetItemCount())
    {
        if (item != prevHighlight)
        {
            if (prevHighlight >= 0)
            {
                SetItemState(prevHighlight, 0, LVIS_DROPHILITED); // remove highlight from previous target
                RedrawItems(prevHighlight, prevHighlight);
            }

            prevHighlight = item;
            SetItemState(item, LVIS_DROPHILITED, LVIS_DROPHILITED); // highlight target
            RedrawItems(item, item);

            UpdateWindow();
            return TRUE;
        }
    }
    else
    {
        for (int i(0); i < GetItemCount(); ++i)
            SetItemState(i, 0, LVIS_DROPHILITED); // un-highlight all
        prevHighlight = -1;
    }

    return FALSE;
}

void CListCtrlEx::OnStateChanged(NMHDR* pNMHDR, LRESULT* pResult)
{
// MSDN:
// If a list-view control has the LVS_OWNERDATA style,
// and the user selects a range of items by holding down the SHIFT key and clicking the mouse,
// LVN_ITEMCHANGED notification codes are not sent for each selected or deselected item.
// Instead, you will receive a single LVN_ODSTATECHANGED notification code,
// indicating that a range of items has changed state.

    NMLVODSTATECHANGE* pStateChanged = (NMLVODSTATECHANGE*)pNMHDR;

    // redraw newly selected items
    if (pStateChanged->uNewState == LVIS_SELECTED)
        RedrawItems(pStateChanged->iFrom, pStateChanged->iTo);
}
眼眸印温柔 2024-12-16 06:50:45

您不需要这些事件。该控件为您提供所需的一切。

从您的代码中我了解到您想要在列表控件中实现项目的拖放。
与其将一些无论如何都行不通的东西拼凑在一起并诽谤微软,不如以正确的方式去做:

处理 LVN_BEGINDRAG 通知开始拖动操作。

You don't need those events. The control provides you with everything you need.

From your code I gather that you want to implement drag'n'drop of items in the list control.
Instead of hacking together something that won't work anyway and badmouth MS, do it the right way instead:

Handle the LVN_BEGINDRAG notification to start the drag operation.

傲世九天 2024-12-16 06:50:45

Stefan的答案可能会为您提供最好的服务...

但是,您可以挂钩CListCtrl的winproc(地狱,在MFC中您可以子类化CListCtrl,提供您自己的虚拟WindowProc()并转发/拦截您想要的任何鼠标消息。

只是 标准 CListCtrl 类。

在运行时使用标准 MFC 子类控制机制来“替换”您希望的任何对话框或窗口上的

You may be best served by Stefan's answer....

However, you could hook the CListCtrl's winproc (hell, in MFC you could just subclass the CListCtrl, provide your own virtual WindowProc() and forward / intercept whatever mouse messages you want.

Just use the standard MFC subclass a control mechanic at runtime to "substitute" your class for the standard CListCtrl on whatever dialog or window you wish.

I assume you know how to do that?

时光倒影 2024-12-16 06:50:45

您可以使用 LVS_OWNERDRAWFIXED 样式,它使所有鼠标事件都来找您

You can use LVS_OWNERDRAWFIXED style, it makes all mouse events come to you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文