重拾VB621: OLE Drag and Drop

来自MSDN-2001-OCT: Visual Tools and Languages/Visual Studio 6.0 Documentation/Visual Basic Documentation/Using Visual Basic/Programmer’s Guide/Part 2: What Can You Do With Visual Basic/Responding to Mouse and Keyboard Events/

1. Concepts for OLE Drag and Drop

(1) With OLE drag and drop,you’re not dragging one control to another control to invoke some code (as with the drag and drop discussed earlier in this chapter); you’re moving data from one control or application to another control or application.

(2) A control that does not have automatic support for OLE drag will not have the OLEDragMode property,but it will have an OLEDrag method if it supports OLE drag through code.

(3) Forms,MDI forms,Document Objects,User Controls,and Property Pages contain the OLEDropMode property and provide support for manual dragging and dropping only.

(4) The source and the target may be in different applications,in the same application,or even in the same control.

(5) When dragging data,you may notice that the mouse pointer indicates if the object that it is passing over supports OLE drag and drop for the type of data that you are dragging. If the object supports OLE drag and drop for the type of data,the “drop” pointer is displayed. If the object does not,a "no drop" pointer is displayed.

2. The OLE Drag and Drop DataObject Object

(1) It is the DataObject object by which data is moved from the source to the target.

(2) The SetData,GetData,and GetFormat methods use the data and format arguments to return either the type of data in the DataObject object or to retrieve the data itself if the format is compatible with the target. For example:

Private Sub txtSource_OLEStartDrag( Data As _
VB . DataObject , AllowedEffects As Long)
Data . SetData txtSource . SelText , vbCFText
End Sub

(3) The Clear method is used to clear the content of the DataObject object on the source side when the OLEStartDrag event is triggered. When data from a control is dragged in an automatic drag operation,its data formats are placed into the DataObject object before the OLEStartDrag event is triggered. If you don’t want to use the default formats,you use the Clear method. If you want to add to the default data formats,you do not use the Clear method.

(4) When data is dropped onto the target and no format has been specified,Visual Basic is able to detect if it is a bitmap,metafile,enhanced metafile,or text. All other formats must be specified explicitly or an error will be generated.

(5) You should use the vbCFDIB data format instead of vbCFBitmap and vbCFPalette,in most cases. The vbCFDIB format contains both the bitmap and palette and is therefore the preferred method of transferring a bitmap image. You can,however,also specify the vbCFBitmap and vbCFPalette for completeness. If you chose not to use the vbCFDIB format,you must specify both the vbCFBitmap and vbCFPalette formats so that the bitmap and the palette are correctly placed into the DataObject object.

3. How OLE Drag and Drop Works

(1)

(2) When the user drags data from an OLE drag source (a text box control,for example) by selecting and then holding down the left mouse button,the OLEStartDrag event is triggered.

(3) As the user drags over the target,the target’s OLEDragOver event is triggered,indicating that the source is within its boundaries.You then specify what the target would do if the data were dropped there — either copy,move,or refuse the data. By convention,the default is usually move,but it may be copy.

(4) When the target specifies which drop effect will be performed if the source is dropped there,the OLEGiveFeedback event is triggered. The OLEGiveFeedback event is used to provide visual feedback to the user on what action will be taken when the selection is dropped — i.e.,the mouse pointer will be changed to indicate a copy,or "no drop" action.

(5) When the user drops the source onto the target,the target’s OLEDragDrop event is triggered. The target queries the source for the format of the data it contains (or supports,if the data wasn’t placed into the source when the drag was started) and then either retrieves or rejects the data.

If the data was stored when the drag started,the target retrieves the data by using the GetData method. If the data wasn’t stored when the drag started,the data is retrieved by triggering the source’s OLESetData event and then using the SetData method.

(6) When the data is accepted or rejected,the OLECompleteDrag event is triggered and the source can then take the appropriate action: if the data is accepted and a move is specified,the source deletes the data,for example.

4. Starting the OLE Drag Operation

(1) You use the OLEDrag method to manually start the drag operation and the OLEStartDrag event to specify the allowed drop-action effects and the supported data formats.

(2) Generally,the OLEDrag method is called from an object’s MouseMove event when data has been selected,the left mouse button is pressed and held,and the mouse is moved.

The OLEDrag method does not provide any arguments. Its primary purpose is to initiate a manual drag and then allow the OLEStartDrag event to set the conditions of the drag operation

(3) The OLEStartDrag event uses two arguments to specify supported data formats and whether the data can be copied or moved when the data is dropped (drop effects).

Note If no drop effects or data formats are specified in the OLEStartDrag event,the manual drag will not be started.

Private Sub rtbSource_OLEStartDrag( Data As _
VB . DataObject , AllowedEffects As Long)
AllowedEffects = vbDropEffectMove Or _
vbDropEffectCopy

Data . SetData , vbCFText
Data . SetData , vbCFRTF
End Sub

(4) Placing Data into the DataObject object:

AllowedEffects As Long)
Data . Clear
Data . SetData txtSource . SelText , vbCFText
End Sub

5. Dragging the OLE Drag Source over the OLE Drop Target

(1)

(2) Notice that the OLEDragOver event is generated several times a second,even when the mouse is stationary.

The state argument of the OLEDragOver event specifies when the data enters,passes over,and leaves the target control by using the following constants:vbEnter,vbLeave,vbOver

