简体   繁体   English

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

[英]How to get screen X and Y of an Excel 2003 cell in C#

How can I find the absolute position of a cell in Excel 2003 (eg relative to the screen[s]) when writing a C# Excel 2003 Add-in. How can I find the absolute position of a cell in Excel 2003 (eg 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. Range 的 Top 和 Left 属性(例如 ActiveCell)似乎给出了相对于左上角单元格的 X 和 Y。 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). Window.Left 和 Top 给出了 window 的 X 和 Y,但我找不到获取中间位大小的方法(包括工具栏等)。

The aim here is to display a WPF form that relates to the selected cell, and is positioned adjacent to it.此处的目的是显示与所选单元格相关的 WPF 表格,并位于其附近。

I feel like I'm missing something basic here.我觉得我在这里缺少一些基本的东西。 Any help greatly appreciated!非常感谢任何帮助!

The following link has some VBA code, which might be able to point you in the right direction: Form Positioner .以下链接有一些 VBA 代码,它可能会为您指明正确的方向: 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:它比我想象的要复杂,但是如果您需要查找某些 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM