March 29, 2024, 09:58:56 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Line Numbers in Richedit control using margins

Started by aurelCB, February 10, 2020, 09:43:38 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

aurelCB

February 10, 2020, 09:43:38 AM Last Edit: March 04, 2023, 02:37:04 AM by aurelCB
Hello

h3kt0r

I'll try to convert this snippet to iwBasic syntax tonight.

h3kt0r

February 10, 2020, 09:00:14 PM #2 Last Edit: February 10, 2020, 09:47:19 PM by h3kt0r Reason: Update
This is what i've came up with. Haven't tested this snippet yet, you'll have to provide
the code template to create the window, the controls and the corresponding handler.
Don't forget to subclass your Richedit control at first.

INT rline, rchar ': Global variables

Sub drawLinenumbers()
Int hdc, hRichEdit, hFont, prevObj, fontH, first, lastChar
Int last, textW, textH, FWEIGHT, FTSIZE, FFLAGS, leftM
RECT rc
POINT pt
POINT tChar
TEXTMETRIC tm
String FONTNAME
FONTNAME = "FixedSys"
FWEIGHT = 0
FTSIZE = 9
FFLAGS = 0
leftM = 54  ': Left margin value
SetFont WIN, FONTNAME, FTSIZE, FWEIGHT, FFLAGS
GETTEXTSIZE (WIN, "0", textW, textH)                 ': Find the desired text Height


  hRichEdit = GetControlHandle (WIN, RICHEDIT_1)
  hdc = GetDC(hRichEdit, 0, DCX_CACHE)

  hFont = CreateFont(textH,0,0,0, FWEIGHT,0,0,0,0,0,0,0,0, FONTNAME)

  prevObj = SelectObject(hDC, hFont)

  GetTextMetrics(hDC, tm)
  fontH = tm.tmHeight

  GetClientRect(hRichEdit, rc)
  rc.right = leftM - 8
  FillRect(hDC, rc, GetStockObject(WHITE_BRUSH))
  SetBkMode(hDC, TRANSPARENT)
  SetTextColor(hDC, 0xC0C0C0)

  ': get the first line
  first = SendMessage(hRichEdit, EM_GETFIRSTVISIBLELINE, 0, 0)

  ': get the last character/line
  pt.x = rc.left
  pt.y = rc.bottom
  lastChar = SendMessage(hRichEdit, EM_CHARFROMPOS, 0, pt)
  last = SendMessage(hRichEdit, EM_LINEFROMCHAR, lastChar, 0)
  ': get the first character
  pt.y = rc.top
  firstChar = SendMessage(hRichEdit, EM_CHARFROMPOS, 0, pt)

  ': get the client coordinates fo the first character
  SendMessage(hRichEdit, EM_POSFROMCHAR, tChar, firstChar)
  ': adjust linenumber RECT
  rc.top = tChar.y

  For rline = first To last
    DrawText(hDc, Str$(rline + 1), -1, rc, DT_RIGHT)
    rc.top+ = fontH
  Next rline
 
  SelectObject(hDC, prevObj)
  ReleaseDC(hRichEdit, hDC)
 
  Return
Endsub

Sub getLinenumber(hRichEdit)
POINT pos
  ': gets the linenumber in a RichEdit control from the caret position
  SetFocus(hRichEdit)
  GetCaretPos(pos)
  rchar = SendMessage(hRichEdit, EM_CHARFROMPOS, 0, pos)
  rline =  SendMessage(hRichEdit, EM_LINEFROMCHAR, rchar, 0)
  Return rline
EndSub

aurelCB

Well yes i know that i must subclass richedit but with or without subclasing not work as in PB

Andy

Aurel,

I've done it without subclassing (just added it in).

Will send you the code tomorrow as I'm a little busy for the rest of the day.

Andy.
 :)
Day after day, day after day, we struck nor breath nor motion, as idle as a painted ship upon a painted ocean.