I have started the code to put in line numbers. If you think it is easy. Here is just the function declaration needed and partial code just to put numbers to the left of a line.
Option Explicit
Public Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetWindowLong Lib "USER32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "USER32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetClientRect Lib "USER32" (ByVal hWnd As Long, lpRect As RECT) As Long
Public Declare Function OffsetRect Lib "USER32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function FillRect Lib "USER32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
Public Declare Function OleTranslateColor Lib "oleaut32.dll" (ByVal lOleColor As Long, ByVal lHPalette As Long, lColorRef As Long) As Long
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Public Declare Function DrawText Lib "USER32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Public Declare Function MoveToEx Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, lpPoint As POINTAPI) As Long
Public Declare Function LineTo Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long) As Long
Public Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Type POINTAPI
x As Long
y As Long
End Type
Public Const SFF_SELECTION = &H8000&
Public Const WM_USER = &H400
Public Const EM_EXSETSEL = (WM_USER + 55)
Public Const EM_EXGETSEL = (WM_USER + 52)
Public Const EM_POSFROMCHAR = &HD6&
Public Const EM_CHARFROMPOS = &HD7&
Public Const EM_EXLINEFROMCHAR = (WM_USER + 54)
Public Const EM_GETTEXTRANGE = (WM_USER + 75)
Public Const EM_STREAMIN = (WM_USER + 73)
Public Const EM_HIDESELECTION = WM_USER + 63
Public Const PS_SOLID = 0
Public Const DT_CALCRECT = &H400
Public Const DT_RIGHT = &H2
Public Const DT_VCENTER = &H4
Public Const DT_SINGLELINE = &H20
Public Const GWL_WNDPROC = (-4)
Private Const WM_VSCROLL = &H115
Public lPrevWndProc As Long
' This actually draws the line numbers
Dim lLine As Long
Dim lCount As Long
Dim lCurrent As Long
Dim hBr As Long
Dim lEnd As Long
Dim lhDC As Long
Dim bComplete As Boolean
Dim tR As RECT, tTR As RECT
Dim oCol As OLE_COLOR
Dim lStart As Long
Dim lEndLine As Long
Dim tPO As POINTAPI
Dim lLineHeight As Long
Dim hPen As Long
Dim hPenOld As Long
'Debug.Print "DrawLines"
lhDC = picTo.hdc
DrawText lhDC, "Hy", 2, tTR, DT_CALCRECT
lLineHeight = tTR.Bottom - tTR.Top
lCount = LineCount
lCurrent = SendMessageLong(TextBox1.hWnd, EM_LINEFROMCHAR, TextBox1.SelStart, 0&)
lStart = TextBox1.SelStart
lEnd = TextBox1.SelStart + TextBox1.SelLength - 1
If (lEnd > lStart) Then
lEndLine = LineForCharacterIndex(lEnd)
Else
lEndLine = lCurrent
End If
lLine = FirstVisibleLine
GetClientRect picTo.hWnd, tR
lEnd = tR.Bottom - tR.Top
hBr = CreateSolidBrush(TranslateColor(picTo.BackColor))
FillRect lhDC, tR, hBr
DeleteObject hBr
tR.Left = 2
tR.Right = tR.Right - 2
tR.Top = 0
tR.Bottom = tR.Top + lLineHeight
SetTextColor lhDC, TranslateColor(vbButtonShadow)
Do
' Ensure correct colour:
If (lLine = lCurrent) Then
SetTextColor lhDC, TranslateColor(vbWindowText)
ElseIf (lLine = lEndLine + 1) Then
SetTextColor lhDC, TranslateColor(vbButtonShadow)
End If
' Draw the line number:
DrawText lhDC, CStr(lLine + 1), -1, tR, DT_RIGHT
' Increment the line:
lLine = lLine + 1
' Increment the position:
OffsetRect tR, 0, lLineHeight
If (tR.Bottom > lEnd) Or (lLine + 1 > lCount) Then
bComplete = True
End If
Loop While Not bComplete
' Draw a line...
MoveToEx lhDC, tR.Right + 1, 0, tPO
hPen = CreatePen(PS_SOLID, 1, TranslateColor(vbButtonShadow))
hPenOld = SelectObject(lhDC, hPen)
LineTo lhDC, tR.Right + 1, lEnd
SelectObject lhDC, hPenOld
DeleteObject hPen
If picTo.AutoRedraw Then
picTo.Refresh
End If
I need to add more code and get it all to work together in each open document.
The past has a lot of memories to hold onto; but, today is chock full of new adventures, and, the future shouts out, "The best is yet to come!" -- TerryC