VB怎么把一个文件 复制到内存,是复制到内存里!!
假设 我点了一下 VB里的某个按钮( 或者按下了某个全局的 快捷键热键 ) 就可以实现把程序根目录下的 a.txt复制到内存
这样的话 无论后来打开到什么目录下,都可以很快的按CTRL+V 实现粘帖这个文件 很方便!!
因为我不知道我会在什么目录下粘帖 所以我需要按下按钮后复制到内存 而不是指定的目录
求高手指点
[此贴子已经被作者于2020-8-31 23:31编辑过]
Option Explicit Private Declare Function OpenClipboard Lib "user32" (ByVal hWnd As Long) As Long Private Declare Function EmptyClipboard Lib "user32" () As Long Private Declare Function CloseClipboard Lib "user32" () As Long Private Declare Function GlobalAlloc Lib "kernel32" (ByVal wFlags As Long, ByVal dwBytes As Long) As Long Private Declare Function GlobalLock Lib "kernel32" (ByVal hMem As Long) As Long Private Const GMEM_MOVEABLE = &H2 Private Const GMEM_ZEROINIT = &H40 Private Declare Function GlobalUnlock Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long Private Const CF_HDROP = &HF Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long) Private Type POINTAPI X As Long Y As Long End Type Private Type DROPFILES pFiles As Long pT As POINTAPI fNC As Long fWide As Long End Type Private Sub Command1_Click() Dim FileName As String FileName = "C:\a.txt" If Dir(FileName) = "" Then Exit Sub FileName = FileName & vbNullChar & vbNullChar Call FileToClipboard(FileName) End Sub Private Sub FileToClipboard(ByVal FileName As String) Dim dFiles As DROPFILES Dim hMem As Long, MemPtr As Long, Len1 As Long, Len2 As Long If OpenClipboard(Me.hWnd) = 0 Then Exit Sub Call EmptyClipboard Len1 = Len(dFiles) Len2 = LenB(StrConv(FileName, vbFromUnicode)) hMem = GlobalAlloc(GMEM_MOVEABLE Or GMEM_ZEROINIT, Len1 + Len2) MemPtr = GlobalLock(hMem) dFiles.pFiles = Len1 Call CopyMemory(ByVal MemPtr, dFiles, Len1) Call CopyMemory(ByVal (MemPtr + Len1), ByVal FileName, Len2) Call GlobalUnlock(hMem) Call SetClipboardData(CF_HDROP, hMem) Call CloseClipboard End Sub