ชุมชนคนรักภาษาเบสิค - Visual Basic Community

 ลืมรหัสผ่าน
 ลงทะเบียน
ค้นหา
ดู: 3074|ตอบกลับ: 0

[VB6/VB.NET] การแสดงกล่องโต้ตอบ (Dialog) ของ Windows เมื่อคัดลอกไฟล์ ด้วย Win32 API

[คัดลอกลิงก์]

310

กระทู้

501

โพสต์

6041

เครดิต

ผู้ดูแลระบบ

ทองก้อน ทับทิมกรอบ

Rank: 9Rank: 9Rank: 9

เครดิต
6041



แอดมินรับอีเมล์ข่าวสารมาจาก 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 อย่าไปตัดทิ้งซ่ะล่ะครับ)


Load Text File เข้ามา ...


เลือก WIn32API.Text


การเรียกใช้งานค่าคงที่ Constant (โดยเราพิมพ์เฉพาะคำสำคัญบางคำเท่านั้น)


การเรียกใช้งาน Type (เป็นพวกโครงสร้าง หรือ Structure ภายในของตัว Windows)


เรียกใช้งานการประกาศฟังค์ชั่น (Declares)


มาดูโค้ดในส่วนของ VB6 ... (กรุณาแก้ไขการ Copy ไฟล์ด้วยล่ะครับ)
  1. ' / -----------------------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' /
  8. ' / Purpose: Displaying the Windows Copy Dialog Box when Copying Files.
  9. ' / Microsoft Visual Basic 6.0 Service Pack 6
  10. ' /
  11. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  12. ' / You can feel free modify and/or distribute without to inform the developer.
  13. ' / -----------------------------------------------------------------------------------------------

  14. Option Explicit

  15. '// TYPE : Structure from Win32 API
  16. '// https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/ns-shellapi-_shfileopstructa
  17. Private Type SHFILEOPSTRUCT
  18.     hwnd As Long
  19.     wFunc As Long
  20.     pFrom As String
  21.     pTo As String
  22.     fFlags As Integer
  23.     fAnyOperationsAborted As Long
  24.     hNameMappings As Long
  25.     lpszProgressTitle As String '// Only used if FOF_SIMPLEPROGRESS
  26. End Type

  27. '// DECLARE : Call Win32 API
  28. '// https://docs.microsoft.com/en-us/windows/desktop/api/shellapi/nf-shellapi-shfileoperationa
  29. Private Declare Function SHFileOperation Lib "shell32" Alias "SHFileOperationA" ( _
  30.     lpFileOp As SHFILEOPSTRUCT _
  31.     ) As Long
  32.    
  33. '// Instance Name for Structure.
  34. Private SHFileOp As SHFILEOPSTRUCT

  35. '// CONSTANT
  36. '// Operation with constant from Win32 API.
  37. Const FO_COPY = &H2
  38. Const FO_DELETE = &H3
  39. Const FO_MOVE = &H1
  40. Const FO_RENAME = &H4
  41.    
  42. Public Sub CopyFiles(ByVal sSource As String, ByVal sTarget As String)
  43.     '// Define value with Structure.
  44.     With SHFileOp
  45.         .wFunc = FO_COPY    '// Mode Copy
  46.         .pFrom = sSource        '// Source
  47.         .pTo = sTarget              '// Destination
  48.     End With
  49.     '// Operation with structure.
  50.     Call SHFileOperation(SHFileOp)
  51. End Sub

  52. Private Sub Command1_Click()
  53.     '// SAMPLE - Please be careful.
  54.     Call CopyFiles("C:\*.*", "C:\NewFolder")
  55. End Sub

  56. Private Sub Form_Unload(Cancel As Integer)
  57.     End
  58. End Sub
คัดลอกไปที่คลิปบอร์ด

