OLE Drag-Drop
Test
1. Aktifkan program Microsoft Visual Basic 6.0.
2. Bukalah kembali file project Latihan.vbp.
3. Tambahkan form baru ke dalam project.
4. Pada
Jendela Object buatlah User Interface seperti ini:
Setting
property object-nya adalah sebagai berikut:
Opject
|
Properties
|
Value
|
Form15
|
BorderStyle
Caption
|
1 – Fixed Single
OLE Drag-Drop Test
|
Text1
|
OLEDragMode
OLEDropMode
Text
|
1 – Automatic
2 – Automatic
<kosong>
|
Image1
|
OLEDragMode
OLEDropMode
Stretch
|
1 – Automatic
2 – Automatic
True
|
5.
Simpanlah Form15 dengan nama file Lat15.frm.
Catatan:
Anda bisa men-download file vb6-latihan.zip.
6.
Klik menu Project > Project1 Properties lalu klik tab General. Gantilah
Startup Object-nya menjadi
Form15.
7.
Coba jalankan project-nya dan aktifkan program WordPad:
1.
Buatlah teks dan
gambar
pada program
WordPad.
2.
Lakukan drag-drop teks
dari
WordPad ke Text1.
3.
Lakukan drag-drop
gambar
dari WordPad
ke
Image1.
Catatan:
-
Drag-drop tanpa
tombol
Ctrl 􀃆
teks/gambar
akan
dipindahkan.
-
Drag-drop dengan
tombol
Ctrl 􀃆
teks/gambar
akan
disalin.
8. Ubahlah property
object-object berikut ini:
Object
|
Properti
|
Value
|
Text1
|
OLEDragMode
|
0 – Manual
|
List1
|
OLEDropMode
|
1 – Manual
|
Image1
|
OLEDragMode
|
0 – Manual
|
Image2
|
OLEDropMode
Stretch
|
1 – Manual
True
|
9.
Buka Jendela Code, lalu ketikkan kode programnya seperti pada Listing-15.
10.
Coba jalankan kembali project-nya:
Keterangan:
2.
Lakukan drag-drop gambar dari Image1 ke
Image2.
Catatan:
-
Drag-drop tanpa tombol Ctrl 􀃆 teks/gambar
akan
dipindahkan.
-
Drag-drop dengan tombol Ctrl 􀃆
teks/gambar akan
disalin.
11.
Aktifkan program Windows Explorer:
1.
Lakukan drag-drop file dari
program
Windows Explorer ke
List1.
2.
Lakukan drag-drop file dari
program
Windows Explorer ke
Image2.
Drag-drop ke
List1 bisa lebih dari 1
file
(pada program Windows
Ctrl+klik
untuk memilih lebih dari 1
file).
Listing-15
Dim FormatOK As
Boolean
'======================
'OLE Drag-Drop
ke List1
'======================
Private Sub
Text1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y
As Single)
'Jika Text1 tdk
kosong dan tombol kiri mouse ditekan
If Text1.Text
<> "" And Button = vbLeftButton Then
Text1.OLEDrag
'Mulai operasi OLE drag-drop
End If
End Sub
Private Sub
Text1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
'Simpan data
dgn format text
Data.SetData
Text1.Text, vbCFText
'Atur efek OLE
drag-drop yg diperbolehkan
AllowedEffects
= vbDropEffectCopy Or vbDropEffectMove
End Sub
Private Sub
List1_OLEDragOver(Data As DataObject, Effect As Long, _
Button As
Integer, Shift As Integer, _
X As Single, Y
As Single, State As Integer)
'Cek format
datanya
FormatOK =
Data.GetFormat(vbCFText) Or _
Data.GetFormat(vbCFFiles)
If FormatOK
Then
'Cek apakah
tombol Ctrl ditekan
If Shift =
vbCtrlMask Then
Effect =
vbDropEffectCopy 'Data disalin
Else
Effect =
vbDropEffectMove 'Data dipindahkan
End If
Else
Effect =
vbDropEffectNone 'Data diabaikan
End If
End Sub
Private Sub
List1_OLEDragDrop(Data As DataObject, Effect As Long, _
Button As
Integer, Shift As Integer, _
X As Single, Y
As Single)
Dim i As
Integer
'Cek format
datanya
FormatOK =
Data.GetFormat(vbCFText) Or _
Data.GetFormat(vbCFFiles)
If FormatOK
Then
'Cek apakah
tombol Ctrl ditekan
If Shift =
vbCtrlMask Then
Effect =
vbDropEffectCopy 'Data disalin
Else
Effect =
vbDropEffectMove 'Data dipindahkan
End If
'Tambahkan data
yg tersimpan ke dalam List1
If
Data.GetFormat(vbCFText) Then
List1.AddItem
Data.GetData(vbCFText) 'Data text
End If
If
Data.GetFormat(vbCFFiles) Then
List1.Clear
For i = 1 To
Data.Files.Count
List1.AddItem
Data.Files(i) 'Data file(s)
Next i
End If
Else
Effect =
vbDropEffectNone 'Data diabaikan
End If
End Sub
Private Sub
Text1_OLECompleteDrag(Effect As Long)
If Effect =
vbDropEffectMove Then
Text1.Text =
"" 'Kosongkan Text1 jika data dipindahkan
End If
End Sub
'=======================
'OLE Drag-Drop
ke Image2
'=======================
Private Sub
Image1_MouseDown(Button As Integer, Shift As Integer, _
X As Single, Y
As Single)
If
Image1.Picture <> 0 And Button = vbLeftButton Then
Image1.OLEDrag
End If
End Sub
Private Sub
Image1_OLEStartDrag(Data As DataObject, AllowedEffects As Long)
Data.SetData
Image1.Picture
AllowedEffects
= vbDropEffectCopy Or vbDropEffectMove
End Sub
Private Sub
Image2_OLEDragOver(Data As DataObject, Effect As Long, _
Button As
Integer, Shift As Integer, _
X As Single, Y
As Single, State As Integer)
FormatOK =
Data.GetFormat(vbCFBitmap) Or _
Data.GetFormat(vbCFMetafile)
Or _
Data.GetFormat(vbCFDIB)
Or _
Data.GetFormat(vbCFFiles)
If FormatOK
Then
If Shift =
vbCtrlMask Then
Effect =
vbDropEffectCopy
Else
Effect =
vbDropEffectMove
End If
Else
Effect =
vbDropEffectNone
End If
End Sub
Private Sub Image2_OLEDragDrop(Data
As DataObject, Effect As Long, _
Button As
Integer, Shift As Integer, _
X As Single, Y
As Single)
FormatOK =
Data.GetFormat(vbCFBitmap) Or _
Data.GetFormat(vbCFMetafile)
Or _
Data.GetFormat(vbCFDIB)
Or _
Data.GetFormat(vbCFFiles)
If FormatOK
Then
If Shift =
vbCtrlMask Then
Effect =
vbDropEffectCopy
Else
Effect =
vbDropEffectMove
End If
If
Data.GetFormat(vbCFBitmap) Then
Image2.Picture
= Data.GetData(vbCFBitmap)
End If
If
Data.GetFormat(vbCFMetafile) Then
Image2.Picture
= Data.GetData(vbCFMetafile)
End If
If
Data.GetFormat(vbCFDIB) Then
Image2.Picture
= Data.GetData(vbCFDIB)
End If
If
Data.GetFormat(vbCFFiles) Then
Image2.Picture
= LoadPicture(Data.Files(1))
End If
Else
Effect =
vbDropEffectNone
End If
End Sub
Private Sub
Image1_OLECompleteDrag(Effect As Long)
If Effect =
vbDropEffectMove Then
Image1.Picture
= Nothing
End If
End Sub
Tidak ada komentar:
Posting Komentar