工控编程吧
标题:
上位机MFC实现编辑框之间内容的拖拽
[打印本页]
作者:
qq263946146
时间:
2019-7-5 14:51
标题:
上位机MFC实现编辑框之间内容的拖拽
效果展示
(, 下载次数: 0)
上传
点击文件名下载附件
我们知道,缺省的编辑框仅支持剪接板的剪切、拷贝、粘贴操作,不支持拖放操作,
当前这个例程扩展了CEdit类为CDragEdit类,并再实现了CDEDropTarget和CDEDataSource,
实现编辑之间拖放文本的操作,参见上图。
可以直接复制下面代码使用,或下载工程源代码学习:
(, 下载次数: 0)
上传
点击文件名下载附件
实现过程
1.新建一基于对话框的应用程序,如上图添加编辑框控件与排版。
2.将例程根目录内两文件CDRAGEDIT.CPP,CDRAGEDIT.H复制到自己工程根目录,
并加载导入到自己工程,两文件源代码也可以在帖子底部复制使用。
这样自己的工程就多出了三个类CDragEdit,CDEDropTarget,CDEDataSource,如图:
(, 下载次数: 0)
上传
点击文件名下载附件
3.然后就是这集成类的使用。
在对话框头文件包含类头文件#include "CDragEdit.h",
添加类的编译框对象
CDragEdit m_edit2;
CDragEdit m_edit1;
DDX_Control(pDX, IDC_EDIT2, m_edit2);
DDX_Control(pDX, IDC_EDIT1, m_edit1);
并初始化两个编辑框对象
m_edit1.Init();
m_edit2.Init();
m_edit1.SetFocus();
4.这样编译执行例程,在两个编辑框内输入的内容就可以想到拖拽了。
下面是上面提到的两文件的源代码,可以复制使用
#if !defined(AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_)
#define AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// CDragEdit.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CDragEdit window
#include <afxole.h>
class CDragEdit;
class CDEDropTarget : public COleDropTarget
{
public:
CDEDropTarget()
{
ms_bBeginDrop=FALSE;
ms_pEditCtl=NULL;
}
BOOL Register(CDragEdit* pEdit);
BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
void OnDragLeave(CWnd* pWnd);
DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
DROPEFFECT OnDragScroll(CWnd* pWnd, DWORD dwKeyState, CPoint point);
private:
BOOL ms_bBeginDrop;
CDragEdit* ms_pEditCtl;
};
class CDEDataSource : public COleDataSource
{
};
#define CDragEditBase CEdit
class CDragEdit : public CDragEditBase
{
// Construction
public:
CDragEdit();
// Attributes
public:
enum { DE_ENABLE_DRAG=1,
DE_ENABLE_DROP=2
};
private:
static BOOL ms_bOleInit;
BOOL ms_bDragInit;
int ms_nEnableFlags;
BOOL ms_bInDragging;
BOOL ms_bDropEqualDrag;
int ms_nDropPtLine;
int ms_nDropPtPos;
CDEDropTarget ms_dropTarget;
CDEDataSource ms_dropSource;
// Operations
public:
BOOL Init(int nFlags=(DE_ENABLE_DRAG|DE_ENABLE_DROP));
BOOL IsInDragging() {return ms_bInDragging;}
void SetDropEqualDrag(BOOL bEqual) {ms_bDropEqualDrag=bEqual;}
void SetDropPos(int nLine, int nPos) {ms_nDropPtLine=nLine,
ms_nDropPtPos=nPos;}
BOOL GetCurRange(int& nLine1, int& nPos1, int& nLine2, int& nPos2);
BOOL GetLinePosByCursor(int& nLine, int& nPos);
BOOL SetCaretByCursor();
BOOL SetCaret(int nLine, int nPos);
BOOL DrawCaretByCursor();
CPoint GetPosFromLinePos(int nLine, int nPos)
{return PosFromChar(_LinePosToChar(nLine,nPos));}
//we reimpelmented this function for fixing SDK's bug
CPoint PosFromChar(UINT uChar);
BOOL IsInSelRange();
BOOL EnableDrag()
{return (ms_nEnableFlags & DE_ENABLE_DRAG) ? TRUE : FALSE;}
BOOL EnableDrop()
{return (ms_nEnableFlags & DE_ENABLE_DROP) ? TRUE : FALSE;}
private:
BOOL _GetSelText(CString& str);
void _CharToLinePos(int nChar, int* pnLine, int* pnPos)
{
if (nChar<0) nChar=0;
*pnLine=LineFromChar(nChar);
*pnPos=nChar-LineIndex(*pnLine);
}
int _LinePosToChar(int nLine, int nPos) {return LineIndex(nLine)+nPos;}
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDragEdit)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CDragEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CDragEdit)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_)
复制代码
#if !defined(AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_)
#define AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_
#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000
// CDragEdit.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CDragEdit window
#include <afxole.h>
class CDragEdit;
class CDEDropTarget : public COleDropTarget
{
public:
CDEDropTarget()
{
ms_bBeginDrop=FALSE;
ms_pEditCtl=NULL;
}
BOOL Register(CDragEdit* pEdit);
BOOL OnDrop(CWnd* pWnd, COleDataObject* pDataObject, DROPEFFECT dropEffect, CPoint point);
DROPEFFECT OnDragEnter(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
void OnDragLeave(CWnd* pWnd);
DROPEFFECT OnDragOver(CWnd* pWnd, COleDataObject* pDataObject, DWORD dwKeyState, CPoint point);
DROPEFFECT OnDragScroll(CWnd* pWnd, DWORD dwKeyState, CPoint point);
private:
BOOL ms_bBeginDrop;
CDragEdit* ms_pEditCtl;
};
class CDEDataSource : public COleDataSource
{
};
#define CDragEditBase CEdit
class CDragEdit : public CDragEditBase
{
// Construction
public:
CDragEdit();
// Attributes
public:
enum { DE_ENABLE_DRAG=1,
DE_ENABLE_DROP=2
};
private:
static BOOL ms_bOleInit;
BOOL ms_bDragInit;
int ms_nEnableFlags;
BOOL ms_bInDragging;
BOOL ms_bDropEqualDrag;
int ms_nDropPtLine;
int ms_nDropPtPos;
CDEDropTarget ms_dropTarget;
CDEDataSource ms_dropSource;
// Operations
public:
BOOL Init(int nFlags=(DE_ENABLE_DRAG|DE_ENABLE_DROP));
BOOL IsInDragging() {return ms_bInDragging;}
void SetDropEqualDrag(BOOL bEqual) {ms_bDropEqualDrag=bEqual;}
void SetDropPos(int nLine, int nPos) {ms_nDropPtLine=nLine,
ms_nDropPtPos=nPos;}
BOOL GetCurRange(int& nLine1, int& nPos1, int& nLine2, int& nPos2);
BOOL GetLinePosByCursor(int& nLine, int& nPos);
BOOL SetCaretByCursor();
BOOL SetCaret(int nLine, int nPos);
BOOL DrawCaretByCursor();
CPoint GetPosFromLinePos(int nLine, int nPos)
{return PosFromChar(_LinePosToChar(nLine,nPos));}
//we reimpelmented this function for fixing SDK's bug
CPoint PosFromChar(UINT uChar);
BOOL IsInSelRange();
BOOL EnableDrag()
{return (ms_nEnableFlags & DE_ENABLE_DRAG) ? TRUE : FALSE;}
BOOL EnableDrop()
{return (ms_nEnableFlags & DE_ENABLE_DROP) ? TRUE : FALSE;}
private:
BOOL _GetSelText(CString& str);
void _CharToLinePos(int nChar, int* pnLine, int* pnPos)
{
if (nChar<0) nChar=0;
*pnLine=LineFromChar(nChar);
*pnPos=nChar-LineIndex(*pnLine);
}
int _LinePosToChar(int nLine, int nPos) {return LineIndex(nLine)+nPos;}
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CDragEdit)
//}}AFX_VIRTUAL
// Implementation
public:
virtual ~CDragEdit();
// Generated message map functions
protected:
//{{AFX_MSG(CDragEdit)
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
/////////////////////////////////////////////////////////////////////////////
//{{AFX_INSERT_LOCATION}}
// Microsoft Developer Studio will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_CDRAGEDIT_H__871E3CD6_6359_11D1_8251_444553540000__INCLUDED_)
复制代码
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4