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

发布于 2024-11-14 16:06:02 字数 266 浏览 1 评论 0原文

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

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

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

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

How can I find the absolute position of a cell in Excel 2003 (e.g. relative to the screen[s]) when writing a C# Excel 2003 Add-in.

The Top and Left properties of a Range (such as ActiveCell) seem to give the X and Y relative to the top-left hand cell. Window.Left and Top give the X and Y of the window, but I can't find a way to get the size of the bit in the middle (consisting of Toolbars and such).

The aim here is to display a WPF form that relates to the selected cell, and is positioned adjacent to it.

I feel like I'm missing something basic here. Any help greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2024-11-21 16:06:02

以下链接包含一些 VBA 代码,可能可以为您指明正确的方向: 表单定位器

它比我想象的更复杂,但如果您需要查找某些 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

The following link has some VBA code, which might be able to point you in the right direction: Form Positioner .

It's more involved than I would have thought, but if you need to find the height of some of the Excel commandbars, the following VBA code in their example might help:

'
' 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文