Announcement

Collapse
No announcement yet.

Per Drag&Drop Link eines Bildes aus einem Internet-Browser ermitteln

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Per Drag&Drop Link eines Bildes aus einem Internet-Browser ermitteln

    Hallo hier nochmal eine Frage zum Thema Drag&Drop.
    Bei meinem Programm möchte ich ein Bild oder allgemein eine Datei aus einem Internet-Browser per Drag&Drop auf mein Programm ziehen.

    Mein erster Versuch sieht folgendermassen aus
    ---
    procedure TForm5.WMDROPFILE(var msg: TMessage);
    var
    DateiURL: string;
    hDrop: Cardinal;
    fName: array[0..MAX_PATH] of Char;
    FFileName: string;
    begin
    hDrop := Msg.WParam;
    fName := '';
    if DragQueryFile(hDrop, 0, fName, MAX_PATH) > 0
    then begin
    DateiURL := string(fName);
    DragFinish(hDrop);
    end;
    end;
    ---
    Leider bekomme ich da nur einen Pfad auf meiner Festplatte zurück (ich nehme an das der Browser hier die Datei zwischenspeichert) und nicht den Internetlink über den ich dann später mit meinem Programm die Datei Downloaden möchte.

    Falls jemand eine idee hat wie man an den Link kommt, wäre ich für hilfe sehr dankbar.

  • #2
    Hallo ich habe in einem anderen Forum eine lösung für das Problem gefunden.
    Hier der Source-Code falls jemand anderes ein ähnliches Problem hat:
    -----------
    unit Unit1;

    interface

    uses
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
    Dialogs, ComCtrls, ActiveX, ShlObj, ComObj, StdCtrls, Shellapi;

    type
    TForm1 = class(TForm, IDropTarget)
    Memo1: TMemo;
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    private
    { Private declarations }
    // IDropTarget
    function DragEnter(const dataObj: IDataObject;
    grfKeyState: Longint;
    pt: TPoint;
    var dwEffect: Longint): HResult; stdcall;
    function DragOver(grfKeyState: Longint;
    pt: TPoint;
    var dwEffect: Longint): HResult; stdcall;
    function DragLeave: HResult; stdcall;
    function Drop(const dataObj: IDataObject;
    grfKeyState: Longint; pt: TPoint;
    var dwEffect: Longint): HResult; stdcall;
    // IUnknown
    // Ignore referance counting
    function _AddRef: Integer; stdcall;
    function _Release: Integer; stdcall;
    public
    { Public-Deklarationen }
    end;

    var
    Form1: TForm1;

    implementation

    {$R *.dfm}

    procedure TForm1.FormCreate(Sender: TObject);
    begin
    OleInitialize(nil);
    {Allow window to accept drop events}
    OleCheck(RegisterDragDrop(Handle, Self));
    { Execute Wordpad for testing }
    // ShellExecute(Handle, 'open', 'wordpad', 'c:\Test.doc', nil, SW_SHOW);
    end;

    // OnDestroy does the exact opposite. It calls RevokeDropTarget to indicate that
    // drop events are no longer accepted.
    // It then calls OleUninitialize, since the application is finished using all OLE functions.

    {-----------------------------------------------------------------}
    { IDropTarget-Implementierung }
    {-----------------------------------------------------------------}
    function TForm1.DragEnter(const dataObj: IDataObject;
    grfKeyState: Longint;
    pt: TPoint;
    var dwEffect: Longint): HResult;
    begin
    dwEffect := DROPEFFECT_COPY;
    Result := S_OK;
    end;

    function TForm1.DragOver(grfKeyState: Longint;
    pt: TPoint;
    var dwEffect: Longint): HResult;
    begin
    dwEffect := DROPEFFECT_COPY;
    Result := S_OK;
    end;

    function TForm1.DragLeave: HResult;
    begin
    Result := S_OK;
    end;

    function TForm1._AddRef: Integer;
    begin
    Result := 1;
    end;

    function TForm1._Release: Integer;
    begin
    Result := 1;
    end;

    function TForm1.Drop(const dataObj: IDataObject;
    grfKeyState: Longint;
    pt: TPoint;
    var dwEffect: Longint): HResult;
    var
    aFmtEtc: TFORMATETC;
    aStgMed: TSTGMEDIUM;
    pData: PChar;
    begin
    {Make certain the data rendering is available}
    if (dataObj = nil) then
    raise Exception.Create('IDataObject-Pointer is not valid!');
    with aFmtEtc do
    begin
    cfFormat := CF_TEXT;
    ptd := nil;
    dwAspect := DVASPECT_CONTENT;
    lindex := -1;
    tymed := TYMED_HGLOBAL;
    end;
    {Get the data}
    OleCheck(dataObj.GetData(aFmtEtc, aStgMed));
    try
    {Lock the global memory handle to get a pointer to the data}
    pData := GlobalLock(aStgMed.hGlobal);
    { Replace Text }
    Memo1.Text := pData;
    finally
    {Finished with the pointer}
    GlobalUnlock(aStgMed.hGlobal);
    {Free the memory}
    ReleaseStgMedium(aStgMed);
    end;
    Result := S_OK;
    end;

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
    {Finished accepting drops}
    RevokeDragDrop(Handle);
    OleUninitialize;
    end;

    end.
    -----------
    Auf der Form befindet sich nur das Memo.

    mfg Christian Schlak

    Comment

    Working...
    X