是否可以在 Microsoft TreeView Control 6.0 (MSComctlLib.TreeCtrl.2) 中模拟三态复选框?

2024-02-09

我在 Microsoft Access 中使用 Microsoft TreeView Control 6.0。它似乎工作得很好,除了它似乎没有灰色状态,表明检查了一些但不是所有子节点。

我已经研究过使用我自己的图像来模拟复选框,但如果我这样做,我就必须删除真正的复选框,否则看起来每个项目都有两个复选框......但我没有任何复选框复选框,我不知道如何处理图像上的点击。

我可以找到很多人在其他语言/用途中对此控件有相同类型的问题,但我找不到 Microsoft Access 的解决方案。

如果有其他可用的东西可以为我提供带有三态复选框的层次结构,我很乐意转移到不同的控件。


经过一些研究和几个小时的编码后,我能够自己编写一个解决方案。

我必须添加一个 ImageList,将其关联到 TreeView,并为这三个状态中的每一个添加一个复选框的图像。谷歌图片搜索在这里节省了我一些时间:)。

'Enumeration for simulated tri-state checkboxes, matching up to the TreeView's associated Image List's Index
Private Enum CheckboxEnum
  Unchecked = 1
  Checked = 2
  Partial = 3
End Enum

'---------------------------------------------------------------------------------------
' Procedure : objTreeView_MouseDown
' Author    : Matty Brown
' Date      : 19/05/2014
' Purpose   : Because TreeView doesn't support tri-state checkboxes, these have to be simulated using images.
'---------------------------------------------------------------------------------------
'
Private Sub objTreeView_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
  Const CHECKBOX_WIDTH As Integer = 195 '195=13px

  Dim objNode As Node
  Set objNode = objTreeView.HitTest(x, y)

  If objNode Is Nothing Then
    'Miss
  Else
    'Find left side of node by moving left one pixel at a time until you fall off the node, then move one pixel to the right
    Dim intX As stdole.OLE_XPOS_PIXELS
    For intX = x To 0 Step -15
      If Not objNode Is objTreeView.HitTest(intX, y) Then
        If x <= intX + CHECKBOX_WIDTH Then
          'User clicked on the checkbox
          Select Case objNode.Image
            Case CheckboxEnum.Unchecked:
              objNode.Image = CheckboxEnum.Checked
            Case Else:
              objNode.Image = CheckboxEnum.Unchecked
          End Select

          'Recursively check child nodes
          Call CheckTreeNodes(objTreeView, objNode, objNode.Image)

          'Update parent node(s)
          Call UpdateParentNodes(objTreeView, objNode)
        Else
          'User clicked outside of the checkbox
          '
        End If

        Exit For
      End If
    Next
  End If
End Sub

'---------------------------------------------------------------------------------------
' Procedure : CheckTreeNodes
' Author    : Matty Brown
' Date      : 16/05/2014
' Purpose   : Checks or unchecks all of the child nodes for the specified node
'---------------------------------------------------------------------------------------
'
Private Sub CheckTreeNodes(ByRef tv As TreeView, ByRef nodNode As Node, ByVal Value As CheckboxEnum)
  Dim lngIndex As Long

  'Cascade change to children
  If nodNode.Children > 0 Then
    lngIndex = nodNode.Child.Index
    Call CheckTreeNodes(tv, tv.Nodes(lngIndex), Value)

    Do While lngIndex <> nodNode.Child.LastSibling.Index
      lngIndex = tv.Nodes(lngIndex).Next.Index
      Call CheckTreeNodes(tv, tv.Nodes(lngIndex), Value)
    Loop
  End If

  nodNode.Image = Value
End Sub

