【问题标题】:How to get screen X and Y of an Excel 2003 cell in C#如何在 C# 中获取 Excel 2003 单元格的屏幕 X 和 Y
【发布时间】:2011-06-10 02:45:12
【问题描述】:

在编写 C# Excel 2003 插件时,如何在 Excel 2003 中找到单元格的绝对位置(例如相对于屏幕[s])。

范围(例如 ActiveCell)的 Top 和 Left 属性似乎给出了相对于左上角单元格的 X 和 Y。 Window.Left 和 Top 给出了窗口的 X 和 Y,但我找不到获取中间位大小的方法(由工具栏等组成)。

这里的目的是显示一个与所选单元格相关的 WPF 表单,并与它相邻。

我觉得我在这里缺少一些基本的东西。非常感谢任何帮助!

【问题讨论】:

  • 我不确定你能做到这一点。您可以获得的最接近的是活动窗口(@98​​7654323@ 集合)的UsableHeightUsableWidth,以及所述窗口的TopLeft,以及主Excel 窗口的WidthHeight。这样你就可以在某种程度上获得非客户区。但是我无法知道窗口上方有多少非客户区(菜单栏、窗口标题栏等)以及下方有多少(状态栏)
  • 感谢您的帮助,InBetween!我没有研究过UsableHeightUsableWidth,但这是一个很好的建议。有可能通过检测是否显示状态栏来推断状态栏的大小(假设状态栏始终是固定高度)。有点“hacky”,但如果我能做到最好的话......

标签: c# excel excel-2003


【解决方案1】:

以下链接包含一些 VBA 代码,可能会为您指明正确的方向:Form Positioner

这比我想象的要复杂,但是如果您需要查找某些 Excel 命令栏的高度,他们示例中的以下 VBA 代码可能会有所帮助:

'
' we'll assume that the application's caption bar and the formula
' bar are the same height as the menu bar.  If we can't figure that out, use 26 as a default.
'
If Application.CommandBars.ActiveMenuBar.Visible = True Then
    DefaultCmdBarHeight = Application.CommandBars.ActiveMenuBar.Height
Else
    DefaultCmdBarHeight = cDefaultCmdBarHeight
End If
'
' We have to have a compenstating factor for command bars. Load an array
' with the heights of visible command bars. The index into the array is
' the RowIndex of the command bar, so we won't "double dip" if two or more
' command bars occupy the same row.
'
For Each CmdBar In Application.CommandBars
    With CmdBar
        If (.Visible = True) And (.Position = msoBarTop) Or (.Position = msoBarMenuBar) Then
            If .RowIndex > 0 Then
                VCmdArr(.RowIndex) = .Height
            End If
        End If
        If (.Visible = True) And (.Position = msoBarLeft) Then
            If .RowIndex > 0 Then
                HCmdArr(.RowIndex) = .Width
            End If
        End If
    End With
Next CmdBar

【讨论】:

    猜你喜欢
    • 2022-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 2010-12-19
    • 1970-01-01
    • 1970-01-01
    • 2014-04-15
    相关资源
    最近更新 更多