如何在C#中获取Excel 2003单元格的屏幕X和Y

时间:2011-06-08 17:53:42

标签: c# excel excel-2003

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

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

此处的目的是显示与所选单元格相关的WPF表单,并与其相邻。

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

1 个答案:

答案 0 :(得分:0)

以下链接包含一些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