ExcelHome技术论坛

 找回密码
 免费注册

QQ登录

只需一步,快速开始

快捷登录

搜索
EH技术汇-专业的职场技能充电站 妙哉!函数段子手趣味讲函数 Excel服务器-会Excel,做管理系统 效率神器,一键搞定繁琐工作
HR薪酬管理数字化实战 Excel 2021函数公式学习大典 Excel数据透视表实战秘技 打造核心竞争力的职场宝典
让更多数据处理,一键完成 数据工作者的案头书 免费直播课集锦 ExcelHome出品 - VBA代码宝免费下载
用ChatGPT与VBA一键搞定Excel WPS表格从入门到精通 Excel VBA经典代码实践指南
查看: 5312|回复: 10

[分享] 鼠标高亮显示的 解决 方法

[复制链接]

TA的精华主题

TA的得分主题

发表于 2012-8-24 22:59 | 显示全部楼层 |阅读模式
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
本帖最后由 win2009 于 2012-8-24 22:59 编辑

论坛高手的帖子,改造一下,解决显示高亮时,屏幕跟随的问题
值增加了2句代码
    ActiveSheet.ScrollArea = Selection.Address
    ActiveSheet.ScrollArea = ""
  1. Option Explicit

  2. Declare Function SetTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long

  3. Declare Function KillTimer Lib "user32" (ByVal hWnd As Long, ByVal nIDEvent As Long) As Long
  4. Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

  5. Type POINTAPI
  6.     x As Long
  7.     y As Long
  8. End Type

  9. Dim m_blnTimerOn As Boolean
  10. Dim m_lngTimerId As Long
  11. Dim m_NewRange As Range
  12. Dim m_OldRange As Range
  13. Sub StartTimer()
  14.     If Not m_blnTimerOn Then
  15.         m_lngTimerId = SetTimer(0, 0, 0.05, AddressOf TimerProc)
  16.         m_blnTimerOn = True
  17.         ' Set m_OldRange = ActiveSheet.Range("A1")
  18.     End If
  19. End Sub
  20. Public Function TimerProc(ByVal hWnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
  21.     Dim lngCurPos As POINTAPI
  22.     '
  23.     On Error Resume Next
  24.     GetCursorPos lngCurPos
  25.     Set m_NewRange = ActiveWindow.RangeFromPoint(lngCurPos.x, lngCurPos.y)
  26.     With m_NewRange
  27.         If m_OldRange.Address <> .Address Then
  28.             Dim r As Long, c As Long
  29.             r = .Row
  30.             c = .Column
  31.             Rows(r).Select
  32.             '            Union(Columns(c), Rows(r)).Select
  33.         End If
  34.     End With
  35.     Set m_OldRange = m_NewRange
  36.     TimerProc = 0
  37. End Function

  38. Sub StopTimer()
  39.     If m_blnTimerOn Then
  40.         KillTimer 0, m_lngTimerId
  41.         m_blnTimerOn = False
  42.     End If
  43. End Sub

  44. Public Sub 开始()
  45.     ActiveSheet.ScrollArea = Selection.Address
  46.     StartTimer
  47. End Sub
  48. Public Sub 结束()
  49.     ActiveSheet.ScrollArea = ""
  50.     StopTimer
  51. End Sub

复制代码




111.png

工作簿1.zip (19.29 KB, 下载次数: 159)




TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-24 23:03 | 显示全部楼层
发错地方,版主请移到vba论坛,谢谢

TA的精华主题

TA的得分主题

发表于 2012-8-25 00:58 | 显示全部楼层
不好不好 要修改一句
Rows(r).Select
换成下面两句
Union(Columns(c), Rows(r)).Select
            Cells(r, c).Activate

TA的精华主题

TA的得分主题

发表于 2012-8-25 01:06 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-25 02:30 | 显示全部楼层
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件       ★免费下载 ★       ★ 使用帮助
加载宏
鼠标经过高亮.zip (13.33 KB, 下载次数: 158)

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-25 10:45 | 显示全部楼层
版主说的是,但从实用角度看这个基本上就是最快的,没有停顿的
以前看了多个版本的,虽然不乏高明之作,但没有一个中意的,
原因:就是这点小事,还需多次点击鼠标,所以一直没有用这个功能,
等待明码出现,发现这个后,也觉得有问题,这两天想了一下,觉得
把单元格区域定住就行,结果真的行了,有了这个,我觉得很方便了

只是自己的感觉,软件方便实用的基本功能,胜过用户登峰造极的本领,
我们点灯熬油,孜孜以求的目的,不是要取得微软的表扬奖励,而是要
服务于求生存,和生存过程中追求快感愉悦自身天性。

借用一下古代一个生活在社会底层的歌妓的小词说明一下对学vba的看法
  1. 卜算子
  2.      严蕊
  3. 不是爱风尘,似被前缘误。花落花开自有时,总赖东君主。
  4. 去也终须去,住也如何住!若得山花插满头,莫问奴归处。
复制代码

TA的精华主题

TA的得分主题

发表于 2012-8-25 21:34 | 显示全部楼层
修修改改来个钩子的,应该比定时器合理些
  1. Option Explicit
  2. Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
  3. Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
  4. Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal mHook As Long) As Long
  5. Private Declare Function CallNextHookEx Lib "user32" (ByVal mHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As Long
  6. Private Const WH_MOUSE_LL As Long = 14
  7. Private Const WM_MOUSEMOVE = &H200
  8. Private Type POINTAPI
  9.     x As Long
  10.     y As Long
  11. End Type
  12. Public mHook As Long

  13. Public Sub StartBright()
  14.      If mHook = 0 Then
  15.         mHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, Application.Hinstance, 0)
  16.      End If
  17. End Sub

  18. Public Sub EnaBright()
  19.      If mHook <> 0 Then
  20.         Call UnhookWindowsHookEx(mHook)
  21.         mHook = 0
  22.      End If
  23. End Sub

  24. Private Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long
  25.      Dim PT As POINTAPI, Rng As Excel.Range
  26.      If nCode < 0 Then
  27.         HookProc = CallNextHookEx(mHook, nCode, wParam, lparam)
  28.         Exit Function
  29.      End If
  30.      If wParam = WM_MOUSEMOVE Then
  31.         Call GetCursorPos(PT)
  32.         On Error Resume Next
  33.         Set Rng = ActiveWindow.RangeFromPoint(PT.x, PT.y)
  34.         If Not Rng Is Nothing Then
  35.             Union(Columns(Rng.Column), Rows(Rng.Row)).Select
  36.             Rng.Activate
  37.         End If
  38.      End If
  39. End Function
