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

送分小仙女□ 提交于 2020-01-03 05:02:47

问题


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!


回答1:


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


来源:https://stackoverflow.com/questions/6283084/how-to-get-screen-x-and-y-of-an-excel-2003-cell-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!