[REBOL] Re: Exploring System Port
From: greggirwin:mindspring at: 13-Oct-2002 17:07
Hi All,
OK, it looks like incoming Windows messages are formed like this:
msg: [winmsg 563 8585228 0]
so:
msg/1 = 'winmsg
msg/2 = ID
msg/3 = wParam
msg/4 = lParam
Below is a very quick hack that allows you to drag and drop files onto the
console window. Requires View/Pro or /Command for library access and a
version that supports the system port (e.g. 1.2.5, 1.2.8, Command 2.1.1).
Let me know if it works for you.
--Gregg
P.S. Watch for wrap!
REBOL [
Title: "sys-port-drag-accept"
File: %sys-port-drag-accept.r
Author: "Gregg Irwin"
EMail: [greggirwin--acm--org]
Purpose: {Demo using system port to catch WM_DROPFILE messages.}
]
; Stripped version of win-shell object for demo purposes.
win-shell: make object! [
win-lib: load/library %shell32.dll
null-buff: func [
{Returns a null-filled string buffer of the specified length.}
len [integer!]
][
head insert/dup make string! len #"^@" len
]
drag-accept-files: make routine! [
hWnd [integer!]
fAccept [integer!]
return: [integer!]
] win-lib "DragAcceptFiles"
drag-finish: make routine! [
hDrop [integer!]
return: [integer!]
] win-lib "DragFinish"
drag-query-file: make routine! [
hWnd [integer!]
iFile [integer!]
lpszFile[string!]
cb [integer!]
return: [integer!]
] win-lib "DragQueryFile"
drag-query-filename-size: make routine! [
hWnd [integer!]
iFile [integer!]
lpszFile[integer!]
cb [integer!]
return: [integer!]
] win-lib "DragQueryFile"
num-files-dropped?: func [hdrop [integer!]] [
drag-query-file hdrop -1 "" 0
]
; When they give us a filename index, we'll subtract one for them,
; because Windows has the list as zero based, but I'd much rather let
; everything be one based on the REBOL side.
dropped-filename-size?: func [hdrop [integer!] index [integer!]] [
drag-query-filename-size hdrop index - 1 0 0
]
dropped-filename?: func [hdrop [integer!] index [integer!] /local result
len] [
result: null-buff add 1 dropped-filename-size? hdrop index
len: drag-query-file hdrop index - 1 result length? result
copy/part result len
]
]
my-hwnd?: does [second get-modes system/ports/system [window]]
WM_DROPFILES: 563 ; &H233
enable-system-trap: does [
; Trap OS interrupts
if not system/ports/system [
if none? attempt [system/ports/system: open [scheme: 'system]][
print "NOTE: Missing System Port" exit
]
]
if find get-modes system/ports/system 'system-modes 'winmsg [
set-modes system/ports/system [winmsg: WM_DROPFILES]
]
append system/ports/wait-list system/ports/system
]
check-system-trap: func [port /local msg] [
if not port? port [return none]
if any [port/scheme <> 'system port <> system/ports/system][return
port]
if not system/ports/system [return none]
while [msg: pick system/ports/system 1] [
;print ["msg:" mold msg]
if msg/1 = 'winmsg [
if msg/2 = WM_DROPFILES [
print ["You dropped " win-shell/num-files-dropped? msg/3
files.
]
repeat i win-shell/num-files-dropped? msg/3 [
file: to-rebol-file win-shell/dropped-filename? msg/3 i
print [tab join i "." tab either dir? file [dirize
file][file]]
]
print "Finishing drag operation"
win-shell/drag-finish msg/3
print "Unregistering window from DragAcceptFiles queue"
win-shell/drag-accept-files my-hwnd? to integer! false
; (save files here)
halt ;quit
]
]
]
none
]
print "Drag some files and drop them on this window...^/"
win-shell/drag-accept-files my-hwnd? to integer! true
enable-system-trap
forever [
wake-port: wait 10 ;timeout period
if wake-port [check-system-trap wake-port]
]