如何在Excel中获取Excel 2003单元格的屏幕X和Y#
问题描述:
如何在编写C#Excel 2003外接程序时在Excel 2003中找到单元格的绝对位置(例如,相对于屏幕[s])。如何在Excel中获取Excel 2003单元格的屏幕X和Y#
范围的顶部和左侧属性(例如ActiveCell)似乎给出了X和Y相对于左上角的单元格。 Window.Left和Top给出窗口的X和Y,但是我找不到一个方法来获得中间位的大小(由工具栏等组成)。
这里的目的是显示一个与所选单元格相关的WPF表单,并且它位于它的相邻位置。
我觉得我在这里错过了一些基本的东西。任何帮助非常感谢!
答
以下链接有一些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
我不知道你可以做到这一点。你可以得到的最接近的是活动窗口('Application.Windows'集合)的'UsableHeight'和'UsableWidth',以及所述窗口的'Top'和'Left'以及主Excel的'Width'和'Height'窗口。这样你可以在一定程度上获得非客户区域。但是,我不知道要知道窗口上方有多少非客户区域(菜单栏,窗口标题栏等)以及下面有多少(状态栏) – InBetween 2011-06-08 18:26:55
感谢您的帮助,InBetween! 我没有看过'UsableHeight'和'UsableWidth',但这是一个很好的建议。可以通过检测是否显示状态栏来推断状态栏的数量(假设状态栏总是固定的高度)。有点'哈克',但如果它是我能做的最好的... – csharpfrood 2011-06-08 23:04:38