Delphi scrolling Memo -
right have code.
procedure tform1.memo1change(sender : tobject); begin sendmessage(informacje.handle, em_linescroll, 0, memo1.lines.count); end;
the problem can't scroll memolines when new lines added. want stop moving cursor end on scroll up, start moving cursor end when scrollbar @ bottom. thank you help.
in others words. imagine there irc chat memo. new message, caret @ end of last message. want read previous messages usin scroll bar (up) cant cause there newer messages move carret bottom. want stop on mouse wheel up, read messages , after previous state (caret once again @ end on new message) when scroll bottom.
you need condition decide whether scroll bottom or not. below seems work simple test, sums top visible line number of possible lines memo can show find out if last line visible or not.
procedure tform1.memo1change(sender: tobject); var linecount, topline: integer; begin linecount := memo1.perform(em_getlinecount, 0, 0) - 1; topline := memo1.perform(em_getfirstvisibleline, 0, 0); if (topline + getvisiblelinecount(memo1)) >= linecount sendmessage(memo1.handle, em_linescroll, 0, linecount); end;
where
function getvisiblelinecount(memo: tmemo): integer; var dc: hdc; savefont: hfont; textmetric: ttextmetric; editrect: trect; begin dc := getdc(0); savefont := selectobject(dc, memo.font.handle); gettextmetrics(dc, textmetric); selectobject(dc, savefont); releasedc(0, dc); memo.perform(em_getrect, 0, lparam(@editrect)); result := (editrect.bottom - editrect.top) div textmetric.tmheight; end;
(you can cache visible line count avoid having calculate every change in memo.)
you may need further tweak code though, f.i. case there less lines in memo can show. code not take caret position consideration.
Comments
Post a Comment