2011-06-08 40 views
0

如何在編寫C#Excel 2003外接程序時在Excel 2003中找到單元格的絕對位置(例如,相對於屏幕[s])。如何在Excel中獲取Excel 2003單元格的屏幕X和Y#

範圍的頂部和左側屬性(例如ActiveCell)似乎給出了X和Y相對於左上角的單元格。 Window.Left和Top給出窗口的X和Y,但是我找不到一個方法來獲得中間位的大小(由工具欄等組成)。

這裏的目的是顯示一個與所選單元格相關的WPF表單,並且它位於它的相鄰位置。

我覺得我在這裏錯過了一些基本的東西。任何幫助非常感謝!

+0

我不知道你可以做到這一點。你可以得到的最接近的是活動窗口('Application.Windows'集合)的'UsableHeight'和'UsableWidth',以及所述窗口的'Top'和'Left'以及主Excel的'Width'和'Height'窗口。這樣你可以在一定程度上獲得非客戶區域。但是,我不知道要知道窗口上方有多少非客戶區域(菜單欄,窗口標題欄等)以及下面有多少(狀態欄) – InBetween 2011-06-08 18:26:55

+0

感謝您的幫助,InBetween! 我沒有看過'UsableHeight'和'UsableWidth',但這是一個很好的建議。可以通過檢測是否顯示狀態欄來推斷狀態欄的數量(假設狀態欄總是固定的高度)。有點'哈克',但如果它是我能做的最好的... – csharpfrood 2011-06-08 23:04:38

回答

0

以下鏈接有一些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 
相關問題