Post by GWS on Apr 11, 2018 18:56:17 GMT -5
Hi folks,
Here's a basic graphics window that incorporates several features that a coder might find useful
It's the first LB program I've written in a while, so it took a lot of research to get things working as I wanted.
The first thing I wanted was a Shaded background, so I've included a simple routine to give a blue shading.
Then a needed to place a transparent text heading on the page - another subroutine achieves that.
Finally, I wanted a bit of flashy decoration, so I've used a number of coloured pixel areas to look pretty.
Anyway, here it is ..
all the best,
Graham
Here's a basic graphics window that incorporates several features that a coder might find useful
It's the first LB program I've written in a while, so it took a lot of research to get things working as I wanted.
The first thing I wanted was a Shaded background, so I've included a simple routine to give a blue shading.
Then a needed to place a transparent text heading on the page - another subroutine achieves that.
Finally, I wanted a bit of flashy decoration, so I've used a number of coloured pixel areas to look pretty.
Anyway, here it is ..
nomainwin
WindowWidth = 800 : WindowHeight = 600
wW = WindowWidth : wH = WindowHeight ' hate typing long variable names
Global wW,wH,pi ' needed in subroutines
UpperLeftX = (DisplayWidth - wW) / 2 ' centre graphics window
UpperLeftY = (DisplayHeight - wH) / 2
pi = 4 * atn(1) ' gives an accurate value of pi (3.14 .. )
button #win.quit, "Exit",[quit],UL,(wW - 60)/2, wH * 0.8, 60, 30
open " A Graphics Window" for graphics_nsb_nf as #win ' open graphics window
#win "trapclose [quit]" ' capture window closed message
#win, "down" ' prepare to draw
Call Gradient ' draw a blue gradient fill
' create some coloured spots ...
for i = 1 to 10
meanx = int(rnd(1)*(wW - 100))
meany = int(rnd(1)*(wH - 200))
sdev = 30 ' controls the spread of the pixels ...
red = int(rnd(1)*200) + 55
green = int(rnd(1)*200) + 55
blue = int(rnd(1)*200) + 55
#win, "color ";red;" ";green;" ";blue
for j = 1 to 400
x = splot() * sdev + meanx
y = splot() * sdev + meany
#win, "size ";int(rnd(1)*3)
#win, "set ";x;" ";y
next j
next i
' prepare for transparent text ..
Call Trans
text$ = "Liberty Basic" ' set heading text
' format the text ..
red = 50
green = 200
blue = 255
#win, "color ";red;" ";green;" ";blue
#win, "font arial 26 italic; backcolor 0 0 int(originY * cstep)"
tw = TextWidth(text$) ' function to get text width
textX = (wW - tw)/2: textY = 100
#win, "place "; textX; " "; textY ' position text
#win, "|"; text$ ' indicate graphics text "|" and display it
#win, "flush" ' retain graphos if window resized
wait
[quit]
close #win
end
function TextWidth(t$)
#win, "stringwidth? t$ twidth"
TextWidth = twidth
end function
function splot()
' routine to generate random values from a normal distribution ..
' the Normal curve is bell-shaped, centred at the Mean, and extending
' 3 Standard deviations above and below the mean.
x1 = rnd(1)
x2 = rnd(1)
if (x1 < 1e-5) then x1 = 1e-5 ' catch very small values of x1 ..
splot = sqr(-2 * log(x1)) * cos(2 * pi * x2) ' the Normal curve value ..
end function
sub Gradient
cstep = 0.75 * 255 / wH ' multiplier of 255 controls the gradient rate
for i = 1 to wH
x1 = 0 : y1 = i
x2 = wW : y2 = i
c = int(i * cstep)
#win, "color 0 0"; " "; c ' set line colour
#win, "line "; x1; " "; y1; " "; x2; " "; y2
next i
end sub
sub Trans
' prepare for transparent text ..
hWin = hwnd(#win) ' get graphics window handle
CallDLL #user32, "GetDC",_ ' get device context handle of graphics window
hWin As ulong,_ ' handle of graphics window
gDC As ulong ' device context
CallDLL #gdi32, "SetBkMode",_
gDC As ulong,_ ' handle of graphics window
_TRANSPARENT As long,_ ' flag for transparent background
result As long
callDLL #user32, "ReleaseDC",_
hWin As ulong,_ ' graphics window handle
gDC As ulong,_ ' device context
result As long
end sub
all the best,
Graham