怎样在vc程序中实现拖放操作?

zm_stone 2000-03-14 02:19:00
加精
...全文
453 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZPoint 2000-03-14
  • 打赏
  • 举报
回复
老歌新唱,你可去访问www.softWonder.com

2.MFC中用于对象拖放的类

MFC(Microsoft Foundation ClassLibrary)为实现对象拖放提供了如下三个类。为便于后边的
讨论我们先来熟悉一下这些类。

2.1.COleDataSource。用于启动一次拖放操作,并向系统提供拖放对象的数据。类中的成员
函数有如下三种:

a.设定提供数据的方式和使用的数据格式。提供数据的方式有两种,一种是即时方式,
另一种是延迟方式;即时方式需要在拖动开始之前提供数据;延迟方式不需要立即提供数据,当系
统请求有关数据时,由OnRenderData()等虚函数提供所需的数据。

可以用CacheGlobalData()等函数指定使用即时方式提供数据,也可以用DelayRenderData
()等函数指定使用延时方式提供数据。

b.响应请求,提供数据。应当重载OnRenderFileData()或其他相应的虚函数,以提供有关
数据(后边将详细讨论)。

c.实施拖放操作。调用函数DoDragDrop(),开始实施拖放操作。

2.2.OleDataTarget。用于准备接收拖放对象的目标窗口;一个窗口要想能够接收拖放对象,
必须包含一个COleDataTarget对象,并注册该对象。类中主要成员函数:

a.注册。函数Register()注册该对象,以便使窗口能够接收拖放对象。

b.响应拖放过程中的动作(虚成员函数) 当鼠标首次进入窗口时系统将调用
OnDragEnter(),当鼠标移出窗口时系统将调用OnDragLeave(), 当鼠标在窗口内移动,
系统将重复调用调用OnDragOver(),当对象在窗口内落下调用OnDrop()。

2.3.OleDataObject.用于接收拖放对象,类中主要成员函数有两种:

a.确定可以使用的数据格式。IsDataAvailable()等函数确定指定数据格式是否可用;

b.获取数据。GetData()、GetFileData()等函数用于按指定数据格式获得数据。

3.利用MFC实现对象拖放

要实现一次对象拖放,需要做三方面的工作:对象所在的窗口准备拖放对象并启拖动操作,接受对象的窗口响应有关拖放消息并接受落下的对象,以及拖放完成时的后期处理。以下分别予以
介绍。

3.1. 拖动操作的启动。拖放操作一般是从单击鼠标左键开始。在消息WM_LBUTTONDOWN的响应
函数OnLButtonDown(...)中,首先要判定是否选定了某一对象,如果未选定或选定多个,则不能进
行拖放操作;如果选定了一个对象,则可以进行拖放操作。

要启动一次拖放操作,需要先准备一个COleDataSource对象。注意到类COleClientIten和类
COleServerItem都是从类COleDataSource上派生的,如果选定的是COleClientItem对象或者是
COleServerItem对象,则可以直接使用;否则,需要生成一个COleDataSource对象,值得注意的
是:需要象上文中所说的,应该指定使用的数据格式,并按指定格式提供对象的有关数据。
ZPoint 2000-03-14
  • 打赏
  • 举报
回复
老歌新唱,你可去访问www.softWonder.com
cqjiang 2000-03-14
  • 打赏
  • 举报
回复
MFC的DOC-VIEW框架自带脱放操作,不加任何代码。
haihong 2000-03-14
  • 打赏
  • 举报
回复
THIS IS A PART FROM MSDN:(HAVE SAMPLE)
A tree control (CTreeCtrl) sends a notification when the user starts to drag an item. The control sends a TVN_BEGINDRAG notification message when the user begins dragging an item with the left mouse button and a TVN_BEGINRDRAG notification message when the user begins dragging with the right button. You can prevent a tree control from sending these notifications by giving the tree control the TVS_DISABLEDRAGDROP style.

You obtain an image to display during a drag operation by calling the CreateDragImage member function. The tree control creates a dragging bitmap based on the label of the item being dragged. Then the tree control creates an image list, adds the bitmap to it, and returns a pointer to the CImageList object.

You must provide the code that actually drags the item. This typically involves using the dragging capabilities of the image list functions and processing the WM_MOUSEMOVE and WM_LBUTTONUP (or WM_RBUTTONUP) messages sent after the drag operation has begun. For more information about the image list functions, see CImageList in the Class Library Reference and Image Lists in the Platform SDK. For more information about dragging a tree control item, see Dragging a Tree View Item, also in the Platform SDK.

If items in a tree control are to be the targets of a drag-and-drop operation, you need to know when the mouse cursor is on a target item. You can find out by calling the HitTest member function. You specify either a point and integer, or the address of a TVHITTESTINFO structure that contains the current coordinates of the mouse cursor. When the function returns, the integer or structure contains a flag indicating the location of the mouse cursor relative to the tree control. If the cursor is over an item in the tree control, the structure contains the handle of the item as well.

You can indicate that an item is the target of a drag-and-drop operation by calling the SetItem member function to set the state to the TVIS_DROPHILITED value. An item that has this state is drawn in the style used to indicate a drag-and-drop target.
netsky 2000-03-14
  • 打赏
  • 举报
回复
试试用ole来进行拖放。
929 2000-03-14
  • 打赏
  • 举报
回复
一般是处理WM_DROPFILES消息来完成。在WIN32 API帮助中有SDK的程序片段。
首先窗口用DragAcceptFiles指明自己将接受WM_DROPFILES消息。然后在WM_DROPFILES消息处理过程中对拖放进行处理。其中需要调用DragQueryFile来获得拖放文件的信息。可在帮助中再看看。
下面是API帮助中的程序片段.
For example, an application can call the DragAcceptFiles function when it starts and call a drag-drop function when it receives a WM_DROPFILES message, as shown in the following example.

case WM_CREATE:
DragAcceptFiles(hwnd, TRUE);
break;

case WM_DROPFILES:
DragFunc(hwnd, wParam); /* application-defined function */
break;

case WM_DESTROY:
DragAcceptFiles(hwnd, FALSE);
break;


The following example uses the DragQueryPoint function to determine where to begin to write text. The first call to the DragQueryFile function determines the number of dropped files. The loop writes the name of each file, beginning at the point returned by DragQueryPoint.

POINT pt;
WORD cFiles, a;
char lpszFile[80];

DragQueryPoint((HANDLE) wParam, &pt);

cFiles = DragQueryFile((HANDLE) wParam, 0xFFFF, (LPSTR) NULL, 0);
for(a = 0; a < cFiles; pt.y += 20, a++) {
DragQueryFile((HANDLE) wParam, a, lpszFile, sizeof(lpszFile));
TextOut(hdc, pt.x, pt.y, lpszFile, lstrlen(lpszFile));
}

DragFinish((HANDLE) wParam);

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