(3) When the source data is dragged over the target,and the OLEDragOver event is triggered,the source tells the target which effects it allows (move,copy,no drop). You must then chose which single effect will occur if the data is dropped. The effect argument of the OLEDragOver event informs the source which drop action it supports,and the source then informs the user by using the OLEGiveFeedback event to modify the mouse pointer.

(4)

Private Sub TxtSource_OLEGiveFeedback( Effect As Long , _
DefaultCursors As Boolean)
DefaultCursors = False
If Effect = vbDropEffectNone Then
Screen . MousePointer = vbNoDrop
ElseIf Effect = vbDropEffectCopy Then
Screen . MousePointer = vbCustom
Screen . MouseIcon = LoadPicture( "c:/copy.ico")
ElseIf Effect = ( vbDropEffectCopy Or _
vbDropEffectScroll) Then
Screen . MousePointer = vbCustom
Screen . MouseIcon = _
LoadPicture( "c:/copyscrl.ico")
ElseIf Effect = vbDropEffectMove Then
Screen . MousePointer = vbCustom
Screen . MouseIcon = LoadPicture( "c:/move.ico")
ElseIf Effect = ( vbDropEffectMove Or _
vbDropEffectScroll) Then
Screen . MousePointer = vbCustom
Screen . MouseIcon = _
LoadPicture( "c:/movescrl.ico")
Else
' If some new format is added that we do not
' understand,allow OLE to handle it with
' correct defaults.
DefaultCursors = True
End If
End Sub

Note You should always reset the mouse pointer in the OLECompleteDrag event if you specify a custom mouse pointer in the OLEGiveFeedback event.

6. Dropping the OLE Drag Source onto the OLE Drop Target

(1) The OLEDragDrop event is triggered when the user drops the source onto the target. If data was placed into the DataObject object when the drag operation was initiated,it can be retrieved when the OLEDragDrop event is triggered,by using the GetData method. If,only the supported source formats were declared when the drag operation was initiated,then the GetData method will automatically trigger the OLESetData event on the source to place the data into,and then retrieve the data from,the DataObject object.

(2)

Private Sub txtTarget_OLEDragDrop( Data As _
VB . DataObject , Effect As Long , Button As _
Integer , Shift As Integer , X As Single , _
Y As Single)
If Data . GetFormat( vbCFText) Then
txtTarget . Text = Data . GetData( vbCFText)
End If
End Sub

(3) When the target uses the GetData method to retrieve data from the source,the OLESetData event is only triggered if the data was not placed into the source when the drag operation was initiated.

Private Sub txtSource_OLESetData( Data As _
VB . DataObject , DataFormat As Integer)
If DataFormat = vbCFText Then
Data . SetData txtSource . SelText , vbCfText
End If
End Sub

(4) On the source side,the OLECompleteDrag event is triggered when the source is dropped onto the target,or when the OLE drag-and-drop operation is canceled. The OLECompleteDrag event contains only one argument (effect),The effect argument returns the same values that are used by the effect argument of the other OLE drag-and-drop events.

By setting this argument after a move has been specified by the target and the source has been dropped into the target,for example,the source will delete the original data in the control. You should also use the OLECompleteDrag event to reset the mouse pointer if you specified a custom mouse pointer in the OLEGiveFeedback event:

Private Sub txtSource_OLECompleteDrag( Effect As Long)
If Effect = vbDropEffectMove Then
txtSource . SelText = ""
End If
Screen . MousePointer = vbDefault
End Sub

7. Using the Mouse and Keyboard to Modify Drop Effects and User Feedback

(1)

_
Y As Single)
If Shift And vbCtrlMask Then
txtTarget . Text = Data . GetData( vbCFText)
Effect = vbDropEffectCopy
Else
txtTarget . Text = Data . GetData( vbCFText)
Effect = vbDropEffectMove
End If
End Sub

(2)

Private Sub txtTarget_OLEDragOver( Data As _
VB . DataObject , _
Y As Single , State As Integer)
If Shift And vbCtrlMask Then
Effect = vbDropEffectCopy
Else
Effect = vbDropEffectMove
End If
End Sub

8. Creating a Custom Data Format

(1) If the formats supplied in Visual Basic are insufficient for some specific purpose,you can create a custom data format for use in an OLE drag-and-drop operation.

(2) To create a custom data format,you have to call the Windows API RegisterClipboardFormat function. For example:

Private Declare Function RegisterClipboardFormat Lib _
"user32.dll" Alias "RegisterClipboardFormatA" _
( ByVal lpszFormat$) As Integer
Dim MyFormat As Integer

(3) To use this functionality,you have to place data into and retrieve data from the DataObject object as a Byte array. You can then assign your custom data format to a string variable because it is automatically converted. For example:

Dim a() As Byte
a = Data . GetData( MyFormat)

(4) Because Visual Basic doesn’t understand your custom data format (because you defined it),it doesn’t have a way to determine the size of the data. Visual Basic can determine the memory size of the Byte array because it has been allocated by Windows,but the operating system usually assigns more memory than is needed.

Therefore,when you retrieve a custom data format,you get back a Byte array containing at least,and possibly more than,the number of bytes that the source actually placed into the DataObject object. You must then correctly interpret your custom data format when it is retrieved from the DataObject object. For example,in a simple string,you have to search for the NULL character and then truncate the string to that length.

9. Dragging Files From the Windows Explorer

(1) You can use OLE drag-and-drop to drag files from the Windows Explorer into an appropriate Visual Basic control,or vice versa.

(2) To do this,use the Files property and the vbCFFiles data format of the DataObject object.

重拾VB6目录

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...