Is there a component or some function in Delphi so that I can drag a .txt file onto a form, and everything inside this file is copied into a StringGrid table?

    3 answers 3

    I don’t know the component, but for files, in principle, everything is not so difficult:

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TMemo = class(StdCtrls.TMemo) protected procedure WM_DROPFILES(var Message: TWMDropFiles); message WM_DROPFILES; end; TForm1 = class(TForm) Memo: TMemo; procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation uses ShellAPI; {$R *.dfm} procedure TForm1.FormCreate(Sender: TObject); begin DragAcceptFiles(Memo.Handle, True); end; { TMemo } procedure TMemo.WM_DROPFILES(var Message: TWMDropFiles); var NameSize: Cardinal; NewSize: Cardinal; FileName: PChar; Index: Cardinal; Count: Cardinal; begin // чтобы не делать вложенных try/finally FileName := nil; // Получаем количество перетащенных файлов Count := DragQueryFile(Message.Drop, $FFFFFFFF, FileName, 0); try // выделяем память под имя файла NameSize := 256; GetMem(FileName, NameSize); // ... и список их имён for Index := 0 to Count - 1 do begin NewSize := DragQueryFile(Message.Drop, Index, nil, 0) + 1; // +1 для #0 в конце имени // проверка на то, что нам хватит места if NewSize > NameSize then begin NameSize := NewSize; ReallocMem(FileName, NameSize); end; // получаем имя файла, обработка ошибок - в качестве самостоятельной работы :) DragQueryFile(Message.Drop, Index, FileName, NameSize); // делаем с ним всё что нам нужно Lines.Add(FileName); end; finally DragFinish(Message.Drop); if FileName <> nil then FreeMem(FileName); end; end; end. 

    Something like this. I took an example from here and modified it a bit so that the files were taken not by the form itself, but by TMemo on it.

    • I wrote Form7.Memo2.Lines.LoadFromFile (FileName). When you hover the cursor changes .. and all - Nick
    • You need to see all the code to understand something. - Alekcvp

    JEDI has a JvDragDrop component (non-visual). Allows you to flexibly adjust the parameters of "throwing" on different areas of your form.

    http://wiki.delphi-jedi.org/wiki/JVCL_Help:TJvDragDrop

      Components:

      1. Drag and Drop Component Suite
      2. JVCL ( JvDragDrop ).
      3. Raize DropMaster

      Programmatically:

      1. Delphi
      2. MSDN