簡體   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.

Range 的 Top 和 Left 屬性(例如 ActiveCell)似乎給出了相對於左上角單元格的 X 和 Y。 Window.Left 和 Top 給出了 window 的 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM