关于SENDMESSAGE和POSTMESSAGE的发送问题!~~~~~~~~~~ ( 积分: 150 )

  • 主题发起人 主题发起人 LDSON
  • 开始时间 开始时间
L

LDSON

Unregistered / Unconfirmed
GUEST, unregistred user!
只知道这二个函数可以发送按键模拟,和发送消息或字符串之类的,不知道能不能发送实体文件,比如*.EXE *.TXT *.MP3等去别的程序的窗口,换句话,也就是说能不能用这二个函数模拟发送相当于鼠标拖放效果的操作? 如果不能的话,怎么样用程序模拟鼠标拖放到别的程序里的操作,希望能有答符,或者能给出实例代码,谢谢各位了.
 
只知道这二个函数可以发送按键模拟,和发送消息或字符串之类的,不知道能不能发送实体文件,比如*.EXE *.TXT *.MP3等去别的程序的窗口,换句话,也就是说能不能用这二个函数模拟发送相当于鼠标拖放效果的操作? 如果不能的话,怎么样用程序模拟鼠标拖放到别的程序里的操作,希望能有答符,或者能给出实例代码,谢谢各位了.
 
两个办法都可以,<br>1.用发消息的话,最好将文件放到系统的共享区(用GlobalAlloc申请内存),然后只用自定义消息通知其他窗口去取.<br>或者发送文件名到其他窗口.<br>用Wm_CopyData可以发送自定义的数据.<br><br>用拖拽的办法就在其他窗体中用WM_DropFiles消息接受文件咯.<br>明白了?
 
这样说吧,现在别的程序的窗口本身就接受拖放文件进来的,但是我要用什么方式去模拟鼠标拖放文件去这个窗口? wm_copydata这个参数是可以用SENDMESSAGE或POSTMESSAGE发送实体文件去别的程序的窗口吗? 我的QQ是106187,盼答复
 
用消息模拟拖放文件,请参考:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=1567087
 
独帅 谢谢给了参考资料,可是往别的窗口拖放文件依然不成功比如QQ的RICHEDIT这个窗口,连一点动静也没有....
 
你想通过这种方法给QQ发文件大概是不行的.<br>可以这样试试看,将文件拷贝到剪贴板然后给richedit发WM_Paste消息.<br>注意拷贝文件到剪贴板,注意剪贴板的格式为CF_HDROP.
 
可以这样试试看,将文件拷贝到剪贴板然后给richedit发WM_Paste消息.<br><br>不知道你这个用SENDMESSAGE方式是否可行,但是我现在用CTRL+C和CTRL+V都粘帖不进去
 
在QQ2004版本,你找到的“与 XX 聊天中”窗口,有四个子窗口,<br>其中前三个类名为Button的是右上角的三个控制按钮(最小化、最大化和关闭),<br>另外一个类名为&quot;#32770&quot;的子窗口才是你应该发送消息的窗口<br><br>至少QQ2004和TM 3.0是这样的,其他版本不确定
 
关键 是不管那个版本的句柄我都找对了,但是用DoDropFiles(j2,list);办法就是不能拖放文件进聊天窗口,J2是句柄,为找到的RICHEDIT或RichEdit20A都不请,还望在指教
 
可以这样试试看,将文件拷贝到剪贴板然后给richedit发WM_Paste消息.<br>注意拷贝文件到剪贴板,注意剪贴板的格式为CF_HDROP. &nbsp;<br>daqun朋友的这种方法也没有通过,RICHEDIT的控件好像不接受WM_PASTE消息吧<br>WM_PASTE &nbsp;是程序发送此消息给editcontrol或combobox从剪贴板中得到数据<br>我的QQ是106187那位能指导一下,谢谢
 
WM_DropFiles消息不是发给RICHEDIT或RichEdit20A而是发给其父窗口的,<br>由其父窗口进行处理,和RICHEDIT或RichEdit20A没有关系<br>要是还不明白,请把你找到j2的过程写出来看看
 
