🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #155

Re: Drag-and-drop 101

#155
From: Remy Lebeau (TeamB)
Date: Tue, 17 Jun 2008 16:27
89 lines
3202 bytes
"Mike K" <nospam@nospam.net> wrote in message
news:4857aabb$1@newsgroups.borland.com...

> I would like to drag files (just file names) from windows explorer
> into a Borland control.

There are two ways to do that:

1) subclass the control to intercept the WM_DROPFILES message (obsolete, 
provided for backwards compatibility)

2) implement the IDropTarget interface (recommended)

>    END_MESSAGE_MAP

That has a required parameter:

    END_MESSAGE_MAP(TForm)

> void __fastcall WMDropFiles(TWMDropFiles &message)

That needs to be a member of the class:

    void __fastcall TForm1::WMDropFiles(TWMDropFiles &message)

>  for(int i=1;i<count; i++)

Your loop needs to start at 0, not 1.

> filename.SetLength(DragQueryFile((HDROP)message.Drop,i,fname.c_str(),MAX_PATH));

On the first iteration of the loop, fname is empty, so you are telling 
DragQueryFile() to write to a NULL buffer.  You need to allocate memory 
before passing it around.  In this situation, I would suggest using a 
static-length character array instead.

Try this code:

    private:
        MESSAGE void __fastcall WMDropFiles(TWMDropFiles &message);

    protected:
        virtual void __fastcall CreateWnd();
        virtual void __fastcall DestroyWnd();

    public:
        BEGIN_MESSAGE_MAP
            VCL_MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDropFiles)
        END_MESSAGE_MAP(TForm)


    void __fastcall TForm1::CreateWnd()
    {
        TForm::CreateWnd();
        DragAcceptFiles(Handle, TRUE);
    }

    void __fastcall TForm1::DestroyWnd()
    {
        DragAcceptFiles(Handle, FALSE);

        TForm::DestroyWnd();
    }

    void __fastcall TForm1::WMDropFiles(TWMDropFiles &message)
    {
        TCHAR filename[MAX_PATH+1];

        UINT count = DragQueryFile((HDROP)message.Drop, 0xFFFFFFFF, NULL, 
0);

        for(int i = 0; i < count; ++i)
        {
            memset(filename, 0, sizeof(filename));
            DragQueryFile((HDROP)message.Drop, i, filename, MAX_PATH);
            FileListBox1->Items->Add(filename);
        }

        DragFinish((HDROP)message.Drop);
    }

> Also, what would I use the TFileListBox's OnDragDrop method for?
> Drag/drop within the application?

Yes.


Gambit
Technical Details
Message-ID: <4857e627$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4857aabb$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,15dfb293aacb32ec
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4857aabb$1@newsgroups.borland.com>
Subject: Re: Drag-and-drop 101
Date: Tue, 17 Jun 2008 09:27:18 -0700
Lines: 92
Organization: Lebeau Software
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.3138
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3198
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 207.178.157.90
Message-ID: <4857e627$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1213720103 207.178.157.90 (17 Jun 2008 09:28:23 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail