gpt4 book ai didi

c++ - 在滚动条上跟踪鼠标事件

转载 作者:行者123 更新时间:2023-12-02 10:29:41 24 4
gpt4 key购买 nike

我有一个被子类化为对话框的组件,并且正在处理该组件内的鼠标事件。我要实现的目标是根据鼠标事件来更改滚动条的可见性(即,滚动条仅在鼠标悬停在该区域时才会显示)。我正在处理TrackMouseEvent消息上的WM_MOUSEMOVE,并分别在ShowScrollBarWM_MOUSEHOVER上使用WM_MOUSELEAVE
除了鼠标悬停滚动条外,它均按预期工作。它不断发送WM_MOUSEHOVERWM_MOUSELEAVE消息,整个区域闪烁,就像每秒被重绘一样。
我不太了解ShowScrollbarTrackMouseEvent的行为。谁能帮助我找到解决这个问题的方法?
我的应用是在没有MFC的Win32下开发的,这是我的代码的小片段。

// gets called once on initialize
void CUDComment::InitScrollRange(int cyWindow)
{
int nTotal = _getTotalItemHeight();
int nRangeY = nTotal - cyWindow + _getItemHeight();
if( nRangeY < 0 ) nRangeY = 0;

if( m_nVerPos >= nRangeY )
{
m_nVerPos = nRangeY;
}

DWORD dwScrollState = 0;
// Hide scrollbar when the content is just one line
if( nTotal > cyWindow )
{
dwScrollState = 1;
m_bShowScroll = TRUE;
}
else
{
if( m_bShowScroll )
{
dwScrollState = 2;
m_bShowScroll = FALSE;
}
}

if( dwScrollState > 0 )
{
SCROLLINFO siVer;

siVer.cbSize = sizeof(SCROLLINFO);
siVer.fMask = SIF_PAGE | SIF_RANGE | SIF_POS;
siVer.nMin = 0;

if( dwScrollState == 2 )
{
siVer.nMax = 0;
siVer.nPage = 0;
siVer.nPos = 0;
}
else
{
siVer.nMax = nRangeY;
siVer.nPage = min(nRangeY, (int)_getItemHeight());
siVer.nPos = m_nVerPos;
}

SetScrollInfo(m_hWnd, SB_VERT, &siVer, TRUE);
}
}

void CUDComment::OnMouseMove(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
if( !m_bTracking )
{
TRACKMOUSEEVENT tme;

tme.cbSize = sizeof(tme);
tme.hwndTrack = m_hWnd;
tme.dwFlags = TME_LEAVE | TME_HOVER;
tme.dwHoverTime = 500;

m_bTracking = TrackMouseEvent(&tme);
}
}

void CUDComment::OnMouseHover(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
ShowScrollBar(m_hWnd, SB_VERT, TRUE);
}

void CUDComment::OnMouseLeave(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
m_bTracking = FALSE;
ShowScrollBar(m_hWnd, SB_VERT, FALSE);
}

最佳答案

我尝试了Johnathan的方法,但是当我为TME_LEAVE设置TME_NONCLIENTTrackMouseEvent触发器时,它并不起作用。我无法用鼠标捕获非客户区域。我所做的基本上是计算滚动条(GetSystemMetrics(SM_CXVSCROLL))及其坐标的大小,然后在鼠标悬停滚动条区域时忽略离开消息。因此,我在处理WM_MOUSELEAVE消息时基本上创建了一个异常(exception)情况。
总体代码与我的OnMouseLeave函数相似。

void CUDComment::OnMouseLeave(HWND hWnd, WPARAM wParam, LPARAM lParam)
{
POINT ptCurrent;

GetCursorPos(&ptCurrent);
ScreenToClient(hWnd, &ptCurrent);

if (!PtInRect(&m_rcScrollbar, ptCurrent))
{
ShowScrollBar(hWnd, SB_VERT, FALSE);
m_bTracking = FALSE;
}

}

关于c++ - 在滚动条上跟踪鼠标事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62807205/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com