'---------------------------------------------------------------------------------------
' Procedure : CountChildNodes
' Author    : Matty Brown
' Date      : 19/05/2014
' Purpose   : Counts how many child nodes are checked or unchecked, so that a parent node can be set correctly
'---------------------------------------------------------------------------------------
'
Private Sub CountChildNodes(ByRef tv As TreeView, ByRef nodNode As Node, ByRef lngChecked As Long, ByRef lngUnchecked As Long)
  Dim lngIndex As Long

  'Check this node's children
  If nodNode.Children > 0 Then
    lngIndex = nodNode.Child.Index
    Call CountChildNodes(tv, tv.Nodes(lngIndex), lngChecked, lngUnchecked)

    Do While lngIndex <> nodNode.Child.LastSibling.Index
      lngIndex = tv.Nodes(lngIndex).Next.Index
      Call CountChildNodes(tv, tv.Nodes(lngIndex), lngChecked, lngUnchecked)
    Loop
  Else
  'Update totals
    Select Case nodNode.Image
      Case CheckboxEnum.Checked:
        lngChecked = lngChecked + 1
      Case CheckboxEnum.Unchecked:
        lngUnchecked = lngUnchecked + 1
    End Select
  End If
End Sub


'---------------------------------------------------------------------------------------
' Procedure : UpdateParentNodes
' Author    : Matty Brown
' Date      : 19/05/2014
' Purpose   : Steps through parent nodes, updating them according to how many checked/unchecked child nodes they have
'---------------------------------------------------------------------------------------
'
Private Sub UpdateParentNodes(ByRef tv As TreeView, ByRef nodNode As Node)
  Dim lngIndex As Long
  Dim nodParent As Node
  Dim lngChecked As Long, lngUnchecked As Long

  'If this node has no parents, there's nothing to update
  If nodNode.Parent Is Nothing Then Exit Sub
  Set nodParent = nodNode

  Do While Not nodParent.Parent Is Nothing
    Set nodParent = nodParent.Parent

    'Reset counters
    lngUnchecked = 0
    lngChecked = 0

    'Count children
    Call CountChildNodes(tv, nodParent, lngChecked, lngUnchecked)

    'Update parent nodes
    If lngUnchecked = 0 And lngChecked > 0 Then
      nodParent.Image = CheckboxEnum.Checked
    ElseIf lngUnchecked > 0 And lngChecked > 0 Then
      nodParent.Image = CheckboxEnum.Partial
    Else
      nodParent.Image = CheckboxEnum.Unchecked
    End If
  Loop
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

是否可以在 Microsoft TreeView Control 6.0 (MSComctlLib.TreeCtrl.2) 中模拟三态复选框? 的相关文章

