如何实现拖拉文件到窗体中的列表框中?
是要把文件拖拉到一列表框中,只有拖拉到列表框中才可以接收文件,除列表框以外的任何地方都禁止接收。这个列表框是原生的TListBox。如何能让它处理触发的WM_DROPFILES消息?
[解决办法]
- Delphi(Pascal) code
procedure Tform1.FormCreate(Sender: TObject); begin DragAcceptFiles(Handle,True); end;procedure TfrmMain.DragFiles(var Msg: TWMDROPFILES);var Num,i:Integer; FName:array [0..255] of char; FName1:PWideChar;begin Num:=DragQueryFile(Msg.Drop,$FFFFFFFF,nil,0); for i:=0 to Num-1 do begin DragQueryFile(Msg.Drop,i,FName,SizeOf(FName)); Label3.Caption:=FName; GGPMemo1.Lines.LoadFromFile(StrPas(FName)); end; DragFinish(Msg.Drop);end;
[解决办法]
[解决办法]
- Delphi(Pascal) code
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, ExtCtrls;type TDBGrid = class(DBGrids.TDBGrid) private procedure WMDropFiles(var M: TWMDropFiles); message WM_DROPFILES; end; TForm1 = class(TForm) DBGrid1: TDBGrid; procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementationuses ShellAPI;{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);begin DragAcceptFiles(DBGrid1.Handle, True)end;{ TDBGrid }procedure TDBGrid.WMDropFiles(var M: TWMDropFiles);var I: Integer; P: PChar;begin inherited; GetMem(P, MAX_PATH); for I := 0 to DragQueryFile(M.Drop, $FFFFFFFF, P, MAX_PATH) - 1 do begin DragQueryFile(M.Drop, I, P, MAX_PATH); ShowMessage(P); end; FreeMem(P); DragFinish(M.Drop); M.Result := 0;end;end.