| 网站首页 | 业界新闻 | 群组 | 人才 | 下载频道 | 博客 | 代码贴 | 编程论坛
共有 199 人关注过本帖
标题:RichTextBox 显示行号
只看楼主 收藏
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
结帖率:50%
  已结贴   问题点数:10  回复次数:8   
RichTextBox 显示行号

RichTextBox 显示行号,如何让两个 RichTextBox 联动呢?
程序代码:
Private Sub RichTextBox2_Change()
RichTextBox1.Text = ""
    For i = 1 To UBound(Split(RichTextBox2.Text, vbCrLf)) + 1
        RichTextBox1.Text = RichTextBox1.Text & i & vbCrLf
    Next
End Sub

如上在RichTextBox2下拉进度条和手动编辑满屏换行的时候,RichTextBox1怎么也一起跟着换行

附件: 您没有浏览附件的权限,请 登录注册
2018-06-10 15:37
wds1
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:20
帖 子:211
专家分:1148
注 册:2016-3-10
  得分:5 
手动编辑的同步【滚动条同步建议用VScroll控制】
Private Sub RichTextBox2_SelChange() '光标改变时row1为当前行
 temp = Left$(RichTextBox2.Text, RichTextBox2.SelStart)
 row1 = (Len(temp) - Len(Replace(temp, vbCrLf, ""))) + 1
 Call txt1(row1)'richtextbox同步
End Sub
'假设richtextbox为19行
Public Sub txt1(row1)
 RichTextBox1.Text = ""
 row1 = IIf(row1 < 18, 1, row1 - 17)
 For i = 0 To 19
   RichTextBox1.Text = RichTextBox1.Text & Trim(Str(i + row1)) & vbCrLf
 Next
End Sub

[此贴子已经被作者于2018-6-10 17:38编辑过]

2018-06-10 17:37
风吹过b
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:206
帖 子:4184
专家分:25706
注 册:2008-10-15
  得分:5 
百度找到一个,未测试。
http://www.myhack58.com/Soft/html/9/90/2010/2010102220468.htm
感觉可行。

大量的API,所以我没看懂。

授人于鱼,不如授人于渔
早已停用QQ了
2018-06-10 18:08
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
  得分:0 
回复 2楼 wds1
固定RichTextBox1大小,再输出行号,不联动也是一种思路。
VScroll应该可以用API取其刻度值。
我再试试,谢谢!

[此贴子已经被作者于2018-6-11 19:56编辑过]

2018-06-11 19:30
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
  得分:0 
回复 3楼 风吹过b
API好多啊,用Picture的话应该不用那么多
之前试过在Picture上Print,结果和RichTextBox间距不对,看这视频好像改一下某种字体就行了。
我再试试,谢谢!
2018-06-11 19:55
wds1
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:20
帖 子:211
专家分:1148
注 册:2016-3-10
  得分:0 
根据http://www.myhack58.com/Soft/html/9/90/2010/2010102220468.htm程序,简化版
用得到1个picturebox,2个richtextbox

Private Declare Function SendMessageLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare Function SendMessageByRef Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, wParam As Long, lParam As Any) As Long
Private Declare Function SendMessageByLong Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEFROMCHAR = &HC9
Private Const EM_GETFIRSTVISIBLELINE = &HCE

Public Function LineCount() As Long 'richtextbox总行数
    LineCount = SendMessageByRef(Text2.hWnd, EM_GETLINECOUNT, 0&, 0&)
End Function

Public Function FirstVisibleLine() As Long 'richtextbox第一行可见行数
   FirstVisibleLine = SendMessageByLong(Text2.hWnd, EM_GETFIRSTVISIBLELINE, 0, 0)
End Function

Private Sub Form_Initialize()
  Picture1.Width = Screen.Width
  Picture1.Height = Screen.Height
  Picture1.Top = 0
  Picture1.Left = 0
  Text1.Width = 500
  Text1.Height = Screen.Height
  Text1.Top = 0
  Text1.Left = 0
  Text2.Width = Screen.Width - 500
  Text2.Height = Screen.Height
  Text2.Top = 0
  Text2.Left = 500
