สวัสดีครับ วันนี้จะขอเสนอการใช้ API ในการ ลากและวาง โดยในตัวอย่างที่นำเสนอนี้ เราสามารถที่จะลากไฟล์จาก window explorer มาใว้บน listbox ที่เรากำหนดได้ ซึ่งเมื่อทำการลากมาวางแล้วเราก็ สามารถที่จะประยุกต์ไปใช้งานอื่นๆได้อีกแล้วแต่ ไอเดีย ของแต่ละคน
เริ่มแรกให้ทำการสร้าง windows ขึ้นมาใหม่ 1 ตัว แล้วก็ทำการกำหนด external functions ดังนี้เลยfunction ulong DragQueryFileA( ulong hDrop, ulong iFile, ref string LPTSTR, ulong cb ) library 'shell32.dll'
subroutine DragAcceptFiles(ulong h, boolean b ) library 'shell32.dll'
จากนั้นให้วาง listbox ลงไป และวาง static text ลงไปเพื่อเป็นตัวอธิบายถึงการทำงาน จากนั้นก็ทำการกำหนด user event ในส่วนของตัว window ขึ้นมาดังนี้
ue_dropfiles
ทำการ map กับ pbm_dropfiles
คราวนี้ก็ลงมือเขียนสคริปต์
เริ่มจาก open event
This.SetPosition(TopMost!)
DragAcceptFiles(handle(this),true)
ต่อด้วย close event
DragAcceptFiles(handle(this),false)
และที่ ue_dropfiles event
string ls_name
ulong ii,icount
icount = DragQueryFileA(Message.WordParm,-1,ls_name,0)
ls_name = space(255)
lb_1.Reset()
for ii = 1 to icount
DragQueryFileA(Message.WordParm,ii - 1,ls_name,255)
lb_1.AddItem(ls_Name)
next
เสร็จแล้วก็ทำการ save ลงชื่อ dropfile.pbl
คราวนี้ก็ทำการทดลอง รัน ดู ให้เปิด window explorer ด้วย แล้วทดลองลากไฟล์ที่เราต้องการมาวางลงบน listbox ดูนะครับ