复制代码

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-25 21:41 | 显示全部楼层
HHAAMM 发表于 2012-8-25 21:34
修修改改来个钩子的,应该比定时器合理些

谢谢,不知道怎么回事,我复制代码是这样

Option ExplicitPrivate Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As LongPrivate Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As LongPrivate Declare Function UnhookWindowsHookEx Lib "user32" (ByVal mHook As Long) As LongPrivate Declare Function CallNextHookEx Lib "user32" (ByVal mHook As Long, ByVal nCode As Long, ByVal wParam As Long, lparam As Any) As LongPrivate Const WH_MOUSE_LL As Long = 14Private Const WM_MOUSEMOVE = &H200Private Type POINTAPI    x As Long    y As LongEnd TypePublic mHook As LongPublic Sub StartBright()     If mHook = 0 Then        mHook = SetWindowsHookEx(WH_MOUSE_LL, AddressOf HookProc, Application.Hinstance, 0)     End IfEnd SubPublic Sub EnaBright()     If mHook <> 0 Then        Call UnhookWindowsHookEx(mHook)        mHook = 0     End IfEnd SubPrivate Function HookProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lparam As Long) As Long     Dim PT As POINTAPI, Rng As Excel.Range     If nCode < 0 Then        HookProc = CallNextHookEx(mHook, nCode, wParam, lparam)        Exit Function     End If     If wParam = WM_MOUSEMOVE Then        Call GetCursorPos(PT)        On Error Resume Next        Set Rng = ActiveWindow.RangeFromPoint(PT.x, PT.y)        If Not Rng Is Nothing Then            Union(Columns(Rng.Column), Rows(Rng.Row)).Select            Rng.Activate        End If     End IfEnd Function

TA的精华主题

TA的得分主题

发表于 2012-8-25 21:45 | 显示全部楼层

TA的精华主题

TA的得分主题

 楼主| 发表于 2012-8-25 21:48 | 显示全部楼层
HHAAMM 发表于 2012-8-25 21:34
修修改改来个钩子的,应该比定时器合理些

是了,但屏幕还是乱跑啊,不行的,版主再改改
屏幕乱动就没用
您需要登录后才可以回帖 登录 | 免费注册

本版积分规则

手机版|关于我们|联系我们|ExcelHome

GMT+8, 2025-1-15 23:38 , Processed in 0.028361 second(s), 10 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 1999-2023 Wooffice Inc.

沪公网安备 31011702000001号 沪ICP备11019229号-2

本论坛言论纯属发表者个人意见,任何违反国家相关法律的言论,本站将协助国家相关部门追究发言者责任!     本站特聘法律顾问:李志群律师

快速回复 返回顶部 返回列表