IonicWind Software

Creative Basic => GUI Programs => Topic started by: aurelCB on February 10, 2020, 09:43:38 AM

Title: Line Numbers in Richedit control using margins
Post by: aurelCB on February 10, 2020, 09:43:38 AM
Hello
Title: Re: Line Numbers in Richedit control using margins
Post by: h3kt0r on February 10, 2020, 09:54:11 AM
I'll try to convert this snippet to iwBasic syntax tonight.
Title: Re: Line Numbers in Richedit control using margins
Post by: h3kt0r on February 10, 2020, 09:00:14 PM
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
Title: Re: Line Numbers in Richedit control using margins
Post by: aurelCB on February 13, 2020, 05:11:48 AM
Well yes i know that i must subclass richedit but with or without subclasing not work as in PB
Title: Re: Line Numbers in Richedit control using margins
Post by: Andy on February 13, 2020, 06:22:01 AM
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.
 :)