随机推荐

  • 相机2输出位图

    我正在尝试使用谷歌移动视觉API https developers google com vision barcodes overview使用camera2模块 我遇到了很多麻烦 我正在使用谷歌的android Camera2Video h
  • 使 google-chrome 浏览器在页面加载时全屏显示

    即使您将其标记为重复 也请考虑在此处回答此问题因为出于某种原因 我无法让它与其他解决方案一起使用 尽管我试图寻求帮助 但没有人回复 我真正想要的是 document ready function browser goes fullscree
  • PHP 中 MySQL 结果的分页

    直到最近我才真正考虑过这个 分页 作为一个问题 当我坐下来专注于它时 我发现自己面临很多问题 我感兴趣的是一个基本的联系人管理系统 用户可以在其中添加 更新 删除 搜索联系人 搜索部分是我需要有效实现分页的地方 我的想法 ve 和 ve 点
  • Google Apps 脚本 - JavaScript 不工作

    我创建了一个简单的网络应用程序 但它不起作用 Code gs function doGet I think this works var output HtmlService createTemplateFromFile index eva
  • 使用 Java 8 的 cobertura-maven-plugin

    是我一个人的问题还是 cobertura maven plugin 不适用于 java 8 当它运行时我得到 INFO cobertura maven plugin 2 6 instrument default provider impl
  • Oozie Shell Action 的 stdout 和 stderr 输出

    在 Oozie 站点中 它说 Shell 操作的 stdout 和 stderr 输出被重定向到运行 shell 命令的 Oozie Launcher 映射减少作业任务 STDOUT 谁能告诉我具体应该去哪里看 Oozie 在 启动器 中运
  • 递归是如何工作的? [关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 请用最简单的方式解释递归是如何工作的
  • Robotium:测试运行未能完成。预期 N 次测试,已收到 (N-1)

    Android 测试仍然是我头疼的问题 我创建了最简单的应用程序只是为了弄清楚如何Robotium有效 每次测试失败时都会出现错误 Running tests Test running started Test failed to run
  • subprocess.call cd 不起作用[重复]

    这个问题在这里已经有答案了 In 3 pwd Out 3 u Users aarcher Desktop scripts In 5 subprocess call mkdir p os path expanduser file path n
  • 防止在 C++ 中重新绘制窗口

    我正在编写一个全局钩子 DLL 它需要在窗口上使用 GDI 进行一些绘图以响应事件 我的问题是正在绘制的窗口不断重新绘制自身 因此我绘制的内容在我想要的之前就被删除了 有什么办法可以阻止窗户在我需要的时间内绘制任何东西吗 我的钩子目前是WH
  • PHP $_POST 只有 id,没有名称

    有人可以向我解释一下吗 我有以下代码
  • 如何为 CustomClipper 创建的小部件制作合适的边框和阴影

    我有一个Container里面的小部件ClipPath它使用一个CustomClipper 一切正常 我有所需的小部件形状 但是 我找不到为这个自定义形状的小部件制作阴影的方法 另外 我想要一个自动跟随此自定义小部件边缘的轮廓 边框 再次没
  • 不同的 ActionBar.Tab 使用不同的颜色

    我想让 ActionBar 中的所有选项卡都有不同的颜色指示器 例如选项卡 1 为蓝色 选项卡 2 为红色等 为了实现这一点 我为所有颜色创建了不同的选择器 并将它们放在可绘制的不同 xml 中 在 style xml 中我通过以下方式调用
  • 使用正则表达式 OR 运算符解决 2 个条件

    我正在尝试使用 OR 运算符将 2 个正则表达式合并为 1 个 我有一个检查字母后跟 8 位数字是否匹配的方法 Regex IsMatch s A Z d 8 我有另一个只检查 9 位数字的程序 Regex IsMatch s d 9 现在
  • Kubernetes 记录命令 TLS 握手超时

    我正在运行开源 kube 版本 1 9 在运行时出现 TLS 握手超时错误kubectl logs命令 其他kubectl命令工作正常 etc kubernetes pki gt kubectl get pods NAME READY ST
  • 按键或值对 JavaScript 对象进行排序; ES6

    在我问之前 关于这个特定主题有很多讨论 其中大部分与 ES5 相关 并不一定适用于 ES6 我试图得到一些澄清 也许可以帮助下一个在互联网上寻找答案的人 这是专门针对 ES6 的 问题 考虑以下对象结构 const unsorted obj
  • 如何在 Delphi 中使用非托管导出

    我正在用 C 创建一个项目 我想使用不受管理的出口 http sites google com site robertgiesecke Home uploads TOC Samples 后来在Delphi中使用它 那么谁能解释一下 非托管导
  • Google 地图 Android Api v2 的问题

    我一直在尝试实现一个示例应用程序 以研究 Google 地图 API 的工作原理 我对配置本身感到震惊 我似乎无法运行该应用程序 这是我在 Github 上的代码 https github com siva54 SampleMapAppli
  • 使用AJAX和远程PHP上传文件

    我正在尝试使用 AJAx 构建一个上传器 我想使用 AJAX 而不是表单和 Iframe 因为我希望默认 CSS 样式与响应一起使用 问题是 我的 PHP 位于远程服务器上 我正在使用 Shopify 但他们不允许我使用他们的 PHP 服务
  • 是否可以在 Microsoft TreeView Control 6.0 (MSComctlLib.TreeCtrl.2) 中模拟三态复选框?

    我在 Microsoft Access 中使用 Microsoft TreeView Control 6 0 它似乎工作得很好 除了它似乎没有灰色状态 表明检查了一些但不是所有子节点 我已经研究过使用我自己的图像来模拟复选框 但如果我这样做