接受多个拖拽的文件
相关的例子:下载>>> 作者:山原依纲 于2009-3-17上传 

上一次我们介绍了接受一个文件名称的方法,这次介绍如何接受多个的拖拽。

UINT DragQueryFile(
HDROP hDrop, //指向拖动文件结构的句柄;
UINT iFile, //iFile 是以0开始的被托动文件的序号,因为一次可能拖动多个文件。当此参数设为0xFFFFFFFF,则函数返回拖动文件的数量
LPTSTR lpszFile,//指向文件名的缓冲区;
UINT cch //文件名缓冲区的大小
);

程序中,令iFile为-1(0xFFFFFFFF),调用DragQueryFile,之后取得拖拽的文件数量;再从0递增

逐次取文件名到Buffer中,输出在RichEdit上。

;MASMPlus 代码模板 - 普通的 Windows 程序代码

.386
.Model Flat, StdCall
Option Casemap :None

Include windows.inc
Include user32.inc
Include kernel32.inc
Include gdi32.inc
include shell32.inc


includelib gdi32.lib
IncludeLib user32.lib
IncludeLib kernel32.lib
includelib shell32.lib
include macro.asm

WinMain PROTO :DWORD,:DWORD,:DWORD,:DWORD
WndProc PROTO :DWORD,:DWORD,:DWORD,:DWORD

.DATA
szClassName db "MASMPlus_Class",0
szRichEditDLL db "riched20.dll",0
szRichEditClass db "RichEdit20A",0
szCRCL db 10,0
.DATA?
hInstance dd ?
hwndRichEdit dd ?
hRichEdit dd ?
szBuffer db 200 dup(0)
.CODE
START:
invoke LoadLibrary,addr szRichEditDLL ;动态加载包含RichEdit的DLL
mov hRichEdit,eax
.if eax==0 ;加载失败的话简单提示
invoke MessageBox,0,CTXT("Cannot find DLL"),NULL,MB_OK or MB_ICONERROR
.endif

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT
invoke FreeLibrary,hRichEdit ;前面加载了,最后就要释放一次
invoke ExitProcess,0

WinMain proc hInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD
LOCAL wc :WNDCLASSEX
LOCAL msg :MSG
local hWnd :HWND

mov wc.cbSize,sizeof WNDCLASSEX
mov wc.style,CS_HREDRAW or CS_VREDRAW or CS_BYTEALIGNWINDOW
mov wc.lpfnWndProc,offset WndProc
mov wc.cbClsExtra,NULL
mov wc.cbWndExtra,NULL
push hInst
pop wc.hInstance
mov wc.hbrBackground,COLOR_BTNFACE+1
mov wc.lpszMenuName,NULL
mov wc.lpszClassName,offset szClassName
invoke LoadIcon,hInst,100
mov wc.hIcon,eax
invoke LoadCursor,NULL,IDC_ARROW
mov wc.hCursor,eax
mov wc.hIconSm,0
invoke RegisterClassEx, ADDR wc
invoke CreateWindowEx,NULL,ADDR szClassName,CTXT("http://www.aogosoft.com"),WS_OVERLAPPEDWINDOW,\
200,200,600,200,NULL,NULL,hInst,NULL
mov hWnd,eax

invoke DragAcceptFiles,hWnd,TRUE ;要接受拖拽

invoke ShowWindow,hWnd,SW_SHOWNORMAL
invoke UpdateWindow,hWnd

StartLoop:
invoke GetMessage,ADDR msg,NULL,0,0
cmp eax, 0
je ExitLoop
invoke TranslateMessage, ADDR msg
invoke DispatchMessage, ADDR msg
jmp StartLoop
ExitLoop:

mov eax,msg.wParam
ret
WinMain endp

WndProc proc hWin:DWORD,uMsg:DWORD,wParam :DWORD,lParam :DWORD
.if uMsg==WM_CREATE
invoke CreateWindowEx,WS_EX_CLIENTEDGE,addr szRichEditClass,0,WS_CHILD or WS_VISIBLE or ES_MULTILINE or WS_VSCROLL or WS_HSCROLL or ES_NOHIDESEL,\
10,10, 570, 140,hWin,2001,hInstance,0
mov hwndRichEdit,eax ;创建RichEdit

.elseif uMsg == WM_DESTROY
invoke PostQuitMessage,NULL
.ELSEIF uMsg==WM_DROPFILES
invoke DragQueryFile,wParam,-1,addr szBuffer,sizeof szBuffer
mov ecx,eax ;取得拖拽的文件数量

xor ebx,ebx
NextFileName:
push ecx
push ebx
invoke DragQueryFile,wParam,ebx,ADDR szBuffer,sizeof szBuffer

invoke SendMessage,hwndRichEdit,EM_SETSEL,-1,-1
invoke SendMessage,hwndRichEdit,EM_REPLACESEL,0,addr szBuffer ;取文件名

invoke SendMessage,hwndRichEdit,EM_SETSEL,-1,-1
invoke SendMessage,hwndRichEdit,EM_REPLACESEL,0,addr szCRCL ;用这样的方法换行

pop ebx
inc ebx
pop ecx
loop NextFileName
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
.endif
ret
WndProc endp

END START

运行结果


 



欢迎访问AoGo汇编小站:http://www.aogosoft.com
下一篇>>>