Here is a different implimentation...
Code:
'----------------------------------------------------------------- ' Graph It ' ' '----------------------------------------------------------------- $COMPILE EXE $INCLUDE "WIN32API.INC" GLOBAL ghPanel AS LONG GLOBAL ghInst AS LONG GLOBAL gfPlotIt AS LONG GLOBAL OldPanelProc AS LONG %ID_PLOT = 110 %ID_CLEAR = 120 %AbsMinPts = 1 %AbsMaxPts = 200 %MaxCols = 2 %GraphThick = 1 %XMargin = 100 %YMargin = 100 %Radius = 2 %BorderThick = 1 %GMargin = 5 GLOBAL gX#(),gY#() GLOBAL gMinPts AS INTEGER ' Array minimum index GLOBAL gMaxPts AS INTEGER ' Array maximum index GLOBAL gXMin#,gYMin#,gXMax#,gYMax# 'Min and Max of the arrays DECLARE SUB graph() DECLARE SUB plot() '-------------------------------------------------------------- ' WinMain: ' ' '-------------------------------------------------------------- FUNCTION WINMAIN (BYVAL hInstance AS LONG, _ BYVAL hPrevInstance AS LONG, _ lpCmdLine AS ASCIIZ PTR, _ BYVAL iCmdShow AS LONG) AS LONG LOCAL Msg AS tagMsg LOCAL wndclass AS WndClassEx LOCAL szAppName AS ASCIIZ * 80 LOCAL hWnd AS LONG LOCAL gBrush AS LONG ghInst = hInstance gBrush = GetStockObject(%WHITE_BRUSH) szAppName = "Rotary" wndclass.cbSize = SIZEOF(WndClass) wndclass.style = %CS_HREDRAW OR %CS_VREDRAW wndclass.lpfnWndProc = CODEPTR( WndProc ) wndclass.cbClsExtra = 0 wndclass.cbWndExtra = 0 wndclass.hInstance = hInstance wndclass.hIcon = 0 'LoadIcon( hInstance, "HELLOWIN" ) wndclass.hCursor = LoadCursor( %NULL, BYVAL %IDC_ARROW ) wndclass.hbrBackground = gBrush wndclass.lpszMenuName = %NULL wndclass.lpszClassName = VARPTR( szAppName ) wndclass.hIconSm = LoadIcon( hInstance, BYVAL %IDI_APPLICATION ) RegisterClassEx wndclass ' Create a window using the registered class hWnd = CreateWindow(szAppName, _ ' window class name "Graph Plot", _ ' window caption %WS_OVERLAPPEDWINDOW, _ ' window style 126, _ ' initial x position 72, _ ' initial y position 600, _ ' initial x size 550, _ ' initial y size %NULL, _ ' parent window handle %NULL, _ ' window menu handle hInstance, _ ' program instance handle BYVAL %NULL) ' creation parameters '*Display the window on the screen ShowWindow hWnd, %SW_SHOW UpdateWindow hWnd WHILE GetMessage(Msg, %NULL, 0, 0) TranslateMessage Msg DispatchMessage Msg WEND FUNCTION = msg.wParam END FUNCTION ' WinMain '---------------------------------------------------------------------- ' Main Window Procedure: ' ' '---------------------------------------------------------------------- FUNCTION WndProc (BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _ BYVAL wParam AS LONG, BYVAL lParam AS LONG) EXPORT AS LONG LOCAL hDC AS LONG LOCAL hPen AS LONG LOCAL hBrush AS LONG LOCAL ps AS PaintStruct LOCAL rc AS RECT LOCAL pt AS POINTAPI LOCAL hBitmap AS BITMAP LOCAL rcPanel AS RECT SELECT CASE wMsg '--- CASE %WM_CREATE 'Image ->%SS_BITMAP, %SS_ICON, %SS_ENHMETAFILE, %SS_REALSIZEIMAGE 'Panel ->%SS_BLACKRECT,%SS_GRAYRECT,%SS_WHITERECT Call GetClientRect(hWnd,rcPanel) Call InflateRect(rcPanel,-50,-50) ghPanel = CreateWindowEx(0,"STATIC", "", %WS_CHILD _ OR %WS_CLIPSIBLINGS OR %SS_NOTIFY _ OR %SS_BITMAP OR %WS_VISIBLE OR %WS_BORDER, _ rcPanel.nLeft, _ rcPanel.nTop, _ rcPanel.nRight - rcPanel.nLeft, _ rcPanel.nBottom - rcPanel.nTop, _ hWnd, %NULL, ghInst, BYVAL %NULL) 'Subclass the control OldPanelProc = GetWindowLong(ghPanel, %GWL_WNDPROC) Call SetWindowLong(ghPanel, %GWL_WNDPROC, CODEPTR(PanelWndProc)) 'Create the Plot button ghPlotBtn& = CreateWindowEx(0,"BUTTON", "&Plot", _ %WS_CHILD OR %WS_VISIBLE OR %WS_CLIPSIBLINGS _ OR %BS_PUSHBUTTON, _ 50, _ 10, _ 100, _ 30, _ hWnd, %ID_PLOT, ghInst, ByVal %NULL) 'Create the Clear button ghClrBtn& = CreateWindowEx(0,"BUTTON", "&Clear", _ %WS_CHILD OR %WS_VISIBLE OR %WS_CLIPSIBLINGS _ OR %BS_PUSHBUTTON, _ 160, _ 10, _ 100, _ 30, _ hWnd, %ID_CLEAR, ghInst, ByVal %NULL) '--- CASE %WM_COMMAND SELECT CASE LOWRD(wParam) '--- CASE %ID_PLOT 'Button IF HIWRD(wParam) = %BN_CLICKED THEN gfPlotIt = 1 Call InvalidateRect(ghPanel,Byval %NULL,%TRUE) Call UpdateWindow(ghPanel) Function =0 END IF '--- CASE %ID_CLEAR 'Button IF HIWRD(wParam) = %BN_CLICKED THEN gfPlotIt = 0 Call InvalidateRect(ghPanel,Byval %NULL,%TRUE) Call UpdateWindow(ghPanel) Function =0 END IF END SELECT '--- CASE %WM_DESTROY IF OldPanelProc THEN Call SetWindowLong(ghPanel, %GWL_WNDPROC, OldPanelProc) PostQuitMessage 0 FUNCTION = 0 EXIT FUNCTION END SELECT FUNCTION = DefWindowProc(hWnd, wMsg, wParam, lParam) END FUNCTION '------------------------------------------------------------------------ ' ' ' '------------------------------------------------------------------------ FUNCTION PanelWndProc(BYVAL hWnd AS LONG, BYVAL wMsg AS LONG, _ BYVAL wParam AS LONG, BYVAL lParam AS LONG) AS LONG IF wMsg = %WM_PAINT THEN If gfPlotIt = 1 Then Call graph() Call Plot() Else Call ClearWindow(hWnd) End If END IF FUNCTION = CallWindowProc(OldPanelProc, hWnd, wMsg, wParam, lParam) END FUNCTION '---------------------------------------------------------------------- ' ' ' '---------------------------------------------------------------------- SUB ClearWindow(BYVAL hWnd AS LONG) LOCAL hDc AS LONG LOCAL rc AS RECT hDc = GetDC(hWnd) Call GetClientRect(hWnd, rc) Call FillRect(hDc, rc, GetStockObject(%WHITE_BRUSH)) Call ReleaseDC(hWnd, hDc) END SUB '------------------------------------------------------------------------ ' ' ' '------------------------------------------------------------------------ SUB graph LOCAL i AS INTEGER,xTemp$,yTemp$ gMinPts = %AbsMinPts : gMaxPts = %AbsMaxPts DIM gX(gMinPts : gMaxPts) DIM gY(gMinPts : gMaxPts) gMaxPts = gMinPts i = gMinPts WHILE i <= DATACOUNT xTemp = READ$(i) yTemp = READ$(i+1) gX(gMaxPts) = VAL(TRIM$(xTemp)) gY(gMaxPts) = VAL(TRIM$(yTemp)) IF gMaxPts = gMinPts THEN gXMin = gX(gMaxPts) : gXMax = gX(gMaxPts) 'Set the data Min and Max gYMin = gY(gMaxPts) : gYMax = gY(gMaxPts) ELSE SELECT CASE gY(gMaxPts) CASE < gYMin gYMin = gY(gMaxPts) CASE > gYMax gYMax = gY(gMaxPts) END SELECT END IF i = i + %MaxCols INCR gMaxPts WEND DECR gMaxPts REDIM PRESERVE gX(gMinPts : gMaxPts) REDIM PRESERVE gY(gMinPts : gMaxPts) ARRAY SORT gX(),TAGARRAY gY() gXMin = gX(gMinPts) gXMax = gX(gMaxPts) DATA 0, 134.67 DATA 1, 101.45 DATA 2, 98.45 DATA 3, 154.70 DATA 4, 189.87 DATA 5, 94.56 DATA 6, 89.23 DATA 7, 78.12 DATA 8, 99.99 END SUB '-------------------------------------------------------------------------- ' ' ' '-------------------------------------------------------------------------- SUB plot LOCAL LpPaint AS PaintStruct LOCAL hDC&,hBrush&,hPen&,WinX&,WinY&,i&,Colr&,j& LOCAL r AS RECT ' For Window dimensions LOCAL X0&,Y0&,X1&,Y1& ' Temporary origin to draw FROM LOCAL XMarg&,YMarg&,GMarg&,XWide&,YWide& LOCAL XUnit#,YUnit# GetClientRect ghPanel, r WinX = r.nRight - r.nLeft 'Get windows dimensions WinY = r.nBottom - r.nTop hDC = BeginPaint(ghPanel, LpPaint) '---------- 'Initializations XMarg = WinX/%XMargin 'Margins are 10% of the Window YMarg = WinY/%YMargin GMarg = WinY*(%GMargin/100) XWide = WinX - 2*XMarg YWide = WinY - 2*YMarg XUnit = (gXMax-gXMin) / CSNG(XWide-(2*GMarg)) YUnit = (gYMax-gYMin) / CSNG(YWide-(2*GMarg)) #if 0 '---------- 'For the border hPen = CreatePen(%PS_SOLID, %BorderThick, %Black) SelectObject hDC, hPen MoveToEx hDC, XMarg, YMarg, BYVAL %NULL LineTo hDC, XMarg+XWide, YMarg MoveToEx hDC, XMarg+XWide, YMarg, BYVAL %NULL LineTo hDC, XMarg+XWide, YMarg+YWide MoveToEx hDC, XMarg+XWide, YMarg+YWide, BYVAL %NULL LineTo hDC, XMarg, YMarg+YWide MoveToEx hDC, XMarg, YMarg+YWide, BYVAL %NULL LineTo hDC, XMarg, YMarg #endif '---------- 'For the graph Colr = %Red hPen = CreatePen(%PS_SOLID, %GraphThick, Colr) SelectObject hDC, hPen XMarg = XMarg+GMarg 'i.e. the inside margins between the border YMarg = YMarg-GMarg ' and the actual graph X0 = XMarg + CLNG ( (gX(gMinPts)-gXMin) / XUnit ) 'First point Y0 = Ymarg + (YWide - CLNG ( (gY(gMinPts)-gYMin) / YUnit ) ) Ellipse hDC, X0-%Radius, Y0-%Radius, X0+%Radius, Y0+%Radius FOR j = gMinPts TO gMaxPts 'And the points thereafter X1 = XMarg + CLNG ( (gX(j)-gXMin) / XUnit ) 'Point TO which to draw Y1 = YMarg + (YWide - CLNG ( (gY(j)-gYMin) / YUnit ) ) ' becomes next origin MoveToEx hDC, X0, Y0, BYVAL %NULL 'Move to start LineTo hDC, X1, Y1 ' and draw line from it to next Ellipse hDC, X1-%Radius, Y1-%Radius, X1+%Radius, Y1+%Radius X0 = X1 'Point FROM which to draw Y0 = Y1 'Origin point NEXT j '---------- 'Clean up EndPaint hDC, LpPaint DeleteObject hPen END SUB
Leave a comment: