|
-
Jul 31st, 2000, 09:38 AM
#1
Thread Starter
New Member
Hi all,
I am designing some sort of a Text viewer in which a user can change the FontColor. I use a RichTextBox so that filesize is not limited to 32k. When I change the Text color, I use RTB options as .SelStart, .SelLen, .SelColor, etc.
However, when -in a large Text- you scrolled down and then change the Text color, the scrollbar is 'reset' to the top (or focused on the cursor position).
What I want now is that, effectively, changing the Text color does not affect the Scroll position in the RTB (just as setting the .BackColor property doesn't, or the ForeColor in a ordinary TextBox doesn't). I've tried the user32 functions GetScrollPos and SetScrollPos. The latter however, sets ONLY the Scoll Bar position, but leaves the Text position unchanged, so that only half of the job is done (you get the odd situation that you see the top of the text but the scrollbar is e.g. half way down...).
Can someone help me with this?
Thanx!
Gildor
-
Jul 31st, 2000, 11:22 AM
#2
Set the SelStart property.
Code:
RichTextBox1.SelStart = 100
-
Aug 1st, 2000, 03:45 AM
#3
Thread Starter
New Member
Thanx Megatron, but setting the .SelStart would only set the cursor to that position, not the ScrollBar, right?
I have now something like this:
With RichTextBox1
CursorPos = .SelStart 'Remember current Cursor Pos
ScrollPos = GetScrollPos(.hWnd, 1) 'Get scrollbar postition
.Visible = False 'Don't show the selecting part
.SelStart = 0
.SelLen = Len(.Text)
.SelColor = NewColor
.SelLen = 0
.Visible = True
.SelStart = CursorPos
Dummy = SetScrollPos(.hWnd, 1, ScrollPos, 1)
End With
Again, SetScrollPos sets only the position of the scrollbar, but the text is still focussed at the cursor position (.SelStart), which is not necessarily the same.
Hope this clears things up,
Regards,
Gildor
-
Aug 1st, 2000, 08:33 AM
#4
Try this (change the variable and object name as necessary):
Code:
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_LINESCROLL = &HB6
Private Const EM_GETFIRSTVISIBLELINE = &HCE
Private Const SB_LINEDOWN = 1
Private Const SB_LINEUP = 0
Private Sub Command1_Click()
Dim lngFirstLine&
Dim lngCurrentLine&
Dim lngSelStart&
Randomize
With RichTextBox1
lngSelStart = .SelStart
lngFirstLine = SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
.Visible = False
.SelStart = 0
.SelLength = Len(.Text)
.SelColor = QBColor(Int(Rnd * 16))
.SelStart = lngSelStart
lngCurrentLine = SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&)
If lngCurrentLine < lngFirstLine Then
Do
SendMessage .hwnd, EM_LINESCROLL, SB_LINEDOWN, 0&
Loop While SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) <> lngFirstLine
ElseIf lngCurrentLine > lngFirstLine Then
Do
SendMessage .hwnd, EM_LINESCROLL, SB_LINEUP, 0&
Loop While SendMessage(.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) <> lngFirstLine
End If
.Visible = True
End With
End Sub
Good luck!
-
Aug 1st, 2000, 08:42 AM
#5
Hyperactive Member
Scroll Position
Is the caret in the same area as the scroll box position when you do the colour action?
-
Aug 2nd, 2000, 04:22 AM
#6
Thread Starter
New Member
I finally managed to control the scrollbar, using something similar to what Joacim suggested: the SendMessage function and the EM_LINESCROLL constant.
Thanx all for your attention,
Gildor
-
Aug 2nd, 2000, 08:49 AM
#7
Thanx Megatron, but setting the .SelStart would only set the cursor to that position, not the ScrollBar, right?
It will move the cursor position as well as the ScrollBar. I know it might sound "to good to be true" but it works.
-
Aug 2nd, 2000, 09:49 AM
#8
Originally posted by Megatron
It will move the cursor position as well as the ScrollBar. I know it might sound "to good to be true" but it works.
What I think Gildor wants is to scroll beyound the cursor position. The cursor can for example be at the first line but the first visible line i the RTF-box may be the 10th line.
-
Aug 2nd, 2000, 09:58 AM
#9
Thread Starter
New Member
Joacim, you are absolutely right ;
I couldn't have set it better! (Otherwise there wouldn't have been so much confusion )
Anyways, I got it working now... Thanx again!
Gildor
-
Aug 4th, 2001, 11:14 AM
#10
Related Question
I'm trying to do the same type of thing, except I want to do this:
1) Check to see if scrollbar is at the bottom of the RichTextBox
2) Add a line of text (and add colouring) to the end.
3) If the scrollbar WAS at the bottom, scroll down. (I can do this), BUT, if it was somewhere else, return to that spot. (I can do this as well, with the information you just gave.)
What I can't seem to figure out how to do, however, is to check to see if the scrollbar is at the bottom. I initially thought about checking to see if .SelStart = Len(.Text), but the cursor may not be at the very end of the RichTextBox. (User may have clicked somewhere else in the RichTextBox.) If it's not too much to ask, perhaps someone could expand on this method, to check where the scrollbar is at the beginning? Perhaps some sort of method to determine the LAST visible line in the RichTextBox would be the key? And then comparing it to the TOTAL number of lines?
Thanks!
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
|