接受一个拖拽的文件
相关的例子:下载>>> 作者:山原依纲 于2008-12-15上传 

汇编语言编写程序有着小巧灵活的特点,非常适合编写小型的简单工具。下面的程序使用的API,都是在 shell32.lib 里面定义的。因此,程序开头要 include shell32.inc 和 includelib shell32.lib。

VOID DragAcceptFiles(
HWND hWnd,//目标窗体的句柄
BOOL fAccept//为True时,hWnd所指向的窗体可以接受拖放的文件

当窗口接受到文件后,会收到 WM_DROPFILES 消息。wParam中给出拖动文件结构的句柄。我们再使用下面的API即可得到传递的文件名。

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

;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

IDC_EDIT equ 2001

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

.DATA
szClassName db "MASMPlus_Class",0
szFileName db "Waiting for a file",0

.DATA?
hInstance dd ?
hEdit dd ?
szBuffer db 512 dup(?)
.CODE
START:

invoke GetModuleHandle,NULL
mov hInstance,eax
invoke WinMain,hInstance,NULL,NULL,SW_SHOWDEFAULT
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,400,200,NULL,NULL,hInst,NULL
mov hWnd,eax
INVOKE CreateWindowEx, NULL, CTXT('static'), addr szFileName,\
WS_CHILD or WS_VISIBLE or SS_NOTIFY,10, 50, 400, 150, hWnd, IDC_EDIT, hInst, NULL
mov hEdit, eax
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 DragAcceptFiles,hWin,TRUE
.elseif uMsg == WM_DESTROY
invoke PostQuitMessage,NULL
.elseif uMsg == WM_DROPFILES
invoke DragQueryFile,wParam,0,ADDR szBuffer,sizeof szBuffer
invoke SetWindowText,hEdit,ADDR szBuffer
.else
invoke DefWindowProc,hWin,uMsg,wParam,lParam
.endif

ret
WndProc endp

END START

运行结果




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