Hi guys. I ported in ebasic some codes from a cprogramming post about richedit streamin:
http://cboard.cprogramming.com/windows-programming/54701-rich-edit.html (http://cboard.cprogramming.com/windows-programming/54701-rich-edit.html)
and that's my code:
$INCLUDE "windows.inc"
TYPE EDITSTREAM
UINT dwCookie
UINT dwError
UINT pfnCallback
ENDTYPE
CONST SF_TEXT = 1
CONST EM_STREAMIN = WM_USER + 73
FILE file1
DIALOG d1
STRING filename,filter
filter = "TXT file (*.txt)|*.txt||"
CREATEDIALOG d1,0,0,587,360,0x80C80080,0,"Rich Edit demo",&Handler
CONTROL d1,@RICHEDIT,"",7,35,570,294,0x50B010C4,1
DOMODAL d1
END
SUB Handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
BEGINMENU d1
MENUTITLE "File"
MENUITEM "Save",0,6
MENUITEM "Load",0,7
SEPARATOR
MENUITEM "Quit",0,1
ENDMENU
CASE @IDMENUPICK
SELECT @MENUNUM
CASE 1:'Quit
CLOSEDIALOG d1,@IDOK
CASE 6:'SAVE
filename = FILEREQUEST("Save file",d1,0,filter,"rtf")
if(len(filename))
if(OPENFILE(file1,filename,"W")=0)
CONTROLCMD d1,1,@RTSAVE,file1,1
CLOSEFILE file1
endif
endif
CASE 7:'LOAD
filename = FILEREQUEST("Load file",d1,1,filter,"txt")
if(len(filename))
reLoadFile(filename)
endif
ENDSELECT
ENDSELECT
RETURN
ENDSUB
SUB StreamIn(hFile:INT,buffer:POINTER,cb:INT,pcb:POINTER),INT
POINTER p
p=NULL
RETURN _ReadFile(hFile,buffer,cb,pcb,#<OVERLAPPED>p)
ENDSUB
SUB reLoadFile(fname:STRING)
INT hFile
EDITSTREAM es
hFile = _CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
IF hFile<>INVALID_HANDLE_VALUE
es.dwCookie = hFile
es.dwError=0
es.pfnCallback = &StreamIn
SENDMESSAGE d1,EM_STREAMIN,SF_TEXT,es,1
SENDMESSAGE d1,EM_SETMODIFY,FALSE,0,1
ENDIF
_CloseHandle(hFile)
RETURN
ENDSUB
But that's not work ??? . Any idea ? Thanks in advance for the answers :)
You can load a rich edit directly by reading the users guide.
See Windows Programming -> Controls -> Using Rich Edit Controls.
Specifically the part about:
return = CONTROLCMD (window | dialog, ID, @RTSAVE, FILE | STRING, type)
and
return = CONTROLCMD (window | dialog, ID, @RTLOAD, FILE | STRING, type)
;)
Paul.
Hi Paul, I know that way. I'm interested to understand how to load a file in a richedit control via API. :)
The code works well on a C compiler, not when i translate it on EBasic. What's wrong ? ???
Whoever wrote the original wrote it wrong then ;)
You need to return 0 from the callback function, not the return of _ReadFile for one. The 1st parameter for the callback is wrong, it is a pointer that points to the es variable that you send in the SendMessage command, which you need to dereference to get the file handle from. Your are dereferencing a NULL pointer in the callback, you should just be passing NULL to _ReadFile. But I am assuming you used windows.inc which has the last parameter declared wrong.
The correct declare is
DECLARE IMPORT,ReadFile(hFile as UINT,lpBuffer as POINTER,nNumberOfBytesToRead as UINT,lpNumberOfBytesRead as POINTER,lpOverlapped as POINTER)
There may be other problems with the code, but that is just from looking at it.
Paul.
Actually it was your interpretation of the original that was wrong.
DWORD_PTR dwCookie
Is a pointer to a dword. Which is technically incorrect but works because the C code does this:
HANDLE hFile = (HANDLE) dwCookie;
The address of the first element of a structure (udt) is the same as the udt itself. The (HANDLE) typecasts the pointer to a UINT.
Again I have to ask, why not use the built in functionality? The code is the same and will work with any rich edit control, even if you create the control with CreateWindowEx.
Paul.
Thank you Paul . Yes, i made some mistakes porting the code in EBasic. Now it works. ;D
Now i changed the declaration in windows.inc
DECLARE IMPORT, _ReadFile ALIAS ReadFile(hFile AS INT,lpBuffer AS POINTER,nNumberOfBytesToRead AS INT,lpNumberOfBytesRead AS POINTER,lpOverlapped AS OVERLAPPED),INT
with
DECLARE IMPORT, _ReadFile ALIAS ReadFile(hFile AS INT,lpBuffer AS POINTER,nNumberOfBytesToRead AS INT,lpNumberOfBytesRead AS POINTER,lpOverlapped AS POINTER),INT
and the code
$INCLUDE "windows.inc"
TYPE EDITSTREAM
POINTER dwCookie
UINT dwError
POINTER pfnCallback
ENDTYPE
CONST SF_TEXT = 1
CONST EM_STREAMIN = WM_USER + 73
FILE file1
DIALOG d1
STRING filename,filter
filter = "TXT file (*.txt)|*.txt||"
CREATEDIALOG d1,0,0,587,360,0x80C80080,0,"Rich Edit demo",&Handler
CONTROL d1,@RICHEDIT,"",7,35,570,294,0x50B010C4,1
DOMODAL d1
END
SUB Handler
SELECT @MESSAGE
CASE @IDINITDIALOG
CENTERWINDOW d1
BEGINMENU d1
MENUTITLE "File"
MENUITEM "Save",0,6
MENUITEM "Load",0,7
SEPARATOR
MENUITEM "Quit",0,1
ENDMENU
CASE @IDMENUPICK
SELECT @MENUNUM
CASE 1:'Quit
CLOSEDIALOG d1,@IDOK
CASE 6:'SAVE
filename = FILEREQUEST("Save file",d1,0,filter,"rtf")
if(len(filename))
if(OPENFILE(file1,filename,"W")=0)
CONTROLCMD d1,1,@RTSAVE,file1,1
CLOSEFILE file1
endif
endif
CASE 7:'LOAD
filename = FILEREQUEST("Load file",d1,1,filter,"txt")
if(len(filename))
reLoadFile(filename)
endif
ENDSELECT
ENDSELECT
RETURN
ENDSUB
SUB StreamIn(dwCookie:POINTER,buffer:POINTER,cb:INT,pcb:POINTER),INT
UINT hfile
hfile=#<UINT>dwCookie
_ReadFile(hfile,buffer,cb,pcb,NULL)
RETURN 0
ENDSUB
SUB reLoadFile(fname:STRING)
UINT hFile
EDITSTREAM es
hFile = _CreateFile(fname,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL)
IF hFile<>INVALID_HANDLE_VALUE
es.dwCookie = &hFile
es.dwError=0
es.pfnCallback = &StreamIn
SENDMESSAGE d1,EM_STREAMIN,SF_TEXT,es,1
SENDMESSAGE d1,EM_SETMODIFY,FALSE,0,1
ENDIF
_CloseHandle(hFile)
RETURN
ENDSUB
:)
Let me know for eventual mistakes or suggestions. ;)
Paul, maybe this is a oftopic, but please look at the attached image. I'm using IE6 from XPSP3
Looks fine in IE7 on SP3. So either it was an IE bug that was corrected, or a SMF bug that doesn't affect IE7 and above.
Paul.