[VB6/VB.NET] การแสดงกล่องโต้ตอบ (Dialog) ของ Windows เมื่อคัดลอกไฟล์ ด้วย Win32 API
http://www.g2gnet.com/webboard/images/vb6/api/apicopydialog.pngแอดมินรับอีเมล์ข่าวสารมาจาก www.developer.com มาเจอหัวข้อหนึ่งที่น่าสนใจ คือ Displaying the Windows Copy Dialog Box when Copying Files ซึ่งเป็นการคัดลอกไฟล์ขนาดใหญ่ แล้วให้มันแสดงผลไดอะลอค (หน้าต่างตอบโต้ผู้ใช้งาน) ของตัว Windows ขึ้นมา แต่ทว่าโค้ดมันเป็น VB.NET ดังนั้นแอดมินก็เลยทำการแปลงร่างมันให้กลายเป็น VB6 แทน เพราะโค้ดของ VB6 ค่อนข้างจะอ่านได้ง่ายกว่า ทั้งนี้ทั้งนั้นเราต้องเรียกใช้ Win32 API (Application Programming Interface) ซึ่งตัวนี้เรียกได้ว่าเป็นพลังของ VB6 ในการดึงขีดความสามารถของ Windows ออกมาใช้งานได้อย่างเต็มที่ ...
ข้อมูลเบื้องต้น มาทำความรู้จักกับ Application Programming Interface (API)
เรียกใช้งาน API Text Viewer (ตอนลงโปรแกรม Visual Studio อย่าไปตัดทิ้งซ่ะล่ะครับ)
http://www.g2gnet.com/webboard/images/vb6/api/apitextviewer.png
Load Text File เข้ามา ...
http://www.g2gnet.com/webboard/images/vb6/api/apiload.png
เลือก WIn32API.Text
http://www.g2gnet.com/webboard/images/vb6/api/apiloadtext.png
การเรียกใช้งานค่าคงที่ Constant (โดยเราพิมพ์เฉพาะคำสำคัญบางคำเท่านั้น)
http://www.g2gnet.com/webboard/images/vb6/api/apiconstant.png
การเรียกใช้งาน Type (เป็นพวกโครงสร้าง หรือ Structure ภายในของตัว Windows)
http://www.g2gnet.com/webboard/images/vb6/api/apitype.png
เรียกใช้งานการประกาศฟังค์ชั่น (Declares)
http://www.g2gnet.com/webboard/images/vb6/api/apideclare.png
มาดูโค้ดในส่วนของ VB6 ... (กรุณาแก้ไขการ Copy ไฟล์ด้วยล่ะครับ) ' / -----------------------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' /
' / Purpose: Displaying the Windows Copy Dialog Box when Copying Files.
' / Microsoft Visual Basic 6.0 Service Pack 6
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can feel free modify and/or distribute without to inform the developer.
' / -----------------------------------------------------------------------------------------------
Option Explicit
'// TYPE : Structure from Win32 API
'// https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/ns-shellapi-_shfileopstructa
Private Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As String '// Only used if FOF_SIMPLEPROGRESS
End Type
'// DECLARE : Call Win32 API
'// https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shfileoperationa
Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" ( _
lpFileOp As SHFILEOPSTRUCT _
) As Long
'// Instance Name for Structure.
Private SHFileOp As SHFILEOPSTRUCT
'// CONSTANT
'// Operation with constant from Win32 API.
Const FO_COPY = &H2
Const FO_DELETE = &H3
Const FO_MOVE = &H1
Const FO_RENAME = &H4
Public Sub CopyFiles(ByVal sSource As String, ByVal sTarget As String)
'// Define value with Structure.
With SHFileOp
.wFunc = FO_COPY '// Mode Copy
.pFrom = sSource '// Source
.pTo = sTarget '// Destination
End With
'// Operation with structure.
Call SHFileOperation(SHFileOp)
End Sub
Private Sub Command1_Click()
'// SAMPLE - Please be careful.
Call CopyFiles("C:\*.*", "C:\NewFolder")
End Sub
Private Sub Form_Unload(Cancel As Integer)
End
End Sub
มาดูโค้ดต้นฉบับของ VB.NET (2010)
' / -----------------------------------------------------------------------------------------------
' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
' / eMail : thongkorn@hotmail.com
' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
' / Facebook: https://www.facebook.com/commonindy (Worldwide)
' /
' / Purpose: Displaying the Windows Copy Dialog Box when Copying Files.
' / Microsoft Visual Basic .NET (2010)
' /
' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
' / You can feel free modify and/or distribute without to inform the developer.
' / -----------------------------------------------------------------------------------------------
Imports System.Runtime.InteropServices
Public Class frmCopyDialogNet
Private Enum FO_Func As Short
FO_COPY = &H2
FO_DELETE = &H3
FO_MOVE = &H1
FO_RENAME = &H4
End Enum
Private Structure SHFILEOPSTRUCT
Public hwnd As IntPtr
Public wFunc As FO_Func
<MarshalAs(UnmanagedType.LPWStr)>
Public pFrom As String
<MarshalAs(UnmanagedType.LPWStr)>
Public pTo As String
Public fFlags As UShort
Public fAnyOperationsAborted As Boolean
Public hNameMappings As IntPtr
<MarshalAs(UnmanagedType.LPWStr)>
Public lpszProgressTitle As String
End Structure
<DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
Private Shared Function SHFileOperation(
<()> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
End Function
Private Shared _ShFile As SHFILEOPSTRUCT
Public Shared Sub CopyFiles(ByVal sSource As String, ByVal sTarget As String)
Try
_ShFile.wFunc = FO_Func.FO_COPY
_ShFile.pFrom = sSource
_ShFile.pTo = sTarget
SHFileOperation(_ShFile)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
'// Call Function
CopyFiles("C:\*.*", "C:\NewFolder")
End Sub
Private Sub frmCopyDialogNet_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
Application.Exit()
End Sub
End Class
ดาวน์โหลดไฟล์ต้นฉบับทั้ง VB6 (ชื่อไฟล์ CopyDialog.zip) และ VB.NET (2010) ชื่อไฟล์ CopyDialogNet.zip ได้ที่นี่ ...
หน้า:
[1]