j1:=FindWindowEx(GetForegroundWindow,0,'#32770',nil);<br>j2:=FindWindowEx(j1,0,'AfxWnd42',nil);<br>j3:=FindWindowEx(j2,0,'RICHEDIT',nil);<br>j3是找到的RICHEDI句柄,然后用你给的例子里的<br><br>List.Add('c:/1.exe');<br>DoDropFiles(j2,list);<br>这样成功不了,在线等,望指教
 
RICHEDIT的父窗口是AfxWnd42 而RichEdit20A的父窗口是#32770 &nbsp;<br>我也照办了也不行啊
 
QQ聊天窗口的结构:<br>-----#32770--与 XXX 交谈中<br> &nbsp; &nbsp; &nbsp; &nbsp;|----BUTTON---(最小化)<br> &nbsp; &nbsp; &nbsp; &nbsp;|----BUTTON---(最大化)<br> &nbsp; &nbsp; &nbsp; &nbsp;|----BUTTON---(关 &nbsp;闭)<br> &nbsp; &nbsp; &nbsp; &nbsp;|----#32770&lt;-------------------------------你需要给这个发消息才行<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|----RichEdit20A----消息显示窗口<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;|----AfxWnd42<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;|----RICHEDIT---消息输入窗口<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;| &nbsp; &nbsp; &nbsp; &nbsp;...<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...
 