End Sub


Private Sub Text2_KeyUp(KeyCode As Integer, Shift As Integer) '键盘事件
  Call row
End Sub

Private Sub Text2_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) '鼠标事件
  Call row
End Sub

Private Sub Text2_SelChange() 'richtext文本标尺更改
  Call row
End Sub

Private Sub row
  lcount = LineCount                               '行总数
  lcurrent = SendMessageLong(Text2.hWnd, EM_LINEFROMCHAR, Text2.SelStart, 0&) '当前行【0开始】
  lline = FirstVisibleLine                            '最高可见行
  Text1.Text = ""
  For i = lline + 1 To lcount
      Text1.Text = Text1.Text & Str(i) & vbCrLf
 Next
End Sub


[此贴子已经被作者于2018-6-12 05:38编辑过]

附件: 您没有浏览附件的权限,请 登录注册
7 天前 05:27
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
  得分:0 
回复 6楼 wds1
好的,谢谢!

继续用RichTextBox显示行的话,如下一个API取滚动条刻度值,放在Timer事件中,基本上完全满足要求了..
程序代码:
Private Declare Function FuckSendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETFIRSTVISIBLELINE = &HCE

Private Sub Timer1_Timer()
RichTextBox1.Text = ""
i = FuckSendMessage(RichTextBox2.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) + 1 '取滚动条刻度值
    For j = i To UBound(Split(RichTextBox2.Text, vbCrLf)) + 1
        RichTextBox1.Text = RichTextBox1.Text & j & vbCrLf
            Next
End Sub


但是......汉字输入法会闪现,根本就打不了汉字,只可以输入英文
一样的代码用TEXT控件来输入是可以的,不会闪现,附件可以测试下
For j = i To UBound(Split(RichTextBox2.Text, vbCrLf)) + 1 应该是这里出问题了???


附件: 您没有浏览附件的权限,请 登录注册
7 天前 20:31
wds1
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:20
帖 子:211
专家分:1148
注 册:2016-3-10
  得分:0 

确实不行,不过不建议用2time,资源都被占用了。

看看如下语句
Private Sub Form_Load()
  Call RichTextBox2_SelChange
End Sub

Private Sub RichTextBox2_SelChange()
RichTextBox1.Text = ""
i = FuckSendMessage(RichTextBox2.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) + 1 '取滚动条刻度值
    For j = i To UBound(Split(RichTextBox2.Text, vbCrLf)) + 1
      RichTextBox1.Text = RichTextBox1.Text & j & vbCrLf
    Next
End Sub


[此贴子已经被作者于2018-6-12 22:27编辑过]

7 天前 21:41
xxyyx82
Rank: 1
等 级:新手上路
帖 子:22
专家分:3
注 册:2015-7-16
  得分:0 
回复 8楼 wds1
2个Timer是为了演示 TEXT与RichTextBox的区别
只放在Change事件中,没办法响应滚动条事件,

'-----------------------------------------------
异常应该还是在这里,UBound(Split(RichTextBox2.Text, vbCrLf)) ,为什么会冲突还不清楚。
既然获取TEXT1没有问题的话,那么.......再加一个RichTextBox3同步中转再获取其行数,是不是就不会冲突了?
试了下,可行,基本上全搞定啦。


程序代码:
Private Declare Function FuckSendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Const EM_GETFIRSTVISIBLELINE = &HCE

Private Sub RichTextBox2_Change()
RichTextBox3.Text = RichTextBox2.Text   'RichTextBox3.Text同步RichTextBox2
End Sub

Private Sub Timer1_Timer()
RichTextBox1.Text = ""
i = FuckSendMessage(RichTextBox2.hwnd, EM_GETFIRSTVISIBLELINE, 0&, 0&) + 1 '取滚动条刻度值
    For j = i To UBound(Split(RichTextBox3.Text, vbCrLf)) + 1              '获取RichTextBox3行数
        RichTextBox1.Text = RichTextBox1.Text & j & vbCrLf
            Next
End Sub


6 天前 09:32







关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.240619 second(s), 8 queries.
Copyright©2004-2018, BCCN.NET, All Rights Reserved