ole drag drop and problem with unicode charachters or load image with unicode
hi i set oledragmode of form to manual and then i draged attached file on form,and then i want load it like loadpicture but form can not known name of file.
any way to can fix it, i want use in ole drag drop and load image or use in common dialog too.
Code:
Private Sub Form_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
End Sub
in this event data.files(1) return name like "C:\Users\barnameha\Desktop\type\d?.jpg"
how can fix it?
Last edited by Black_Storm; Feb 14th, 2018 at 03:03 AM.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Hi Black_Storm,
I'm not positive (as I didn't test for you), but it seems that LaVolpe's Clipboard/DataObject Extension Class might get you out of trouble. Possibly just "Attach" the passed DataObject to the class and then use its methods to get the files you're interested in.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Originally Posted by Elroy
Hi Black_Storm,
I'm not positive (as I didn't test for you), but it seems that LaVolpe's Clipboard/DataObject Extension Class might get you out of trouble. Possibly just "Attach" the passed DataObject to the class and then use its methods to get the files you're interested in.
Good Luck,
Elroy
hi thanks,but problem not solved yet,can help more?
i used attached in #1 and another 2 images ( see in image result) but not worked.
result :
Last edited by Black_Storm; Feb 14th, 2018 at 01:57 PM.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Hi Black_Storm,
LaVolpe is around often. If he's so inclined, maybe he can help you with his class. Not having actually used it, I feel like I'm the wrong person to throw myself into this one.
Best Of Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Originally Posted by Elroy
Hi Black_Storm,
LaVolpe is around often. If he's so inclined, maybe he can help you with his class. Not having actually used it, I feel like I'm the wrong person to throw myself into this one.
Best Of Luck,
Elroy
@Black_Storm. A VB message box is not going display unicode correctly. And if you are using VB functions for writing files, you are not going to create files with unicode characters. May want to consider using APIs for reading/writing those files instead.
Insomnia is just a byproduct of, "It can't be done"
Re: ole drag drop and problem with unicode charachters or load image with unicode
Ahh, we're now talking about reading/writing a Unicode file from disk?
Black_Storm, I believe LaVolpe is talking about reading the file entirely with API calls, and that's certainly a good alternative (relying on Windows to sort everything out for you).
But, if we're just trying to read a relatively standard (and not hugely large) Unicode file, here are some procedures I occasionally use (no API at all).
Code:
Option Explicit
Public Function bFileIsUnicode(sFileSpec As String) As Boolean
Dim iFle As Long
Dim i As Integer
'
iFle = FreeFile
Open sFileSpec For Binary As iFle
Get iFle, , i
Close iFle
bFileIsUnicode = (i = &HFEFF)
End Function
Public Sub SaveStringToUnicodeFile(sData As String, sFileSpec As String)
' These are typically .TXT files. They can be read with notepad.
Dim iFle As Long
'
iFle = FreeFile
Open sFileSpec For Binary As iFle
Put iFle, , &HFEFF ' This is the Unicode header to a text file. First byte = FF, second byte = FE.
Put iFle, , UnicodeByteArrayFromString(sData)
Close iFle
End Sub
Public Function LoadStringFromUnicodeFile(sFileSpec As String) As String
' These are typically .TXT files. They can be read with notepad.
Dim iFle As Long
Dim bb() As Byte
Dim i As Integer
'
iFle = FreeFile
Open sFileSpec For Binary As iFle
Get iFle, , i
If i <> &HFEFF Then ' Unicode file header. First byte = FF, second byte = FE.
Close iFle
Exit Function ' It's not a valid Unicode file.
End If
ReDim bb(1 To LOF(iFle) - 2&)
Get iFle, , bb
Close iFle
LoadStringFromUnicodeFile = bb ' This directly copies the byte array to the Unicode string (no conversion).
' Note: If you try to directly read the file as a string, VB6 will attempt to convert the string from ASCII to Unicode.
End Function
Public Function UnicodeByteArrayFromString(s As String) As Byte()
' This directly copies the Unicode string into the byte array, using two bytes per character (i.e., Unicode).
UnicodeByteArrayFromString = s
End Function
You will need to be a bit careful though. I believe you may occasionally run across a Unicode (UCS-2) file that doesn't have the &HFEFF header on it. In those cases, my procedures won't work.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: ole drag drop and problem with unicode charachters or load image with unicode
There seems to be a misunderstanding on Posting #1. As I see it, the loading failure is merely owing to a Unicode file name, i.e. if you change the file name to English, then the loading would be okay (assuming Black_Storm's VB drag/drop code is okay).
The image file itself is perfectly okay, I have tested the JPG file in the ZIP (with an English name only, otherwise I would have a better test). I could drop the file to invoke a paint program, drop it to a blank MDI surface, to a Child form and to a PictureBox, all okay. From the letterings shown on the displayed image, the language that Black_Storm is talking about is Iranian -- my wild guess only.
So, if Lavolpe can give Black_Storm some Unicode stuff to enable his code to recognize the file name, e.g. in
Re: ole drag drop and problem with unicode charachters or load image with unicode
Black_Storm,
The JPG file in your ZIP is with an English name. You got the "?" problem because you were using a non-English file name? If the original file is with an Iranian file name (or whatever a non-English name), would you please ZIP and upload that original file?
Re: ole drag drop and problem with unicode charachters or load image with unicode
i created a product for a company to work for open images and show in picturebox and show name it on a label or ....
thic company have more than 1000 images like attached in #1,so i can no tell to them (please rename to english or ....) i want fix this problem without reanem to english.
can u send a simple project to can drag attached image in #1 on form ( use ole drag) and and a button for use commonn dialog and can show in a picturebox and show name of file in a lable too?
my porblem is about :
1-i can not drag and drop or use commong dialog for open attached file in #1 on form and show it on picture box . can send a sample for fix problem?
2-i can not show name of it on label(???? name problem) - can send a sample for fix problem?
Re: ole drag drop and problem with unicode charachters or load image with unicode
Originally Posted by Black_Storm
i created a product for a company to work for open images and show in picturebox and show name it on a label or...
If you are going to create a product for a non-English based system, you should start to learn and understand unicode and how to make your project compatible.
There are plenty of projects in the codebank to include substitutes for the common dialog, labels, and more.
Insomnia is just a byproduct of, "It can't be done"
Re: ole drag drop and problem with unicode charachters or load image with unicode
My request for the original file with a non-English name had been completely ignored, so I am out of this thread. (My intention was to test out an Iranian file name before offering my small project which I happen to have - about 5 years ago I got a bundle of photos, some of them with non-English names, so I created the said small project. Because I was not sure whether it would also be the same good on an Iranian file name, I though I better had a test first.)
Before leaving, I would like to mention two relevant points, for the benefit of the thread initiator:
(1) Nowadays many photos have an ICC color profile; a good project should be able to present ICC-converted images to viewers.
(2) By a great majority, photos are taken at Orientation 1; however, there may be some with Orientation 2 to 8. A good project dealing with JPG files should be able to provide an auto-orientation of photos (like Windows 10 does in File Explorer). Failing that, then buttons should be provided for users to manually rotate on the spot.
Re: ole drag drop and problem with unicode charachters or load image with unicode
i fixed problem by this thread (#8) but problem 2 not fixed yet
i created this label support unicode (attached) but i think this is so complicated,any body can send a simple label user control or better way for show uncode charachters on a label(transparent is matter for me)
Last edited by Black_Storm; Feb 16th, 2018 at 03:41 AM.
Reason: better edit
Re: ole drag drop and problem with unicode charachters or load image with unicode
Here's an RTF label (and button) I use often. I don't use them in multi-lingual situations, but I do use them to "get at" many of the special characters of Unicode that you can't get at via a regular label. Maybe they'll do what you're trying to do. Basically, they're just piggy-backing on the RTB control.
Alternatively, you could just directly use the RTB as a label. That'd also probably work for you, but you would have to load it with Unicode at runtime. The nicety of mine is that it's got a little IDE editor with it.
Good Luck,
Elroy
EDIT1: I've never used it, but the InkEdit control is also suppose to have some nice features.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
Re: ole drag drop and problem with unicode charachters or load image with unicode
1. Use this to convert longpath (unicode) to a proper DosPath (it is in a unicode string but letters are in system codepage, as a direct conversion of a DosPath to unicode, so VB convert it to Ansi when you work with Open).
Code:
Declare Function GetShortPathName Lib "KERNEL32" Alias _
"GetShortPathNameW" (ByVal lpszLongPath As Long, _
ByVal lpszShortPath As Long, ByVal cchBuffer As Long) As Long
Public Function GetDosPath(LongPath As String) As String
Dim s As String
Dim i As Long
Dim PathLength As Long
i = Len(LongPath) * 2 + 2
s = String(1024, 0)
PathLength = GetShortPathName(StrPtr(LongPath), StrPtr(s), i)
GetDosPath = Left$(s, PathLength)
End Function
So you can use dospath as a filename to open it.
2. You need a way to draw on picture dc using hdc, and a RECT with proper values (values are in pixels).
Code:
Private Const DT_NOPREFIX As Long = &H800&
Private Const DT_SINGLELINE As Long = &H20&
Private Const DT_NOCLIP As Long = &H100&
Private Const DT_VCENTER As Long = &H4&
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Declare Function DrawText Lib "User32" Alias "DrawTextW" (ByVal hDC As Long, ByVal lpStr As Long, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Sub PrintItem(mHdc As Long, c As String, r As RECT, Optional way As Long = DT_SINGLELINE Or DT_NOPREFIX Or DT_NOCLIP Or DT_VCENTER)
DrawText mHdc, StrPtr(c), -1, r, way
End Sub
3. If you need to create a file with full unicode filename you need something else. You have to make a file in another way, and then you get the DosPath to append data.
Code:
Public Declare Sub Sleep Lib "KERNEL32" (ByVal dwMilliseconds As Long)
Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileW" (ByVal lpFileName As Long, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As Any, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Const GENERIC_WRITE = &H40000000
Public Const FILE_ATTRIBUTE_NORMAL = &H80
Declare Function FlushFileBuffers Lib "KERNEL32" (ByVal hFile As Long) As Long
Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Public Function NeoUnicodeFile(FileName$) As Boolean
Dim hFile, counter
Dim f$, f1$
Sleep 10
If Left$(FileName$, 2) <> "\\" Then
f$ = "\\?\" + FileName$
Else
f$ = FileName$
End If
On Error Resume Next
f1$ = Dir(f$) '' THIS IS THEWORKAROUND FOR THE PROBLEMATIC CREATIFILE (I GOT SOME HANGS)
hFile = CreateFile(StrPtr(f$), GENERIC_WRITE, ByVal 0, ByVal 0, 2, FILE_ATTRIBUTE_NORMAL, ByVal 0)
FlushFileBuffers hFile
Sleep 10
CloseHandle hFile
NeoUnicodeFile = GetDosPath(f$)<> ""
Sleep 10
'need "\\?\" before
'now we can use the getdospath from normal Open File
End Function
Last edited by georgekar; Feb 16th, 2018 at 09:28 AM.
Re: ole drag drop and problem with unicode charachters or load image with unicode
how can get data of file droped like as string?
its matter for me to string data because i need post this data like as string with xml.
for examle i want keep data of (تصویر.exe or متن.png or نمونه تصویر.pdf) in a string and after my processed then save it as orginal format ( exe or png or pdf).
Re: ole drag drop and problem with unicode charachters or load image with unicode
It doesn't seem the Data.Files object in OLEDragDrop supports Unicode.. it doesn't even return it, so can't send it for a conversion. I passed it straight to a Unicode messagebox to make sure (MessageBoxW StrPtr(Data.Files(0))), and question marks.
There's a couple ways to proceed. Of course my favorite is updating your whole drag/drop method to the fancy new one with IDropTarget that shows file icons when you drag over, among other things.
But the least disruptive is to just drop in a technique to get the Unicode filenames by converting the DataObject into an IDataObject. You will need a typelib with IDataObject defined, such as any version of mine or the original olelib, or really any as long as it's got IDataObject... Any Windows version is fine; it will work on XP... after that:
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function DragQueryFileW Lib "shell32.dll" (ByVal hDrop As Long, ByVal iFile As Long, Optional ByVal lpszFile As Long, Optional ByVal cch As Long) As Long
Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
Private Sub Form1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Data.GetFormat(vbCFFiles) Then
Dim pDataObj As oleexp.IDataObject 'change to olelib.IDataObject if using that, or any other typelib used
Dim fmt As FORMATETC
Dim stg As STGMEDIUM
Dim sFiles() As String
Dim nFiles As Long
Dim i As Long
Dim sBuffer As String
CopyMemory pDataObj, ByVal ObjPtr(Data) + 16&, 4&
fmt.cfFormat = CF_HDROP
fmt.TYMED = TYMED_HGLOBAL
fmt.lIndex = -1
fmt.dwAspect = DVASPECT_CONTENT
pDataObj.GetData fmt, stg
nFiles = DragQueryFileW(stg.Data, &HFFFFFFFF, 0, 0)
ReDim sFiles(nFiles - 1)
For i = 0 To nFiles - 1
SysReAllocStringLen VarPtr(sBuffer), , DragQueryFileW(stg.Data, i)
DragQueryFileW stg.Data, i, StrPtr(sBuffer), Len(sBuffer) + 1&
sFiles(i) = sBuffer
Next
CopyMemory pDataObj, 0&, 4& 'Must be freed like this
End If
End Sub
You now have an array of the dropped filenames, in Unicode, in sFiles()
Edit: PS- When you say you want to get text, do you mean like dragged text without files? Like how you can drag text out of Wordpad? If the origin supports dragging it, via CF_UNICODETEXT, you can retrieve it in VB using a variation of the above technique; if interested let me know.
Last edited by fafalone; Mar 18th, 2018 at 02:55 AM.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Originally Posted by fafalone
It doesn't seem the Data.Files object in OLEDragDrop supports Unicode.. it doesn't even return it, so can't send it for a conversion. I passed it straight to a Unicode messagebox to make sure (MessageBoxW StrPtr(Data.Files(0))), and question marks.
There's a couple ways to proceed. Of course my favorite is updating your whole drag/drop method to the fancy new one with IDropTarget that shows file icons when you drag over, among other things.
But the least disruptive is to just drop in a technique to get the Unicode filenames by converting the DataObject into an IDataObject. You will need a typelib with IDataObject defined, such as any version of mine or the original olelib, or really any as long as it's got IDataObject... Any Windows version is fine; it will work on XP... after that:
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function DragQueryFileW Lib "shell32.dll" (ByVal hDrop As Long, ByVal iFile As Long, Optional ByVal lpszFile As Long, Optional ByVal cch As Long) As Long
Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
Private Sub Form1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Data.GetFormat(vbCFFiles) Then
Dim pDataObj As oleexp.IDataObject 'change to olelib.IDataObject if using that, or any other typelib used
Dim fmt As FORMATETC
Dim stg As STGMEDIUM
Dim sFiles() As String
Dim nFiles As Long
Dim i As Long
Dim sBuffer As String
CopyMemory pDataObj, ByVal ObjPtr(Data) + 16&, 4&
fmt.cfFormat = CF_HDROP
fmt.TYMED = TYMED_HGLOBAL
fmt.lIndex = -1
fmt.dwAspect = DVASPECT_CONTENT
pDataObj.GetData fmt, stg
nFiles = DragQueryFileW(stg.Data, &HFFFFFFFF, 0, 0)
ReDim sFiles(nFiles - 1)
For i = 0 To nFiles - 1
SysReAllocStringLen VarPtr(sBuffer), , DragQueryFileW(stg.Data, i)
DragQueryFileW stg.Data, i, StrPtr(sBuffer), Len(sBuffer) + 1&
sFiles(i) = sBuffer
Next
CopyMemory pDataObj, 0&, 4& 'Must be freed like this
End If
End Sub
You now have an array of the dropped filenames, in Unicode, in sFiles()
Edit: PS- When you say you want to get text, do you mean like dragged text without files? Like how you can drag text out of Wordpad? If the origin supports dragging it, via CF_UNICODETEXT, you can retrieve it in VB using a variation of the above technique; if interested let me know.
Re: ole drag drop and problem with unicode charachters or load image with unicode
Originally Posted by fafalone
It doesn't seem the Data.Files object in OLEDragDrop supports Unicode.. it doesn't even return it, so can't send it for a conversion. I passed it straight to a Unicode messagebox to make sure (MessageBoxW StrPtr(Data.Files(0))), and question marks.
There's a couple ways to proceed. Of course my favorite is updating your whole drag/drop method to the fancy new one with IDropTarget that shows file icons when you drag over, among other things.
But the least disruptive is to just drop in a technique to get the Unicode filenames by converting the DataObject into an IDataObject. You will need a typelib with IDataObject defined, such as any version of mine or the original olelib, or really any as long as it's got IDataObject... Any Windows version is fine; it will work on XP... after that:
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function DragQueryFileW Lib "shell32.dll" (ByVal hDrop As Long, ByVal iFile As Long, Optional ByVal lpszFile As Long, Optional ByVal cch As Long) As Long
Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
Private Sub Form1_OLEDragDrop(Data As DataObject, Effect As Long, Button As Integer, Shift As Integer, X As Single, Y As Single)
If Data.GetFormat(vbCFFiles) Then
Dim pDataObj As oleexp.IDataObject 'change to olelib.IDataObject if using that, or any other typelib used
Dim fmt As FORMATETC
Dim stg As STGMEDIUM
Dim sFiles() As String
Dim nFiles As Long
Dim i As Long
Dim sBuffer As String
CopyMemory pDataObj, ByVal ObjPtr(Data) + 16&, 4&
fmt.cfFormat = CF_HDROP
fmt.TYMED = TYMED_HGLOBAL
fmt.lIndex = -1
fmt.dwAspect = DVASPECT_CONTENT
pDataObj.GetData fmt, stg
nFiles = DragQueryFileW(stg.Data, &HFFFFFFFF, 0, 0)
ReDim sFiles(nFiles - 1)
For i = 0 To nFiles - 1
SysReAllocStringLen VarPtr(sBuffer), , DragQueryFileW(stg.Data, i)
DragQueryFileW stg.Data, i, StrPtr(sBuffer), Len(sBuffer) + 1&
sFiles(i) = sBuffer
Next
CopyMemory pDataObj, 0&, 4& 'Must be freed like this
End If
End Sub
You now have an array of the dropped filenames, in Unicode, in sFiles()
Edit: PS- When you say you want to get text, do you mean like dragged text without files? Like how you can drag text out of Wordpad? If the origin supports dragging it, via CF_UNICODETEXT, you can retrieve it in VB using a variation of the above technique; if interested let me know.
Re: ole drag drop and problem with unicode charachters or load image with unicode
I was answering the question posed when you started this thread. You said you wanted to use OLEDragDrop, and that it was returning filenames with ? instead of Unicode characters in Data.Files(). My response concerned how to retrieve the filename from OLEDragDrop with the proper Unicode characters (a prerequisite to reading the data).