ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Left of an object on a chart sheet (https://www.excelbanter.com/excel-programming/390514-left-object-chart-sheet.html)

mrt

Left of an object on a chart sheet
 
Hi there,

Here is a problem with no solution so far:
- Create an empty chart, and make it be a chart sheet
- Then add a rectangle on it
- If ActiveChart.SizeWithWindow is set to false, how do you know the
screencoordinates of the Rectangle ? Left will only give you the position
inside the chartarea, and ChatArea.Left is 4 (which is not relative to the
screen)?

Thanks,

MrT


Bernie Deitrick

Left of an object on a chart sheet
 
MrT,

What, exactly, is it that you are trying to do? Why do you need the screencoordinates?

HTH,
Bernie
MS Excel MVP


"MrT" wrote in message
...
Hi there,

Here is a problem with no solution so far:
- Create an empty chart, and make it be a chart sheet
- Then add a rectangle on it
- If ActiveChart.SizeWithWindow is set to false, how do you know the
screencoordinates of the Rectangle ? Left will only give you the position
inside the chartarea, and ChatArea.Left is 4 (which is not relative to the
screen)?

Thanks,

MrT




mrt

Left of an object on a chart sheet
 
What, exactly, is it that you are trying to do? Why do you need the
screencoordinates?

I know the mouse coordinates which are based on the screen, and want to draw
a shaphe under the mouse cursor, not the chart position/coordinates. So I
need to know either the mouse position relative to the chartarea or to know
the screen coordinates of the chartarea (or of anything on the chart).

Regards,

MrT



Peter T

Left of an object on a chart sheet
 
The chart area on a chart sheet is sized to the inside margins of the
sheet's page set-up. It's probably possible to calaculate your shape's
screen coordinates but quite a lot of work. Another way is to add and size
a chartobject over the shape. This has a 'window' so pretty straightforward
to get its window coordinates which should be within a pixel or two of the
shape (assuming it doesn't have a thick border). Something like this -

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" _
(ByVal hwnd1 As Long, ByVal hwnd2 As Long, _
ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Private Declare Function GetWindowRect Lib "user32" _
(ByVal HWND As Long, lpRect As RECT) As Long

Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Sub GetShapeScreenCoords()
Dim shp As Shape, chtObj As ChartObject
Dim hwnd1&, hwnd2&, hwnd3&
Dim shpRect As RECT

On Error Resume Next
Set shp = ActiveSheet.Shapes(1)
On Error GoTo 0

If shp Is Nothing Then
MsgBox "no shape on sheet"
Exit Sub
End If

hwnd1 = FindWindow("XLMAIN", Application.Caption)
hwnd2 = FindWindowEx(hwnd1, 0&, "XLDESK", vbNullString)

With shp
Set chtObj = ActiveSheet.ChartObjects.Add( _
.Left, .Top, .Width, .Height)
End With

chtObj.Activate

hwnd3 = FindWindowEx(hwnd2, 0&, "EXCELE", vbNullString)
Call GetWindowRect(hwnd3, shpRect)

chtObj.Delete

With shpRect
' the shape's pixel coordinates
Debug.Print "x1 " & .Left, "y1 " & .Top
Debug.Print "x2 " & .Right, "y2 " & .Bottom
End With

End Sub

Of course I'm assuming it's some shape's coordinates you're after as you
asked in your OP.

Regards,
Peter T


"MrT" wrote in message
...
What, exactly, is it that you are trying to do? Why do you need the

screencoordinates?

I know the mouse coordinates which are based on the screen, and want to

draw
a shaphe under the mouse cursor, not the chart position/coordinates. So I
need to know either the mouse position relative to the chartarea or to

know
the screen coordinates of the chartarea (or of anything on the chart).

Regards,

MrT





Jon Peltier

Left of an object on a chart sheet
 
I need to follow up on Peter's post, but let me give you some advice based
on many hours of unsuccessful trials. I have had no joy when trying to deal
with mouse and screen coordinates in a chart sheet when SizeWithWindow is
false. (I can make it work flawlessly in an embedded chart and in a chart
sheet that sizes to the window.) There is no coordinate consistency in VBA
for this, since the window can be scrolled, and there's no way to get the
scroll position. Perhaps Peter's API approach might help with that, but I
can predict it will be a tedious frustrating process to work out.

- Jon
-------
Jon Peltier, Microsoft Excel MVP
Tutorials and Custom Solutions
Peltier Technical Services, Inc. - http://PeltierTech.com
_______


"MrT" wrote in message
...
Hi there,

Here is a problem with no solution so far:
- Create an empty chart, and make it be a chart sheet
- Then add a rectangle on it
- If ActiveChart.SizeWithWindow is set to false, how do you know the
screencoordinates of the Rectangle ? Left will only give you the position
inside the chartarea, and ChatArea.Left is 4 (which is not relative to the
screen)?

Thanks,

MrT





All times are GMT +1. The time now is 02:49 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com