Solved

How do I find the range of characters visible in RichTextBox

Posted on 2001-06-29
7
259 Views
Last Modified: 2008-02-26
I need to know, at any given time, what is the range of characters currently VISIBLE in my RichTextBox window.
I don't want the user to have to select anything, or click anything except the scroll bars to view the file.

thanks,

russ
0
Comment
Question by:RUSS_EMI
  • 4
  • 2
7 Comments
 
LVL 27

Expert Comment

by:Ark
ID: 6240306
Hi
'====Bas module code===

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type CharRange
   cpMin          As Long
   cpMax          As Long
End Type

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_CHARFROMPOS& = &HD7


Public Function GetCharrange(rtb As RichTextBox) As CharRange
   Dim pt As POINTAPI
   GetCharrange.cpMin = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   pt.X = rtb.Width \ Screen.TwipsPerPixelX
   pt.Y = rtb.Height \ Screen.TwipsPerPixelY
   GetCharrange.cpMax = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
End Function

'====Form code===


Private Sub Command1_Click()
  Dim cr As CharRange
  cr = GetCharrange(RichTextBox1)
  MsgBox "Currently visible characters" & vbCrLf & "from " & cr.cpMin & " to " & cr.cpMax
End Sub


Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 6240330
Hi
For some reason above function get one string more then contain in RichTextBox. Change:

pt.Y = rtb.Height \ Screen.TwipsPerPixelY - rtb.Font.Size * 1.4

Cheers
0
 
LVL 27

Accepted Solution

by:
Ark earned 200 total points
ID: 6240334
OK, more correct code:

Private Type POINTAPI
    X As Long
    Y As Long
End Type

Public Type CharRange
   cpMin          As Long
   cpMax          As Long
End Type

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_CHARFROMPOS& = &HD7
Const EM_LINEINDEX = &HBB
Const EM_LINELENGTH = &HC1
Const WM_USER = &H400
Const EM_EXLINEFROMCHAR = (WM_USER + 54)

Public Function GetCharrange(rtb As RichTextBox) As CharRange
   Dim pt As POINTAPI
   Dim pos As Long
   Dim ret As Long
   GetCharrange.cpMin = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   pt.X = rtb.Width \ Screen.TwipsPerPixelX
   pt.Y = rtb.Height \ Screen.TwipsPerPixelY
   pos = SendMessage(rtb.hwnd, EM_CHARFROMPOS, 0&, pt)
   ret = SendMessage(rtb.hwnd, EM_EXLINEFROMCHAR, 0, ByVal pos)
   ret = SendMessage(rtb.hwnd, EM_LINEINDEX, ret, ByVal 0&)
   ret = SendMessage(rtb.hwnd, EM_LINELENGTH, ret, ByVal 0&)
   GetCharrange.cpMax = pos - ret
End Function

Cheers
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

by:RUSS_EMI
ID: 6240845
Ark,
   Thank you for your information.  Just a few specifics before I close this out and give you the 200 points.

The complete project is to have an .mp3 while a certain portion of text is visible, and stop when it's not.

If you have any ideas regarding this specifically, please share them.  I will increase the points, if necessary.

Russ
0
 
LVL 4

Expert Comment

by:amit_panjwani
ID: 6240989
watching ...
0
 

Author Comment

by:RUSS_EMI
ID: 6241188
Thanks, I got the rest by experimentation.

Russ
0
 
LVL 27

Expert Comment

by:Ark
ID: 6241567
Thanks for points, glad I could help you.
You can use InStr function to determine Special text position and compare it with cr.cpMin and cr.cpMax.
Or, reverse, you can get visible text:
rtb.Selstrat = cr.cpMin
rtb.SelLength = cr.cpMax - cr.cpMin
sTemp = rtb.SelText
and compare sTemp with sSpecText

Cheers

0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now