$INCLUDE "windows.inc"
DECLARE IMPORT, LoadCursor ALIAS LoadCursorA(hInstance AS INT,lpCursorName AS POINTER),INT
DECLARE IMPORT, GetCursorPos(lppoint:POINTER)
DECLARE IMPORT, ScreenToClient(HWND:uint, lppoint:POINTER)
WINDOW figure, plotwin
DOUBLE xmin, xmax, ymin, ymax :DOUBLE xfactor, xshift, yfactor, yshift :DEF MouseDownL, MouseDownR, MouseStartX, MouseStartY:INT
CONST Black=0
Figure_Load()
Resize()
Refresh_Scale()
Plot_Graph()
WAITUNTIL figure=0
END
SUB Figure_Load()
OPENWINDOW figure, -600,100,500,400, @MINBOX | @MAXBOX | @SIZE | @NOAUTODRAW, 0, "Figure - Move by dragging mouse in window", &RoutineFigure
CENTERWINDOW figure
OPENWINDOW plotwin, 0,0,200,200, @NOCAPTION | @NOAUTODRAW, figure, "plot", &RoutinePlotWin
_SetWindowLong(plotwin.hwnd,-20,512)
_SetWindowPos(plotwin.hwnd,NULL,0,0,0,0,39)
CfgSetDefault()
RETURN
ENDSUB
SUB RoutineFigure()
SELECT @MESSAGE
CASE @IDCLOSEWINDOW
CLOSEWINDOW plotwin
CLOSEWINDOW figure
CASE @IDSIZECHANGED
Resize()
Refresh_Scale()
Plot_Graph()
ENDSELECT
RETURN
ENDSUB
SUB RoutinePlotwin()
SETTYPE @HITWINDOW, WINDOW
SELECT @MESSAGE
CASE @IDPAINT
Refresh_Scale()
Plot_Graph()
CASE @IDLBUTTONDN
_SetCapture(*@HITWINDOW.hwnd)
GetCursorPos(&@MOUSEX)
ScreenToClient(*@HITWINDOW.hwnd, &@MOUSEX)
MouseStartX = @MOUSEX
MouseStartY = @MOUSEY
SETCURSOR plotwin, @CSCUSTOM, LoadCursor(NULL, IDC_SIZEALL) : CASE @IDLBUTTONUP
_ReleaseCapture()
SETCURSOR plotwin, @CSARROW : CASE @IDMOUSEMOVE
if (_GetCapture() = *@HITWINDOW.hwnd)
GetCursorPos(&@MOUSEX)
ScreenToClient(*@HITWINDOW.hwnd, &@MOUSEX)
xmin -= (@MOUSEX-MouseStartX) / xfactor
xmax -= (@MOUSEX-MouseStartX) / xfactor
ymin -= (@MOUSEY-MouseStartY) / yfactor
ymax -= (@MOUSEY-MouseStartY) / yfactor
MouseStartX = @MOUSEX
MouseStartY = @MOUSEY
Refresh_Scale()
Plot_Graph()
ENDIF
ENDSELECT
RETURN
ENDSUB
SUB Plot_Graph()
INT L,T,W,H
DOUBLE xscreen, yscreen
INT hdc, hdcMem, hbmMem
INT oldBmp, oldBrush, oldPen, oldFont
GETCLIENTSIZE(plotwin, L,T,W,H)
hdc = _GetDC(plotwin.hwnd)
hdcMem = _CreateCompatibleDC(0)
hbmMem = _CreateCompatibleBitmap(hdc, W, H)
oldBmp = _SelectObject(hdcMem, hbmMem)
oldBrush = _SelectObject(hdcMem, _CreateSolidBrush(RGB(255,255,255)))
oldPen = _SelectObject(hdcMem, _CreatePen(PS_SOLID,1,RGB(255,255,255)))
INT textW, textH
INT fontsize, fontweight
STRING fontface
fontface = "Courier New"
fontsize = 10
fontweight = 400
SETFONT plotwin, fontface, fontsize, fontweight, 0
GETTEXTSIZE(plotwin, "A", textW, textH) : oldFont = _SelectObject(hdcMem, _CreateFont(textH,0,0,0,fontweight, 0,0,0,0,0,0,0,0, fontface))
_SetTextColor(hdcMem, RGB(0,0,255))
_SetBkMode(hdcMem, TRANSPARENT)
_SetTextAlign(hdcMem,TA_UPDATECP)
_Rectangle(hdcMem, 0, 0, W, H)
_DeleteObject(_SelectObject(hdcMem, _CreatePen(PS_SOLID,1,RGB(0,0,0))))
_MoveToEx(hdcMem, xmin * xfactor + xshift, 0 * yfactor + yshift, NULL)
_LineTo(hdcMem, xmax * xfactor + xshift, 0 * yfactor + yshift)
_MoveToEx(hdcMem, 0 * xfactor + xshift, ymin * yfactor + yshift, NULL)
_LineTo(hdcMem, 0 * xfactor + xshift, ymax * yfactor + yshift)
_DeleteObject(_SelectObject(hdcMem, _CreatePen(PS_SOLID,1,RGB(255,0,0))))
xscreen = 0
yscreen = myfunction((xscreen-xshift)/xfactor) * yfactor + yshift
_MoveToEx(hdcMem, xscreen, yscreen, NULL)
FOR n=0 TO w+5 STEP 2
xscreen = n
yscreen = myfunction((xscreen-xshift)/xfactor) * yfactor + yshift
_LineTo(hdcMem, xscreen, yscreen)
NEXT n
_DeleteObject(_SelectObject(hdcMem, _CreatePen(PS_SOLID,1,RGB(0,0,255))))
STRING mystring
mystring = "4 * Sin(x*2) * Cos(x)"
_MoveToEx(hdcMem, 10,10, NULL)
_TextOut(hdcMem, 0, 0, mystring, len(mystring))
_BitBlt(hdc, 0, 0, W, H, hdcMem, 0, 0, SRCCOPY)
_DeleteObject(_SelectObject(hdcMem, oldFont))
_DeleteObject(_SelectObject(hdcMem, oldBrush))
_DeleteObject(_SelectObject(hdcMem, oldPen))
_DeleteObject(_SelectObject(hdcMem, oldBmp))
_DeleteDC(hdcMem)
_ReleaseDC(plotwin.hwnd, hdc)
RETURN
ENDSUB
SUB myfunction(value:DOUBLE),DOUBLE
RETURN 4 * Sin(value*2) * Cos(value)
ENDSUB
SUB Refresh_Scale()
INT L,T,W,H
GETCLIENTSIZE plotwin, L,T,W,H
xfactor = W / (xmax - xmin)
yfactor = -H / (ymax - ymin)
xshift = -xmin * xfactor
yshift = -ymax * yfactor
RETURN
ENDSUB
SUB Resize()
INT L,T,W,H
GETCLIENTSIZE figure, L,T,W,H
SETSIZE plotwin, 0,0,W,H
RETURN
ENDSUB
SUB CfgSetDefault()
xmin=-5
xmax=5
ymin=-5
ymax=5
RETURN
ENDSUB