|
-
Mar 16th, 2009, 03:55 PM
#1
Thread Starter
Lively Member
[RESOLVED] EM_GETLINE LaVolpe-KoolSid conversation
Hello,
Recently I have use the above code which was displayed by Koolsid I belive.
I have used it before with no single problem up to 1200 lines .
But that was then, now I have RTB up to 3000 lines.
Reads fine until hits 2020 line and from that moment returns to the 1st line
and continuously duplicate it to the end of calculated number of all lines.
In this code, is there a buffer limitation ?
Is there something I don’t know and should know?
Code:
Option Explicit
Private Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_GETSEL = &HB0
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_GETLINE = &HC4
Dim RTBLineCount As Long, Charindex As Long
Dim FirstChar As Long, RowLength As Long, CursorPos As Long, nLine As Long
Dim Buffer() As Byte, LineText A
************************************
Sub part
RTBLineCount = SendMessage(RichTextBox1.hwnd, EM_GETLINECOUNT, 0, 0)
For i = 0 To RTBLineCount - 1
ccnt = ccnt + 1
Charindex = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, ByVal i, ByVal CLng(0))
RichTextBox1.SelStart = Charindex
CursorPos = SendMessage(RichTextBox1.hwnd, EM_GETSEL, 0, ByVal 0&) \ 65536
nLine = SendMessage(RichTextBox1.hwnd, EM_LINEFROMCHAR, CursorPos, ByVal 0&)
FirstChar = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
RowLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, FirstChar, ByVal 0&)
ReDim Buffer(RowLength + 1)
Buffer(0) = RowLength + 1
SendMessage RichTextBox1.hwnd, EM_GETLINE, nLine, Buffer(0)
LineText = Left$(StrConv(Buffer, vbUnicode), RowLength-6) ' single line, less 6 right cut.
' ***************
'LineText = Replace(LineText, " ", " ") good but not to be use here
TX = Trim(LineText)
Do While InStr(TX, " ")
TX = Replace$(TX, " ", " ")
Loop
RichTextBox3.Text = RichTextBox3.Text & TX & vbNewLine
Next i
-
Mar 16th, 2009, 04:27 PM
#2
Re: EM_GETLINE LaVolpe-KoolSid conversation
Did you mean to add this as a post to an existing thread? If so provide me with the URL and I'll move it there.
-
Mar 16th, 2009, 04:31 PM
#3
Re: EM_GETLINE LaVolpe-KoolSid conversation
Maybe... The problem is with the EM_GETSEL call.
If the character position at that point is > 65535 then the return value is -1 and -1\ 65536 is zero, back to the first character. There is a workaround
But first, I'm confused by your code:
1. You are looping thru RTF#1, ok
2. You are setting CharIndex to 1st character of each line, ok
3. You are setting the .SelStart, why? Just for visual effect?
4. You are then trying to set CursorPos which is failing but CursorPos should equal CharIndex when it doesn't fail
5. You are trying to set nLine, but isn't nLine = i ?
6. You are setting FirstChar, but FirstChar should = CharIndex
7. RowLength at this point could be retrieved by using CharIndex
So from what I see is CharIndex = FirstChar = CursorPos (if -1 not returned). Also i = nLine
Aside from the confusion, if you need the workaround to get a valid character index from a rtf that has more than 65K characters, I can provide that.
-
Mar 16th, 2009, 04:33 PM
#4
Re: EM_GETLINE LaVolpe-KoolSid conversation
Your CursorPos calculation is perhaps the cause but I don't know for sure. Are you familiar with the Debug module? If not then maybe you could benefit from my VB6 Debug Tutorial.
-
Mar 16th, 2009, 04:36 PM
#5
Thread Starter
Lively Member
Re: EM_GETLINE LaVolpe-KoolSid conversation
Yes, I would like to leave it as is as a new post.
I have found a conversation between those two guys in a similar topic
and I was hoping that one of them, or anyone in this matter would explain it to me.
Many thanks,
-
Mar 16th, 2009, 04:41 PM
#6
Thread Starter
Lively Member
Re: EM_GETLINE LaVolpe-KoolSid conversation
LaVolpe, Thanks for replay.
Any idea from you would be greatly appreciated to correct all this
and have better understanding the issue here.
Much appreciated.
-
Mar 16th, 2009, 04:44 PM
#7
Re: EM_GETLINE LaVolpe-KoolSid conversation
If you want to try to debug you could set a breakpoint to pause the code when i gets to 2020 and then step through the code line by line examining the variables to see what's happening. That's a pretty sure way to find the problem and a solution.
-
Mar 16th, 2009, 04:45 PM
#8
Re: EM_GETLINE LaVolpe-KoolSid conversation
A quick fix, using your code.... but tweaking it
Code:
For nLine = 0 To RTBLineCount - 1
ccnt = ccnt + 1
Charindex = SendMessage(RichTextBox1.hwnd, EM_LINEINDEX, nLine, ByVal 0&)
RichTextBox1.SelStart = Charindex
RowLength = SendMessage(RichTextBox1.hwnd, EM_LINELENGTH, CharIndex, ByVal 0&)
ReDim Buffer(RowLength + 1)
Buffer(0) = RowLength + 1
SendMessage RichTextBox1.hwnd, EM_GETLINE, nLine, Buffer(0)
LineText = Left$(StrConv(Buffer, vbUnicode), RowLength-6) ' single line, less 6 right cut.
...
Copy your current code before replacing this, just in case the tweaks affect something else you didn't show us.
Note that the variables i, FirstChar, CursorPos are no longer used in the above tweaks.
-
Mar 16th, 2009, 06:51 PM
#9
Thread Starter
Lively Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|