VC如何实现拖拽功能...

VC如何实现拖拽功能...,第1张

派生一个编辑框类,基类为CEdit (子类化)

添加WM_DROPFILES消息

void CMyEdit::OnDropFiles(HDROP hDropInfo)

{

int fcount = ::DragQueryFile(hDropInfo,-1,NULL,NULL)

for(int i=0i<fcounti++)

{

int fsize = ::DragQueryFile(hDropInfo,i,NULL,NULL)

HANDLE hHeap = ::GetProcessHeap()

char *fname = (char *)::HeapAlloc(hHeap,HEAP_ZERO_MEMORY,fsize++)

::DragQueryFile(hDropInfo,i,fname,fsize)

this->SetWindowText(fname)

::HeapFree(hDropInfo,HEAP_ZERO_MEMORY,fname)

}

CEdit::OnDropFiles(hDropInfo)

}

在对话框OnInitDialog()中开启编辑框文件拖放功能

m_edit.DragAcceptFiles(1)

首先设置Form的AllowDrop=true

public partial class Form1 : Form

{

    public Form1()

    {

        InitializeComponent()

    }

    private void btnStart_Click(object sender, EventArgs e)

    {

    }

    private void Form1_DragEnter(object sender, DragEventArgs e)

    {

        if (e.Data.GetDataPresent(DataFormats.FileDrop))

        {

            e.Effect = DragDropEffects.Link 

        }

        else

        {

            e.Effect = DragDropEffects.None

        }

    }

    private void Form1_DragDrop(object sender, DragEventArgs e)

    {

        string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString()

        // 接下来可以通过filestream来上传文件。

    }

}

在DragDrop事件中能够得到拖放到窗体上的文件路径,然后使用filestream就可以上传了。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/6064833.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-13
下一篇 2023-03-13

发表评论

登录后才能评论

评论列表(0条)

保存