1、c++版 cedit控件
.h
#pragma once
#include "afxwin.h"
// CMyEdit
class CMyEdit : public CEdit
{
DECLARE_DYNAMIC(CMyEdit)
public:
CMyEdit();
virtual ~CMyEdit();
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnDropFiles(HDROP hDropInfo);
};
.cpp
// MyEdit.cpp : 实现文件
//
#include "stdafx.h"
#include "MyEdit.h"
// CMyEdit
IMPLEMENT_DYNAMIC(CMyEdit, CEdit)
CMyEdit::CMyEdit()
{
}
CMyEdit::~CMyEdit()
{
}
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_DROPFILES()
END_MESSAGE_MAP()
// CMyEdit 消息处理程序
void CMyEdit::OnDropFiles(HDROP hDropInfo)
{
if (hDropInfo)
{
CString strPath;
int nDrag = 0; //拖拽文件的数量
nDrag = DragQueryFile(hDropInfo, 0xFFFFFFFF, NULL, 0);
for (int i = 0; i < nDrag; i++)
{
// 被拖拽的文件的文件名
TCHAR Path[MAX_PATH + 1] = { 0 };
int len = DragQueryFile(hDropInfo, i, Path, MAX_PATH);
// 得到被拖拽的文件名与长度
CString str = Path;
CString xml = _T(".xml");
CString fix = str.Mid(len - 4, 4);
if (fix.CompareNoCase(xml) == 0)
{
strPath += Path;
strPath += _T("\r\n");
}
}
SetWindowText(strPath);
}
DragFinish(hDropInfo);
CEdit::OnDropFiles(hDropInfo);
}
2、c#版 textBox控件
textBoxRePath.DragDrop += textBoxRePath_DragDrop;
/// <summary>
/// 拖拽事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void textBoxRePath_DragDrop(object sender, DragEventArgs e)
{
string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();//GetValue(0) 为第1个文件全路径
if (string.Compare(Path.GetExtension(path), ".xml", true) == 0)
{
textBoxRePath.Text = path;
}
this.textBoxRePath.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状
}