开发者

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

开发者 https://www.devze.com 2023-03-11 03:27 出处:网络
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.

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 appreciate开发者_如何学God!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消