TreeView vs. ListView MouseDown(长按)事件序列代码-执行-暂停差异 [英] TreeView vs. ListView MouseDown (long-press) Event Sequence Code-Execution-Pause Difference

查看:26
本文介绍了TreeView vs. ListView MouseDown(长按)事件序列代码-执行-暂停差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个 Excel VBA 项目(因为有人编辑了标题),我需要澄清这篇文章的内容.

我正在编写通过 VBA 代码模块遍历/工作的代码,以列出用户窗体上的过程和控件,并列出过程如何/哪些过程相互调用.

我试图在 VBA 编辑器的设计器窗口中的用户窗体上在运行时模拟用户窗体控件的抖动,以通知用户使用鼠标选择了该特定控件在无模式用户窗体上的 ListView/TreeView 的 ListItem/Node 上.

用文字描述很复杂,所以我附上了一张GIF.在 GIF 中,可以看到,在立即窗格中,执行 ListView 中的 MouseDown 和 ItemClick 事件后代码执行暂停,因为我没有释放按下的鼠标左键 (LMB).
所以我猜 ListViews 中的事件顺序是这样的:

  1. 鼠标按下
  2. ItemClick
    暂停
  3. MouseUp
  4. 点击.

因此,我将 jiggle 例程代码放在 ListView 的 ItemClick 事件中.

然而,当我尝试用 TreeView 做同样的事情时,我发现代码执行在 TreeView 的 MouseDown 事件之后暂停,如果我没有释放 LMB,没有继续执行 NodeClick 事件,如它与 ListView 一起使用,并且是 MouseDown 事件.

由于我将抖动代码放在 TreeView NodeClick 事件处理程序中(尚未发生),因此没有抖动效果.
但是在 LMB 释放后,控件的抖动效果非常直接,引人注目,因为在 MouseDown 事件结束时暂停后,代码继续执行,不间断,直到 MouseUp 事件结束.
这一系列事件可以在立即窗口的输出中看到,如下面的 GIF 所示.这里也很明显,如果鼠标左键被按下,则在处理 MouseDown 事件后代码执行暂停,没有进入 NodeClick 事件,就像在 ListView 中那样.

  1. 鼠标按下
    暂停
  2. 节点点击
  3. MouseUp
  4. 点击.

所以,情况就如上.
我试图将抖动代码放入 MouseDown 事件中.
但是,由于我需要 TreeView 中的 SelectedItem 来使我的 jiggle 代码正常工作,因此无论如何我都需要 NodeClick 事件.

我尝试在 MouseDown 事件中检查 TreeView 的 SelectedItem/selected-Node 但不幸的是,它只返回前一个 SelectedItem/selected-Node 而不是刚刚发生 MouseDown 事件的节点,这本身就有意义,因为 SelectedItem 尚未设置为新节点,因为 NodeClick 事件尚未发生.

我想,如果我可以在 MouseDown 事件时使用 HitTest 将节点置于鼠标下方,我就可以将该节点用作我的抖动代码中的 SelectedItem.
另一个障碍出现了,因为 TreeView HitTest 函数使用 X,Y 作为单数,这似乎是 ?Twips 而不是像素,我可能需要使用幻数 = 88,90 和神奇的 TwipsPerPixel 函数,并为不同的对象重新计算它显示器和分辨率.
所以,我寻找了不同的解决方案.

PS:作为一个缅甸人,我有责任让人们意识到缅甸目前正在发生一场反对残酷军事政变的革命,并恳请你们支持缅甸人民.谢谢.

解决方案

我找到了不同的解决方案,采用定制的 HitTest 函数的形式,使用 SendMessage API 调用 TreeView,而不是使用 TreeView 的默认 HitTest 函数,可在我认为这里的底线是:

  • ListViews 和 TreeViews 处理鼠标点击事件的不同之处在于 ListViews 似乎将 ItemClick 事件视为 MouseDown 事件序列的一部分,而 TreeViews 将 NodeClick 事件视为与 MouseDown 事件分开
  • 我不知道这背后的原因,但怀疑这可能与他们的起源有关

我写了这个问题+答案帖子,以帮助 VBA 程序员解决 ItemClick 与 NodeClick 事件序列差异的问题,并让他们知道那里有更好的 HitTest 例程,无需转换鼠标即可使用以像素为单位的坐标.

整个 VBA 项目是我的 VBA 项目信息工具(带有代码混淆器和可视化代码流工具)的一部分,在 GitHub.

备注: 我一生都无法在任何地方找到 ListViews 和 TreeViews 的 MouseClick 事件序列的官方描述.因此,不得不求助于 debug.print 1.TV_MouseDown" 东西.

PS:作为缅甸人,我恳请大家支持缅甸春季革命.
缅甸革命万岁!!!

This is an Excel VBA Project (because someone edited the title) and I need to clarify what this post is about.

I am writing code that walks/works through VBA Code Modules to list procedures as well as controls on UserForms and list how/what procedures call each other.

I was trying to simulate jiggling of a userform control, at run time, on a Userform in the VBA Editor's Designer window, to notify the user that this particular control, was selected, with the mouse on the ListItem/Node of ListView/TreeView on a modeless userform.

It's complicated to describe in words, so I attached a GIF. In the GIF, it can be seen, in the Immediate pane, that code execution paused after executing the MouseDown and ItemClick events in the ListView, because I didn't release the pressed Left Mouse Button (LMB).
So I guess the sequence of events in ListViews is like:

  1. MouseDown
  2. ItemClick
    Pause
  3. MouseUp
  4. Click.

And, thus, I put my jiggle routine code in ItemClick event of the ListView.

However, when I try to do the same thing with TreeView, I found that the code execution paused right after MouseDown event of the TreeView, if I didn't release the LMB, without going on to execute the NodeClick event, as it did with ListView and it's MouseDown Event.

Since I placed my jiggle code in TreeView NodeClick event handler, which has not occured yet, there was no jiggle effect.
But upon release of the LMB, the control's jiggle effect was quite immediate for the eye to catch, as the code execution continues after the pause at the end of MouseDown event, nonstop, up to the end of MouseUp Event.
This sequence of events can be seen in the Immediate window's output as shown in the GIF below. It is also evident here that the code execution paused after MouseDown event was handled, if the left mouse button was held down, without going on to NodeClick Event, as it did in ListView.

  1. MouseDown
    Pause
  2. NodeClick
  3. MouseUp
  4. Click.

So, the situation was laid out as above.
I tried to place the jiggle code into the MouseDown Event.
However, since I need the SelectedItem from the TreeView to get my jiggle code working, it's like I needed the NodeClick Event anyway.

I tried to examine the SelectedItem/selected-Node of the TreeView in the MouseDown Event but unfortunately, it only returned the previous SelectedItem/selected-Node rather than the Node on which the MouseDown Event just occured, which makes sense in itself, because SelectedItem is not yet set to new Node as NodeClick Event has not occured yet.

I thought that if only I can get the Node under the mouse at the time of MouseDown Event by using HitTest, I could use that Node as the SelectedItem in my jiggle code.
Another obstacle arises, in that, the TreeView HitTest function uses X,Y as Singles which appear to be in ?Twips rather than Pixels, I may be required to use magic numbers=88,90 and the magical TwipsPerPixel function and recalculate it for different monitors and resolutions.
So, I searched for a different solution.

PS:Being a Myanmar, I have a duty to promote awareness that there's a Revolution occuring right now in Myanmar against a cruel military coup and would like to plead you guys to please support the people of Myanmar. Thank you.

解决方案

I found the different solution in the form of a custom-made HitTest function, using the SendMessage API call to the TreeView, rather than using the TreeView's default HitTest function, available here.
The code sample there, was allowed to be used freely but not to be published on any website as mentioned in their copyright disclaimer.
So I guess I cannot share the code snippet here but I believe it's ok to share the link to that site.

I had to modify, the code mentioned there, a bit, to return the Node but not the handle to the Node as returned in their function.
I used that function because it uses Pixels as X,Y coordinates rather than the ?Twips that VBA uses.
Similar function for ListViews can also be found elsewhere if one googles hard enough.
So, it works like a charm and the control is jiggling now, inside the MouseDown Event, without using NodeClick event. I think that the bottom line here, is:

  • ListViews and TreeViews differ in handling mouseclick events in that ListViews seem to consider ItemClick Event as part of the MouseDown Event sequence and TreeViews consider NodeClick Event as separate from MouseDown Event
  • I have no idea about the reasoning behind this but suspect that it may be in their origins

I wrote up this Question+Answer post to help fellow VBA programmers with the ItemClick vs. NodeClick Event sequence discrepancy and also to let them know that there is a better HitTest routine out there, which can be used without having to convert mouse coordinates in pixels.

This whole VBA project is part of my VBA Project Info tool (with Code Obfuscator and Visual Codeflow tools) with the barebone-basic Alpha RC version available on GitHub.

Remarks: I cannot, for the life of me, find the official description of MouseClick Event sequences of ListViews and TreeViews anywhere. Thus, had to resort to debug.print "1.TV_MouseDown" stuff.

PS:Being a Myanmar, I urge y'All to please support the Myanmar Spring Revolution.
Vive la Myanmar Révolution!!!

这篇关于TreeView vs. ListView MouseDown(长按)事件序列代码-执行-暂停差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