Drawing on graphs, but only within plot rectangle

I programatically create a graph, then draw some line s on top.
I want the lines to scale with the graph so I have used:
 SetDrawEnv xcoord = bottom, ycoord=left

This mostly does what I want, however, when zooming close-in, the drawn objects now extend into the margins of the graph.
is there any way to have the drawn object scale appropriately, but only within the plot rectangle of the graph?
You can draw 4 rectangles above the other objects to "mask" to the plot area. Use a higher drawing layer and plot-relative coordinates.

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
Bummer that this functionality does not seem to be built-in.
I am using your suggested work-around, which works fine for changing zoom levels but not for re-sizing a window.
Maybe something to add to the wish list.
Thanks for the suggestion though, I am making it work for now.
This should work even if the window is resized.

Macro PlotAreaMask(layerName)
    String layerName="UserBack"
    Prompt layerName, "Create Mask on layer", popup, "ProgBack;UserBack;ProgAxes;UserAxes;ProgFront;UserFont;"
   
    SetDrawLayer/K $layerName
    SetDrawEnv push
    SetDrawEnv xcoord=prel, ycoord=prel, linethick=0, fillfgc=(65535,65535,65535), save
    DrawRect -2,2,0,-2 // left
    DrawRect 1,-2,2,2 // right
    DrawRect 0,0,1,-2 // top
    DrawRect -2,1,2,2 // bottom
    SetDrawEnv pop
End


Macro ClearrPlotAreaMask(layerName)
    String layerName="UserBack"
    Prompt layerName, "Clear Mask on layer", popup, "ProgBack;UserBack;ProgAxes;UserAxes;ProgFront;UserFont;"
   
    SetDrawLayer/K $layerName
End


The default is UserBack with the expectation that drawing is done on the ProgBack layer; masking on this level prevents covering up the axes.

Since you can move the axes above the traces, you might draw axes on top of traces:
ModifyGraph axisOnTop=1

You might draw on the ProgAxes layer and put the mask on the UserAxes layer.

--Jim Prouty
Software Engineer, WaveMetrics, Inc.
Thanks, this is working perfectly now.
After drawing on the ProgBack layer, I added the following code:

SetDrawLayer/K UserBack
SetDrawEnv push
SetDrawEnv xcoord=prel, ycoord=prel, linethick=0, fillfgc=(65535,65535,65535), save
DrawRect -2,2,0,-2 // left
DrawRect 1,-2,2,2 // right
DrawRect 0,0,1,-2 // top
DrawRect -2,1,2,2 // bottom
SetDrawEnv pop


This allows me to resize and zoom at will while displaying the drawn objects only within the plot rectangle.
Thanks again!