BCB 中实现文件拖放操作
以前在做公交站点查询的时候,曾经用到过文件拖放的功能,是用C#实现的,代码非常简单。但是同样的功能,即拖动文件到窗体,获得文件名,在BCB 下面实现却略显繁琐,或许会有更好的方式,但是我还没发现。
代码如下:
//在.h文件里加上:
private : // User declarations
void __fastcall AcceptFiles (TMessage& Msg);
public : // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER( WM_DROPFILES, TMessage, AcceptFiles)
END_MESSAGE_MAP(TForm)
//在.cpp文件里
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DragAcceptFiles( this->Handle, true);
}
void __fastcall TForm1 ::AcceptFiles(TMessage& Msg)
{
const int m_nMaxFileNameLen=255;
int i, m_nCount;
char m_sFileName[m_nMaxFileNameLen];
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
m_nCount=DragQueryFile((HANDLE)Msg.WParam, 0xffffffff, m_sFileName , m_nMaxFileNameLen);
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
for (i=0;i<m_nCount;i ++)
{
DragQueryFile((HANDLE )Msg.WParam, i, m_sFileName , m_nMaxFileNameLen);
MessageBox( this->Handle,m_sFileName,"Drop File" ,MB_OK);
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
}
DragFinish ((HANDLE)Msg.WParam);
}
private : // User declarations
void __fastcall AcceptFiles (TMessage& Msg);
public : // User declarations
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER( WM_DROPFILES, TMessage, AcceptFiles)
END_MESSAGE_MAP(TForm)
//在.cpp文件里
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
DragAcceptFiles( this->Handle, true);
}
void __fastcall TForm1 ::AcceptFiles(TMessage& Msg)
{
const int m_nMaxFileNameLen=255;
int i, m_nCount;
char m_sFileName[m_nMaxFileNameLen];
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
m_nCount=DragQueryFile((HANDLE)Msg.WParam, 0xffffffff, m_sFileName , m_nMaxFileNameLen);
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
for (i=0;i<m_nCount;i ++)
{
DragQueryFile((HANDLE )Msg.WParam, i, m_sFileName , m_nMaxFileNameLen);
MessageBox( this->Handle,m_sFileName,"Drop File" ,MB_OK);
//ZeroMemory(m_sFileName,m_nMaxFileNameLen+1);
}
DragFinish ((HANDLE)Msg.WParam);
}
1 条评论:
你好!
欢迎你访问我的网站http://findyou.blogspot.com/,我们相互链接吧!我做这个网站就是想让中文的blogger多交流!
我们很想找到你!
另外我可能把你的网址贴在帖子里推荐给大家,你不反对吧?
发表评论