利用消息&quot;拖放&quot;文件到其他程序,《Windows 95 程式设计指南》的例子 :<br><br>// -----------------------------------------------------------<br><br>// Module name: DFSrc.c -&gt;&gt; DFSrc.pas<br>// Written by: Jeffrey Richter<br>// Notices: Copyright (c) 1995 Jeffrey Richter<br>// Purpose: Drop file source library functions.<br>// Translator: 刘麻子, Liu_mazi@126.com<br><br>unit DFSrc;<br><br>interface<br><br>uses Windows, ShlObj, ShellAPI, CommDlg;<br><br> // 取第nIndex个字符串(文件名)<br>function MultiStrUtil_FindStr(szStrAll: PChar; nIndex: Integer; pnMax: PInteger): PChar; stdcall;<br><br> // 是否选择了多个文件<br>function FileOpenUtil_AreMultipleFilesSelected(pofn: POpenFilename): BOOL; stdcall;<br><br> // 文件列表内含文件个数<br>function FileOpenUtil_GetNumFiles(pofn: POpenFilename): Integer; stdcall;<br><br> // 取第nIndex个文件完整路径<br>function FileOpenUtil_GetFile(pofn: POpenFilename; nIndex: Integer; szPathname: PChar): Integer; stdcall;<br><br> // 目标或其父窗口是否接受文件拖放<br>function DFSrc_OkToDrop(ppt: PPOINT): HWND; stdcall;<br><br> // 分配头部结构内存块<br>function DFSrc_Create(ppt: PPOINT; fNC, fWide: BOOL): HDROP; stdcall;<br><br> // 向结构尾部追加文件<br>function DFSrc_AppendPathname(hdrop: HDROP; pvPathname: Pointer): HDROP; stdcall;<br> <br>implementation<br><br> // (单个)目标窗口是否接受文件拖放<br>function IsAcceptingFiles(hWnd: HWND): Boolean;<br>begin<br> &nbsp;Result := (GetWindowLong(hWnd, GWL_EXSTYLE) and WS_EX_ACCEPTFILES) &lt;&gt; 0;<br>end;<br><br> // 目标或其父窗口是否接受文件拖放<br>function DFSrc_OkToDrop(ppt: PPoint): HWND; stdcall;<br>var<br> &nbsp;ptMousePos: TPoint;<br>begin<br> &nbsp;if (ppt = nil) then<br> &nbsp;begin<br> &nbsp; &nbsp;ptMousePos.x := LOWORD(GetMessagePos());<br> &nbsp; &nbsp;ptMousePos.y := HIWORD(GetMessagePos());<br> &nbsp;end else<br> &nbsp; &nbsp;ptMousePos := ppt^; // 这句我自己加的 ^^<br><br> // 取得指定位置所处的窗口<br> &nbsp;Result := WindowFromPoint(ptMousePos);<br><br> // 看看目标窗囗或其父窗口是否接受文件拖放<br> &nbsp;while IsWindow(Result) and (IsAcceptingFiles(Result) = FALSE) do<br> &nbsp; &nbsp;Result := GetParent(Result);<br><br> // 如果接受, 返回其句柄, 否则返回&quot;空&quot;句柄<br> &nbsp;if (IsWindow(Result) = FALSE) or (IsAcceptingFiles(Result) = FALSE) then<br> &nbsp; &nbsp;Result := 0;<br>end;<br><br> // 分配头部结构内存<br>function DFSrc_Create(ppt: PPoint; fNC, fWide: BOOL): HDROP; stdcall;<br>var<br> &nbsp;PtDropFiles: PDropFiles;<br>begin<br> // 分配头部结构<br> &nbsp;if fWide then<br> &nbsp; &nbsp;Result := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf(TDropFiles) + SizeOf(WideChar))<br> &nbsp;else<br> &nbsp; &nbsp;Result := GlobalAlloc(GMEM_MOVEABLE or GMEM_ZEROINIT, SizeOf(TDropFiles) + SizeOf(Char));<br><br> // 填写结构成员<br> &nbsp;if (Result &lt;&gt; 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;PtDropFiles := PDropFiles(GlobalLock(Result));<br><br> &nbsp; &nbsp;PtDropFiles.pFiles := SizeOf(TDropFiles);<br> &nbsp; &nbsp;PtDropFiles.pt := ppt^;<br> &nbsp; &nbsp;PtDropFiles.fNC := fNC;<br> &nbsp; &nbsp;PtDropFiles.fWide := fWide;<br><br> &nbsp; &nbsp;GlobalUnlock(Result);<br> &nbsp;end;<br>end;<br><br> // 向结构尾部追加文件<br>function DFSrc_AppendPathname(hDrop: HDROP; pvPathname: Pointer): HDROP; stdcall;<br>var<br> &nbsp;PtDropFiles: PDropFiles;<br> &nbsp;szPathA: PChar;<br> &nbsp;szPathW: PWideChar;<br> &nbsp;nOffsetOfNewPathname, nPathSize: Integer;<br>begin<br> &nbsp;PtDropFiles := PDropFiles(GlobalLock(hdrop));<br><br> // 定位至结构头尾部<br> &nbsp;szPathA := PChar(DWORD(PtDropFiles) + PtDropFiles.pFiles);<br> &nbsp;szPathW := PWideChar(szPathA);<br><br> // 是Unicode宽字符?<br> &nbsp;if (PtDropFiles.fWide) then<br> &nbsp;begin<br> &nbsp; // 定位尾部<br> &nbsp; &nbsp;while (szPathW^ &lt;&gt; #0) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;while (szPathW^ &lt;&gt; #0) do Inc(szPathW);<br> &nbsp; &nbsp; &nbsp;Inc(szPathW);<br> &nbsp; &nbsp;end;<br><br> &nbsp; // 首尾间距<br> &nbsp; &nbsp;nOffsetOfNewPathname := Integer(szPathW) - Integer(PtDropFiles);<br> &nbsp; &nbsp;<br> &nbsp; // 新串长度<br> &nbsp; &nbsp;nPathSize := SizeOf(WideChar) * (lstrlenW(pvPathname) + 2);<br> &nbsp;end else<br> &nbsp;begin<br> &nbsp; // 定位尾部<br> &nbsp; &nbsp;while (szPathA^ &lt;&gt; #0) do<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;while (szPathA^ &lt;&gt; #0) do Inc(szPathA);<br> &nbsp; &nbsp; &nbsp;Inc(szPathA);<br> &nbsp; &nbsp;end;<br><br> &nbsp; // 首尾间距<br> &nbsp; &nbsp;nOffsetOfNewPathname := Integer(szPathA) - Integer(PtDropFiles);<br><br> &nbsp; // 新串长度<br> &nbsp; &nbsp;nPathSize := SizeOf(Char) * (lstrlenA(pvPathname) + 2);<br> &nbsp;end;<br><br> &nbsp;GlobalUnlock(hdrop);<br><br> // 重新分配<br> &nbsp;hdrop :=<br> &nbsp; &nbsp;GlobalReAlloc(hdrop, nPathSize + nOffsetOfNewPathname, GMEM_MOVEABLE or GMEM_ZEROINIT);<br><br> // 分配成功<br> &nbsp;if (hdrop &lt;&gt; 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;PtDropFiles := PDropFiles(GlobalLock(hdrop));<br> &nbsp; &nbsp;if (PtDropFiles.fWide) then<br> &nbsp; &nbsp; &nbsp;lstrcmpW(PWideChar(Integer(PtDropFiles) + nOffsetOfNewPathname), pvPathname)<br> &nbsp; &nbsp;else<br> &nbsp; &nbsp; &nbsp;lstrcpyA(PChar(Integer(PtDropFiles) + nOffsetOfNewPathname), pvPathname);<br> &nbsp; &nbsp;GlobalUnlock(hdrop);<br> &nbsp;end;<br><br> // 返回句柄 <br> &nbsp;Result := hdrop;<br>end;<br><br> // 取第nIndex个字符串(文件名)<br>function MultiStrUtil_FindStr(szStrAll: PChar; nIndex: Integer; pnMax: PInteger): PChar; stdcall;<br> &nbsp; &nbsp;// 定位#0, 单个字符串尾部<br> &nbsp;function SearchEnd(const P: PChar): PChar;<br> &nbsp;begin<br> &nbsp; &nbsp;Result := P;<br> &nbsp; &nbsp;while (Result^ &lt;&gt; #0) do Inc(Result);<br> &nbsp;end;<br>var<br> &nbsp;nNumStrs: Integer;<br> &nbsp;szStrSingle: PChar;<br>begin<br> &nbsp;if (nIndex = -1) then nIndex := MaxInt;<br><br> // 定位第nIndex个字符串<br> &nbsp;szStrSingle := szStrAll;<br> &nbsp;nNumStrs := 0; &nbsp;<br> &nbsp;while (szStrSingle^ &lt;&gt; #0) and (nNumStrs &lt; nIndex) do<br> &nbsp;begin<br> &nbsp; &nbsp;szStrSingle := SearchEnd(szStrSingle) + 1;<br> &nbsp; &nbsp;Inc(nNumStrs);<br> &nbsp;end;<br><br> &nbsp;if (pnMax &lt;&gt; nil) then pnMax^ := nNumStrs;<br><br> // 一直搜索到尾部也未找到<br> &nbsp;if (nNumStrs &lt; nIndex) then<br> &nbsp; &nbsp;Result := nil<br> &nbsp;else<br> &nbsp; &nbsp;Result := szStrSingle;<br>end;<br><br> // 是否选择了多个文件<br>function FileOpenUtil_AreMultipleFilesSelected(pofn: POpenFilename): BOOL; stdcall;<br>begin<br> // 如果选择多个文件,nFileOffset是到第一个文件名的偏移(肯定超过路径字符串的长度)<br> &nbsp;Result := lstrlen(pofn.lpstrFile) &lt; pofn.nFileOffset;<br>end;<br><br> // 文件列表内含文件个数<br>function FileOpenUtil_GetNumFiles(pofn: POpenFilename): Integer; stdcall;<br>begin<br> &nbsp;Result := 1;<br> &nbsp;if FileOpenUtil_AreMultipleFilesSelected(pofn) then<br> &nbsp;begin<br> &nbsp; // 最后一个(也即列表长度)<br> &nbsp; &nbsp;MultiStrUtil_FindStr(pofn.lpstrFile, -1, @Result);<br> &nbsp; &nbsp;Dec(Result);<br> &nbsp;end;<br>end;<br><br> // 取第nIndex个文件完整路径<br>function FileOpenUtil_GetFile(pofn: POpenFilename; nIndex: Integer; szPathname: PChar): Integer; stdcall;<br>begin<br> &nbsp;lstrcpy(szPathname, MultiStrUtil_FindStr(pofn.lpstrFile, 0, nil));<br> &nbsp;if FileOpenUtil_AreMultipleFilesSelected(pofn) then<br> &nbsp;begin<br> &nbsp; &nbsp;lstrcat(szPathname, '/');<br> &nbsp; &nbsp;lstrcat(szPathname, MultiStrUtil_FindStr(pofn.lpstrFile, nIndex + 1, nil));<br> &nbsp;end;<br> &nbsp;Result := lstrlen(szPathname);<br>end;<br><br>end.<br><br>// -----------------------------------------------------------<br><br>// Module name: DFSrcDem.c -&gt;&gt; DFSrcDem.dpr<br>// Written by: Jeffrey Richter<br>// Notices: Copyright (c) 1995 Jeffrey Richter<br>// Purpose: Demonstrates how to create a dropfile source application<br>// Translator: 刘麻子, Liu_mazi@126.com<br><br>program DFSrcDem;<br><br>{$R DFSrcDem.res}<br><br>uses Windows, Messages, ShellAPI, CommDlg, DFSrc in 'DFSrc.pas';<br><br>const<br> // 光标资源ID<br> &nbsp;IDC_DROPOK = 103;<br><br> // 图标资源ID<br> &nbsp;IDI_DFSRCDEMO = 104;<br><br> // 模板资源ID<br> &nbsp;IDD_DFSRCDEMO = 105;<br><br> // 控件标识ID <br> &nbsp;IDC_NUMFILES = 1001;<br> &nbsp;IDC_SELECTFILES = 1002;<br> &nbsp;IDC_PATHNAMELIST = 1003;<br><br> // WM_INITDIALOG消息处理<br>function DFSrcDemo_OnInitDialog(hWnd, hWndFocus: HWND; lParam: LPARAM): BOOL;<br>begin<br> // 设置窗体图标<br> &nbsp;SendMessage(hWnd, WM_SETICON, ICON_BIG, LoadIcon(HInstance, MakeIntResource(IDI_DFSRCDEMO)));<br><br> // 加水平滚动条<br> &nbsp;SendMessage(GetDlgItem(hWnd, IDC_PATHNAMELIST),<br> &nbsp; &nbsp;LB_SETHORIZONTALEXTENT, MAX_PATH * LOWORD(GetDialogBaseUnits()), 0);<br><br> // 接受默认焦点<br> &nbsp;Result := TRUE;<br>end;<br><br> // WM_COMMAND消息处理<br>procedure DFSrcDemo_OnCommand(hWnd: HWND; id: Integer; hWndCtl: HWND; codeNotify: UINT);<br>var<br> &nbsp;szAllFileNames: array[0..1024] of Char;<br> &nbsp;szPathname: array[0..MAX_PATH] of Char;<br> &nbsp;ofn: TOpenFilename;<br> &nbsp;nNumFiles, nIndex: Integer;<br> &nbsp;hWndLB: LongWord; // HWND<br>begin<br> &nbsp;case (id) of<br> &nbsp; &nbsp;IDCANCEL: // 关闭对话框<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;EndDialog(hWnd, id);<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;IDC_SELECTFILES: // 选择文件<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; // 清空ListBox<br> &nbsp; &nbsp; &nbsp; &nbsp;hWndLB := GetDlgItem(hWnd, IDC_PATHNAMELIST);<br> &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(hWndLB, LB_RESETCONTENT, 0, 0);<br><br> &nbsp; &nbsp; &nbsp; // 初始化ofn结构<br> &nbsp; &nbsp; &nbsp; &nbsp;ZeroMemory(@ofn, SizeOf(TOpenFilename));<br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.lStructSize := SizeOf(TOpenFilename);<br><br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.hwndOwner := hWnd;<br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.lpstrFilter := 'All files'#0'*.*'#0;<br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.Flags := OFN_ALLOWMULTISELECT or OFN_FILEMUSTEXIST or OFN_HIDEREADONLY or OFN_EXPLORER;<br><br> &nbsp; &nbsp; &nbsp; &nbsp;szAllFileNames[0] := #0;<br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.lpstrFile := @szAllFileNames[0];<br> &nbsp; &nbsp; &nbsp; &nbsp;ofn.nMaxFile := SizeOf(szAllFileNames);<br><br> &nbsp; &nbsp; &nbsp; // 选择了文件<br> &nbsp; &nbsp; &nbsp; &nbsp;if GetOpenFileName(ofn) then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; // 选择文件的数量<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nNumFiles := FileOpenUtil_GetNumFiles(@ofn);<br><br> &nbsp; &nbsp; &nbsp; &nbsp; // 添加至ListBox<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;for nIndex := 0 to nNumFiles - 1 do<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;FileOpenUtil_GetFile(@ofn, nIndex, szPathname);<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SendMessage(hWndLB, LB_ADDSTRING, 0, Integer(@szPathname[0]));<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp; &nbsp;end else<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nNumFiles := 0;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp; &nbsp; // 选择文件的数量<br> &nbsp; &nbsp; &nbsp; &nbsp;SetDlgItemInt(hWnd, IDC_NUMFILES, nNumFiles, FALSE);<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp;end; // END: case (id) of<br>end;<br><br> // WM_LBUTTONDOWN消息处理<br>procedure DFSrcDemo_OnLButtonDown(hWnd: HWND; fDoubleClick: BOOL; nIndex, y: Integer; keyFlags: UINT);<br>begin<br> &nbsp;if SendMessage(GetDlgItem(hWnd, IDC_PATHNAMELIST), LB_GETCOUNT, 0, 0) = 0 then<br> &nbsp; // 文件列表为空<br> &nbsp; &nbsp;MessageBox(GetActiveWindow(), 'No files to drop.', 'DFSrcDem', 0)<br> &nbsp;else<br> &nbsp; // 捕获鼠标消息<br> &nbsp; &nbsp;SetCapture(hWnd);<br>end;<br><br> // WM_MOUSEMOVE消息处理<br>procedure DFSrcDemo_OnMouseMove(hWnd: HWND; x, y: Integer; keyFlags: UINT);<br>begin<br> // 鼠标并未按下(说明不是'拖拽')<br> &nbsp;if (GetCapture() &lt;&gt; hWnd) then Exit;<br><br> // 根据鼠标下窗体的风格设置光标<br> &nbsp;if IsWindow(DFSrc_OkToDrop(nil)) then<br> &nbsp; &nbsp;SetCursor(LoadCursor(HInstance, MakeIntResource(IDC_DROPOK)))<br> &nbsp;else<br> &nbsp; &nbsp;SetCursor(LoadCursor(0, IDC_NO));<br>end;<br><br> // WM_LBUTTONUP消息处理<br>procedure DFSrcDemo_OnLButtonUp(hWnd: HWND; x, y: Integer; keyFlags: UINT);<br>var<br> &nbsp;hWndTarget, hWndLB: LongWord; // HWND<br> &nbsp;rc: TRect;<br> &nbsp;ptMousePos: TPoint; // **<br> &nbsp;nIndex, nNumFiles: Integer;<br> &nbsp;hdrop, hdropT: Longint; // HDROP<br> &nbsp;szPathName: array[0..MAX_PATH] of Char;<br>begin<br> // 之前并非拖拽状态<br> &nbsp;if (GetCapture() &lt;&gt; hWnd) then Exit;<br><br> // 释放鼠标消息捕捉<br> &nbsp;ReleaseCapture();<br><br> // 文件拖放目标窗口<br> &nbsp;GetCursorPos(ptMousePos); // **<br> &nbsp;hWndTarget := DFSrc_OkToDrop(@ptMousePos);<br><br> // 如果窗口句柄非法<br> &nbsp;if (IsWindow(hWndTarget) = FALSE) then Exit;<br><br> // 窗口客户区范围<br> &nbsp;GetClientRect(hWndTarget, rc);<br> &nbsp;ScreenToClient(hWndTarget, ptMousePos);<br><br> // 分配内存并初始化<br> &nbsp;hdrop := DFSrc_Create(@ptMousePos, not PtInRect(rc, ptMousePos), FALSE);<br> &nbsp;if (hdrop = 0) then<br> &nbsp;begin<br> &nbsp; &nbsp;MessageBox(GetActiveWindow(), 'Insufficient memory to drop file(s).', 'DFSrcDem', 0);<br> &nbsp; &nbsp;Exit;<br> &nbsp;end;<br><br> // ListBox文件数量<br> &nbsp;hWndLB := GetDlgItem(hWnd, IDC_PATHNAMELIST);<br> &nbsp;nNumFiles := SendMessage(GetDlgItem(hWnd, IDC_PATHNAMELIST), LB_GETCOUNT, 0, 0);<br> &nbsp;<br> // 添加文件列表<br> &nbsp;for nIndex := 0 to nNumFiles - 1 do<br> &nbsp;begin<br> &nbsp; &nbsp;SendMessage(hWndLB, LB_GETTEXT, nIndex, Integer(@szPathName[0]));<br> &nbsp; &nbsp;hdropT := DFSrc_AppendPathname(hdrop, @szPathName[0]);<br><br> &nbsp; &nbsp;if (hdropT = 0) then<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;MessageBox(GetActiveWindow(), 'Insufficient memory to drop file(s).', 'DFSrcDem', 0);<br> &nbsp; &nbsp; &nbsp;hdrop := GlobalFree(hdrop);<br> &nbsp; &nbsp; &nbsp;Break; // Terminates the 'for' loop.<br> &nbsp; &nbsp;end else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;hdrop := hdropT;<br> &nbsp; &nbsp;end;<br> &nbsp;end;<br><br> // 添加文件列表成功<br> &nbsp;if (hdrop &lt;&gt; 0) then<br> &nbsp;begin<br> &nbsp; // 向拖放目标发消息<br> &nbsp; &nbsp;PostMessage(hWndTarget, WM_DROPFILES, hdrop, 0);<br><br> &nbsp; // 清空文件列表<br> &nbsp; &nbsp;SetDlgItemInt(hWnd, IDC_NUMFILES, 0, FALSE);<br> &nbsp; &nbsp;SendMessage(hWndLB, LB_RESETCONTENT, 0, 0);<br><br> &nbsp; // 注意: 我们不应该用 GlobalFree(hdrop) 来释放这块内存,<br> &nbsp; // 系统会在目标进程空间重新分配内存并作拷贝之后自动释放,<br> &nbsp; // 新的内存块, 由目标进程负责调用DragFinish()来释放 ..<br> &nbsp;end;<br>end;<br><br> // 对话框消息处理回调<br>function DFSrcDemo_Proc(hWnd: HWND; uMsg: UINT; wParam: WPARAM; lParam: LPARAM): BOOL; stdcall;<br>begin<br> &nbsp;case (uMsg) of<br> &nbsp; &nbsp;WM_INITDIALOG:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := BOOL(SetWindowLong(hWnd, DWL_MSGRESULT,<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Longint(DFSrcDemo_OnInitDialog(hWnd, wParam, lParam))));<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_COMMAND:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;DFSrcDemo_OnCommand(hWnd, LOWORD(wParam), lParam, HIWORD(wParam));<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := TRUE;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_LBUTTONDOWN:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;DFSrcDemo_OnLButtonDown(hWnd, FALSE, LOWORD(lParam), HIWORD(lParam), wParam);<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := TRUE;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_MOUSEMOVE:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;DFSrcDemo_OnMouseMove(hWnd, LOWORD(lParam), HIWORD(lParam), wParam);<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := TRUE;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;WM_LBUTTONUP:<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;DFSrcDemo_OnLButtonUp(hWnd, LOWORD(lParam), HIWORD(lParam), wParam);<br> &nbsp; &nbsp; &nbsp; &nbsp;Result := TRUE;<br> &nbsp; &nbsp; &nbsp;end;<br><br> &nbsp; &nbsp;else Result := FALSE;<br> &nbsp;end;<br>end;<br><br> // 程序'主线程'入口<br>begin<br> &nbsp;DialogBox(HInstance, MakeIntResource(IDD_DFSRCDEMO), 0, @DFSrcDemo_Proc);<br>end.
 
完整代码和电子书可在此下载: http://www.2ccc.com/article.asp?articleid=2063<br>请看“第七章 文件的拖放(Drag-and-Drop)技术”中的第二小节的范例“2.DFSrcDem”
 
就是说直接相窗口发WM_DROPFILES是无效的,必须用GlobalAlloc申请全局内存块构造<br>HDROP结构,传给待要接受拖拽的窗体.
 
呵呵,请问daqun:您在用GlobalAlloc申请全局内存块构造好了HDROP结构之后,用什么发过去呢?<br>不是用PostMessage发送WM_DROPFILES吗?难道是神七?[8D]
 
后退
顶部