Snippets - Helpful Code Routines



Convert Date to UK Format

d$ = date$("mm/dd/yyyy")
print UKDate$(d$)
wait

function UKDate$(a$)
    day$ = mid$(a$,4,2)
    mon$ = left$(a$,2)
    year$ = right$(a$,4)

    UKDate$ = day$ + "/" + mon$ + "/" + year$
end function


Get Bitmap Dimensions

    struct BITMAP,_ 
    bmType as long,_
    bmWidth As long,_
    bmHeight As long,_
    bmWidthBytes As long,_
    bmPlanes as word,_
    bmBitsPixel as word,_
    bmBits as Long

filedialog "Open","*.bmp",bmp$
if bmp$="" then end
loadbmp "testbmp",bmp$
hTest=hbmp("testbmp")
print BitmapWidth(hTest)
print BitmapHeight(hTest)
unloadbmp "testbmp"
END

Function BitmapWidth(hBmp)
    length=len(BITMAP.struct)
    calldll #gdi32, "GetObjectA", hBmp as ulong,_
       length as long,BITMAP as struct,_
       results as long
    BitmapWidth=BITMAP.bmWidth.struct
End Function

Function BitmapHeight(hBmp)
    length=len(BITMAP.struct)
    calldll #gdi32, "GetObjectA", hBmp as ulong,_
       length as long,BITMAP as struct,_
       results as long
    BitmapHeight=BITMAP.bmHeight.struct
End Function


Convert RGB to Long Color Value

print MakeRGB(100,200,30)
end
function MakeRGB(red,green,blue)
    if red<0 then red=0
    if red>255 then red=255
    if green<0 then green=0
    if green>255 then green=255
    if blue<0 then blue=0
    if blue>255 then blue=255
    MakeRGB=(blue*256*256)+(green*256)+red
    end function


Get Red, Green, Blue from Long Color Value

print GetRed(9471354)
print GetGreen(9471354)
print GetBlue(9471354)
end
function GetRed(color)
    blue=int(color/(256*256))
    green=int((color-blue*256*256)/256)
    GetRed=color-blue*256*256-green*256
    end function

function GetGreen(color)
    blue=int(color/(256*256))
    GetGreen=int((color-blue*256*256)/256)
    end function

function GetBlue(color)
    GetBlue=int(color/(256*256))
    end function


Retrieve Words and Bytes from DWORD

print HiWord(99471354)
function HiWord(dword)
    HiWord=int(dword/(256*256))
    end function


print LoWord(99471354)
function LoWord(dword)
    hiword=int(dword/(256*256))
    LoWord=dword-(hiword*256*256)
    end function


print HiByte(4*256+5)
function HiByte(word)
    HiByte=int(word/256)
    end function


print LoByte(4*256+5)
function LoByte(word)
    hibyte=int(word/256)
    LoByte=word-(hibyte*256)
    end function


Modulus

function Modulus(number, divisor)
    Modulus=(number)-(int(number/divisor)*divisor)
    end function


String Functions

function InsertString$(string$,insert$,start)
    InsertString$=left$(string$,start-1)+insert$+mid$(string$,start)
    end function


function ReplaceString$(string$,replace$,start)
    length=len(replace$)
    ReplaceString$=left$(string$,start-1)+replace$+mid$(string$,start+length)
    end function


function ReverseString$(string$)
    for i = len(string$) to 1 step -1
        ReverseString$=ReverseString$+mid$(string$,i,1)
    next i
    end function


function String$(char$, total)
    for i=1 to total
    String$=String$+char$
    next i
    end function

function ReverseString$(string$)
    for i = len(string$) to 1 step -1
        ReverseString$=ReverseString$+mid$(string$,i,1)
    next i
    end function


Center a Window

WindowWidth = 200
WindowHeight = 150

UpperLeftX = int((DisplayWidth-WindowWidth)/2)
UpperLeftY = int((DisplayHeight-WindowHeight)/2)

open "Centered Window" for window as #1
print #1, "trapclose [quit]"
wait

[quit]
close #1 : end


Get a File's Time Stamp

filedialog "Open","*.*",file$
if file$="" then end
fname$=SeparateFile$(file$)
fpath$=SeparatePath$(file$)
dim info$(10,10)
files, fpath$,fname$, info$()
if val(info$(0,0))=0 then
    print "failed"
else
    print info$(1,2)
end if
end

'Separate Filename and Path:
Function SeparateFile$(f$)
    fileindex=Len(f$)
    filelength=Len(f$)
      While Mid$(f$, fileindex,1)<>"\"
        fileindex=fileindex-1
      Wend
    SeparateFile$=Right$(f$,filelength-fileindex)
End Function

Function SeparatePath$(f$)
    fileindex=Len(f$)
    filelength=Len(f$)
      While Mid$(f$, fileindex,1)<>"\"
        fileindex=fileindex-1
      Wend
    SeparatePath$=Left$(f$,fileindex)
End Function


Home

Source Code

Utilities

Internet

Games

Graphics

Media Demos

Snippets

DLL's

API Resources

Freeware

LB 4 Companion

Mastering LB 3

LB Workshop

Game Workshop

Links

Index