มาดูโค้ดต้นฉบับของ VB.NET (2010)
  1. ' / -----------------------------------------------------------------------------------------------
  2. ' / Developer : Mr.Surapon Yodsanga (Thongkorn Tubtimkrob)
  3. ' / eMail : thongkorn@hotmail.com
  4. ' / URL: http://www.g2gnet.com (Khon Kaen - Thailand)
  5. ' / Facebook: https://www.facebook.com/g2gnet (For Thailand only)
  6. ' / Facebook: https://www.facebook.com/commonindy (Worldwide)
  7. ' /
  8. ' / Purpose: Displaying the Windows Copy Dialog Box when Copying Files.
  9. ' / Microsoft Visual Basic .NET (2010)
  10. ' /
  11. ' / This is open source code under @Copyleft by Thongkorn Tubtimkrob.
  12. ' / You can feel free modify and/or distribute without to inform the developer.
  13. ' / -----------------------------------------------------------------------------------------------
  14. Imports System.Runtime.InteropServices

  15. Public Class frmCopyDialogNet
  16.     Private Enum FO_Func As Short
  17.         FO_COPY = &H2
  18.         FO_DELETE = &H3
  19.         FO_MOVE = &H1
  20.         FO_RENAME = &H4
  21.     End Enum

  22.     Private Structure SHFILEOPSTRUCT
  23.         Public hwnd As IntPtr
  24.         Public wFunc As FO_Func
  25.         <MarshalAs(UnmanagedType.LPWStr)>
  26.         Public pFrom As String
  27.         <MarshalAs(UnmanagedType.LPWStr)>
  28.         Public pTo As String
  29.         Public fFlags As UShort
  30.         Public fAnyOperationsAborted As Boolean
  31.         Public hNameMappings As IntPtr
  32.         <MarshalAs(UnmanagedType.LPWStr)>
  33.         Public lpszProgressTitle As String
  34.     End Structure

  35.     <DllImport("shell32.dll", CharSet:=CharSet.Unicode)>
  36.     Private Shared Function SHFileOperation(
  37.       <[In]()> ByRef lpFileOp As SHFILEOPSTRUCT) As Integer
  38.     End Function

  39.     Private Shared _ShFile As SHFILEOPSTRUCT

  40.     Public Shared Sub CopyFiles(ByVal sSource As String, ByVal sTarget As String)
  41.         Try
  42.             _ShFile.wFunc = FO_Func.FO_COPY
  43.             _ShFile.pFrom = sSource
  44.             _ShFile.pTo = sTarget
  45.             SHFileOperation(_ShFile)
  46.         Catch ex As Exception
  47.             MessageBox.Show(ex.Message)
  48.         End Try
  49.     End Sub

  50.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  51.         '// Call Function
  52.         CopyFiles("C:\*.*", "C:\NewFolder")
  53.     End Sub

  54.     Private Sub frmCopyDialogNet_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
  55.         Me.Dispose()
  56.         Application.Exit()
  57.     End Sub
  58. End Class
คัดลอกไปที่คลิปบอร์ด

ดาวน์โหลดไฟล์ต้นฉบับทั้ง VB6 (ชื่อไฟล์ CopyDialog.zip) และ VB.NET (2010) ชื่อไฟล์ CopyDialogNet.zip ได้ที่นี่ ...

ขออภัย! โพสต์นี้มีไฟล์แนบหรือรูปภาพที่ไม่ได้รับอนุญาตให้คุณเข้าถึง

คุณจำเป็นต้อง ลงชื่อเข้าใช้ เพื่อดาวน์โหลดหรือดูไฟล์แนบนี้ คุณยังไม่มีบัญชีใช่ไหม? ลงทะเบียน

x
สิ่งที่ดีกว่าการให้ คือการให้แบบไม่มีที่สิ้นสุด
ขออภัย! คุณไม่ได้รับสิทธิ์ในการดำเนินการในส่วนนี้ กรุณาเลือกอย่างใดอย่างหนึ่ง ลงชื่อเข้าใช้ | ลงทะเบียน

รายละเอียดเครดิต

ข้อความล้วน|อุปกรณ์พกพา|ประวัติการแบน|G2GNet.com  

GMT+7, 2024-4-23 19:59 , Processed in 0.270537 second(s), 4 queries , File On.

Powered by Discuz! X3.4, Rev.62

Copyright © 2001-2020 Tencent Cloud.

ตอบกระทู้ ขึ้นไปด้านบน ไปที่หน้ารายการกระทู้