'在窗口中添加一个Text1,属性MultiLine设为True(接受多行文本)
'代码如下:
- Option Explicit
- Private Declare Function MultiByteToWideChar Lib "kernel32 " (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpMultiByteStr As Long, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
- Private Const CP_UTF8 = 65001
- Private Sub Form_Load()
- Text1.OLEDropMode = 1
- End Sub
- Private Sub Text1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
- Dim I As Long, nStr As String
- If Data.GetFormat(vbCFText) Then nStr = Data.GetData(vbCFText)
- If Data.GetFormat(vbCFFiles) Then
- For I = 1 To Data.Files.Count
- nStr = nStr & Data.Files(I) & vbCrLf
- Next
- End If
- Text1.SelText = DecodeToBytes(StrConv(nStr, vbFromUnicode))
- End Sub
- Private Sub Text1_OLEDragOver(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single, State As Integer)
- If Data.GetFormat(vbCFText) Then Exit Sub
- If Data.GetFormat(vbCFFiles) Then Exit Sub
- Effect = 0
- End Sub
- ' UTF8 转字符
- Public Function DecodeToBytes(ByVal sData As String) As Byte()
- Dim aRetn() As Byte
- Dim nSize As Long
- nSize = MultiByteToWideChar(CP_UTF8, 0, StrPtr(sData), -1, 0, 0) - 1
- If nSize = 0 Then Exit Function
- ReDim aRetn(0 To 2 * nSize - 1) As Byte
- MultiByteToWideChar CP_UTF8, 0, StrPtr(sData), -1, VarPtr(aRetn(0)), nSize
- DecodeToBytes = aRetn
- Erase aRetn
- End Function