SetCapture的基本应用示例

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/tanaya/article/details/54030

怎么样可以捕获到鼠标进入和离开窗体呢?

这里要用到两个Api,即SetCapture和ReleaseCapture.

新建一个工程,将下面代码贴入代码区.按F5运行即可.

Option Explicit

Private Declare Function SetCapture Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function ReleaseCapture Lib "user32" () As Long

Private Sub Form_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    With Form1
    If Not ((X < 0) Or (Y < 0) Or (X > .Width) Or (Y > .Height)) Then
       ReleaseCapture
       If .Caption <> "进来了" Then
          .Caption = "进来了"
       End If
       SetCapture .hwnd
    Else
       .Caption = "离开了"
    End If
    End With
End Sub

现在把鼠标移动到窗体内外试试看.

                                                                               --- 程序:唐细刚

展开阅读全文

CListCtrl派生类不可以SetCapture()?

06-27

[code=C/C++]rnBEGIN_MESSAGE_MAP(CHexCtrl, CListCtrl)rn //AFX_MSG_MAP(CHexCtrl)rn ON_NOTIFY_REFLECT(LVN_ITEMCHANGING, OnItemchanging)rn ON_WM_ERASEBKGND()rn ON_NOTIFY_REFLECT(NM_CLICK, OnClick)rn ON_WM_LBUTTONDOWN()rn ON_WM_MOUSEMOVE()rn ON_WM_LBUTTONUP()rn ON_NOTIFY_REFLECT(NM_CUSTOMDRAW, OnCustomDraw)rn //AFX_MSG_MAPrnEND_MESSAGE_MAP()rnrnvoid CHexCtrl::OnClick(NMHDR* pNMHDR, LRESULT* pResult) rnrn // TODO: Add your control notification handler code herern m_bBeginSel = FALSE;rn ReleaseCapture();rn rn *pResult = 0;rnrnrnvoid CHexCtrl::OnLButtonDown(UINT nFlags, CPoint point) rnrn // TODO: Add your message handler code here and/or call defaultrn m_bBeginSel = TRUE;rn rn CWnd* pWnd = SetCapture();rn rn LVHITTESTINFO lvhit;rn lvhit.pt = point;rn int item = SubItemHitTest(&lvhit);rn rn if (item != -1 && (lvhit.flags & LVHT_ONITEM))rn rn int subitem = lvhit.iSubItem;rn if (subitem > 0 && subitem < 17)rn rn m_nBeginItem = MAKELONG(subitem, item);rn m_nEndItem = m_nBeginItem;rn rn rn rn Invalidate(); rn CListCtrl::OnLButtonDown(nFlags, point);rnrnrnvoid CHexCtrl::OnMouseMove(UINT nFlags, CPoint point) rnrn // TODO: Add your message handler code here and/or call default rn CListCtrl::OnMouseMove(nFlags, point);rnrn if (m_bBeginSel)rn rn LVHITTESTINFO lvhit;rn lvhit.pt = point;rn int item = SubItemHitTest(&lvhit);rn if (item != -1 && (lvhit.flags & LVHT_ONITEM))rn rn int subItem = lvhit.iSubItem;rn int id = MAKELONG(subItem, item);rn if (id == m_nEndItem)rn return;rn rn if (subItem > 0 && subItem < 17)rn rn m_nEndItem = id;rn rn //TRACE("%08X~%08X", min(m_nBeginItem,m_nEndItem), max(m_nBeginItem,m_nEndItem));rn rn CRect rcSubItem;rn GetItemRect(item, rcSubItem, LVIR_BOUNDS);rn rn //InvalidateRect(&rcSubItem);rn Invalidate();rn rn rn rnrnrnvoid CHexCtrl::OnLButtonUp(UINT nFlags, CPoint point) rnrn // TODO: Add your message handler code here and/or call defaultrn m_bBeginSel = FALSE;rn ReleaseCapture();rn rn CListCtrl::OnLButtonUp(nFlags, point);rnrn[/code]rnrnclass CHexCtrl : public CListCtrlrnrn以上代码发现,当鼠标按下,按出List,放开鼠标,OnLButtonUp并不响应,何解?rn还有发现当鼠标快速点击,OnLButtonUp也不响应,而响应了OnClick,这又何解? 论坛

没有更多推荐了,返回首页