I noticed a very interesting post, recently, on the API board of the LB Conforums Forum. Doyle Whisenant had dashed off a post and left a link to some code that was really wowing some members. It was so powerful, I couldn't leave it alone until I made something out of it that I felt I understood well enough that I could use the idea in other contexts if I wanted to. The best and most satisfying understanding comes from one's own efforts. So I thought long and hard about whether or not to publish this article. Would I really be doing people a favor? Doyle's quick code actually was very misleading to me - it wouldn't have been to the API experts but to other people, like myself, who are still beginners in Windows programming, I think it contained a red herring. When I finally knuckled down and read the documentation of the API calls used in Doyle's code I got it. And maybe I got it a lot better for having to figure it out. If an image is worth a thousand words, the following code, which allows you to browse your computer files and view a variety of file types, including some image file types, should be worth more than any words of explanation I might add.
 ' ATL Test - thanks to Doyle Whisenant for the concept and the basic atl dll code 
' - and to Alyce Watson for the technique of remembering the filedialog directory
' - and a little code borrowed from Eddie
' - and all the good stuff in the LB4 Companion and past support
' - and to the LB community (Sean Brown, Dan, ElEdwards, Stefan Pendl, Janet Terra...)
' - and, of course, Carl Gundel.

global hWndViewer, hATL, cw, ch, FileOpen, true, false
false = 0
true = not(false)
FileOpen = false
global CurDir$, curFileType$
CurDir$ = DefaultDir$
nomainwin
WindowWidth=DisplayWidth
WindowHeight=DisplayHeight
UpperLeftX=0:UpperLeftY=0
menu #main, "File",_
"Open",ViewFile,_
"Type",SelectFileType,_
"Close" ,CloseViewerFromMenu
listbox #main.type, filetype$(),FileType,0,0,62,100
filetype$(1) = ".txt"
filetype$(2) = ".rtf"
filetype$(3) = ".doc"
filetype$(4) = ".pdf"
filetype$(5) = ".jpg"
filetype$(6) = ".gif"
filetype$(7) = ".xls"
curFileType$ = ".txt"
Open "Liberty Basic ATL Viewer" For Window_nf As #main
#main "TrapClose CloseViewer"
#main.type "hide"
Open "atl" For DLL As #atl
CallDLL #atl, "AtlAxWinInit", Ret As void
hWndViewer = hWnd(#main)
STRUCT Rect,_ 'struct for storing client area rectangle
leftX as long,_ 'upper left x
upperY as long,_ 'upper left y
rightX as long,_ 'lower right x
lowerY as long 'lower right y
calldll #user32,"GetClientRect",_
hWndViewer as ulong,_ 'window handle
Rect as struct,_ 'name of struct
r as long 'return
cw = Rect.rightX.struct
ch = Rect.lowerY.struct
' for CreateWindowExA call which creates a child window with
'WindowWidth=cw
'WindowHeight=ch
'UpperLeftX=0:UpperLeftY=0
wait

sub SelectFileType
call CloseMainView
#main.type "show"
#main.type "setfocus"
end sub

sub FileType handle$
#handle$ "selection? curFileType$"
#handle$ "hide"
call ViewFile
end sub

sub ViewFile
filedialog "Open file", CurDir$+"\*"+curFileType$, fileName$
if fileName$ = "" then exit sub
CurDir$ = SeparatePath$(fileName$)
if FileOpen then call CloseMainView
style = _WS_CHILD + _WS_VISIBLE +_WS_VSCROLL+_WS_DISABLED
FileOpen = true

on error goto [error]

CallDLL #user32, "GetWindowLongA", _
hWndViewer As ulong, _
_GWL_HINSTANCE As long, _
hInst As long
CallDLL #user32, "CreateWindowExA", _
_WS_EX_STATICEDGE As long, _
"AtlAxWin" As ptr, _
fileName$ As ptr, _
style As long, _
0 As long, _
0 As long, _
cw As long, _
ch As long, _
hWndViewer As ulong, _
100 As long, _
hInst As long, _
0 As long, _
hATL As ulong
exit sub
[error]
notice "There is a problem reading this file"
end sub

sub CloseMainView
' destroy the atl child
CallDLL #user32, "DestroyWindow", _
hATL As ulong, _
r as boolean
FileOpen = false
end sub

sub CloseViewer handle$
call CloseViewerFromMenu
end sub

sub CloseViewerFromMenu
Close #atl
if FileOpen then call CloseMainView
Close #main
end
end sub

function SeparatePath$(f$)
fileindex=Len(f$)
filelength=Len(f$)
while Mid$(f$, fileindex,1)<>"\"
fileindex=fileindex-1
wend
SeparatePath$=Left$(f$,fileindex)
end function
' end of code '

You might like to try adding the bmp file type to see what you get! If you remove the _WS_DISABLED style constant you will be able to input to the window but be warned without the window disabled it is very easy to accidentally change the file you are viewing.

The following relevant article was referred to me by Alyce Watson.
http://www.pluralsight.com/articlecontent/cpprep0799.htm


Regards,

Grahame King, April 5th 2006