Конференция "KOL" » RichEdit и автопрокрутка [Delphi, Windows]
 
  • Гость (16.01.11 03:43) [0]
    Помогите с кодом автопрокрутки RichEdit, с возможностью её включения при перемещении ползунка скроллбара до самого низа и выключения при перемещении ползунка скроллбара вверх.

    Вот, собственно, код, не спорю, что кривой, грамотней писать пока опыта не хватает :)

    program Project1;

    uses
     Windows, Messages, KOL;

    type
     PForm1 = ^TForm1;
     TForm1 = object(TObj)
       Form: PControl;
       RichEdit1: PControl;
       Timer1: PTimer;
     public
       procedure Timer1Timer(Sender: PObj);
     end;

    var
     AutoScroll: Boolean;
     VertPos: Integer;

    function WndProcRichEdit(Sender: PControl; var Msg: TMsg;
     var Rslt: Integer): Boolean;
    var
     P: Integer;
    begin
     Result := False;
     Rslt := 0;
     case Msg.message of
       WM_VSCROLL:
         begin
           if ( LOWORD(Msg.wParam) = SB_THUMBTRACK ) then
           begin
             P := HIWORD(Msg.wParam);
             AutoScroll := (VertPos > P - 10) and (VertPos < P + 10);
             if AutoScroll then Sender.Perform(EM_SCROLL, SB_BOTTOM, 0);
             VertPos := GetScrollPos(Sender.Handle, SB_VERT);
           end;
         end;
     end;
    end;

    procedure NewForm1(var Result: PForm1; AParent: PControl);
    var
     Style: Integer;
    begin
     New(Result, Create);
     with Result^ do
     begin
       Form := NewForm(AParent, 'Project1');
       Form.Add2AutoFree(Result);
       Applet := Form;
       Form.SetClientSize(320, 240).CenterOnParent;

       RichEdit1 := NewRichEdit( Form, [eoNoHScroll, eoReadonly] );
       RichEdit1.SetAlign(caClient);
       {
       Style := GetWindowLong(RichEdit1.GetWindowHandle, GWL_STYLE);
       SetWindowLong(RichEdit1.Handle, GWL_STYLE, Style and not ES_AUTOVSCROLL);
       }
       RichEdit1.AttachProc(WndProcRichEdit);

       Timer1 := NewTimer(1000);
       Timer1.OnTimer := Timer1Timer;
       Timer1.Enabled := True;
     end;
    end;

    procedure TForm1.Timer1Timer(Sender: PObj);
    var
     I: Integer;
    begin
     if not AutoScroll then
       I := RichEdit1.SelStart;

     RichEdit1.SelStart := RichEdit1.TextSize;
     RichEdit1.RE_FmtFontColor := clGray;
     RichEdit1.RE_FmtBold := False;
     RichEdit1.RE_Append( Time2StrFmt('HH:mm:ss', Now) + ' ', False );
     RichEdit1.RE_FmtFontColor := clBlack;
     RichEdit1.RE_FmtBold := True;
     RichEdit1.RE_Append('Nick ', False);
     RichEdit1.RE_FmtFontColor := clNavy;
     RichEdit1.RE_FmtBold := False;
     RichEdit1.RE_Append('Message' + sLineBreak, False);

     if not AutoScroll then
       RichEdit1.SelStart := I;

     VertPos := GetScrollPos(RichEdit1.Handle, SB_VERT);
    end;

    var
     Form1: PForm1;

    begin
     NewForm1(Form1, nil);
     Run(Form1.Form);
    end.
  • Vladimir Kladov © (16.01.11 09:30) [1]
    program Project1;

    uses
    Windows, Messages, KOL;

    type
    PForm1 = ^TForm1;
    TForm1 = object(TObj)
      Form: PControl;
      RichEdit1: PControl;
      Timer1: PTimer;
    public
      procedure Timer1Timer(Sender: PObj);
    end;

    var
    AutoScroll: Boolean;
    VertPos: Integer;
    var
    Form1: PForm1;

    function WndProcRichEdit(Sender: PControl; var Msg: TMsg;
    var Rslt: Integer): Boolean;
    var
    P: Integer;
    sbi: TScrollInfo;
    begin
    Result := False;
    Rslt := 0;
    case Msg.message of
      WM_VSCROLL:
        begin
          if ( LOWORD(Msg.wParam) = SB_THUMBTRACK ) then
             PostMessage( Sender.Handle, WM_USER+1111, 0, 0 );
          VertPos := GetScrollPos(Sender.Handle, SB_VERT);
        end;
      WM_USER+1111:
        begin
            sbi.cbSize := Sizeof( sbi );
            sbi.fMask := SIF_POS or SIF_RANGE or SIF_TRACKPOS or SIF_PAGE;

            GetScrollInfo( Sender.Handle, SB_VERT, sbi );
            AutoScroll := sbi.nTrackPos >= sbi.nMax - sbi.nPage;

            Form1.Form.Caption := Int2Str(sbi.nMax  - sbi.nPage) + ' ' +
                             Int2Str(sbi.nPage) + ' ' +
                             Int2Str(sbi.nPos) + ' ' +
                             Int2Str(sbi.nTrackPos) + ' ' +
                             Int2Str( Integer(AutoScroll) );

            {P := HIWORD(Msg.wParam);
            AutoScroll := (VertPos > P - 10) and (VertPos < P + 10);}

        end;
    end;
    end;

    procedure NewForm1(var Result: PForm1; AParent: PControl);
    var
    Style: Integer;
    begin
    New(Result, Create);
    with Result^ do
    begin
      Form := NewForm(AParent, 'Project1');
      Form.Add2AutoFree(Result);
      Applet := Form;
      Form.SetClientSize(320, 240).CenterOnParent;

      RichEdit1 := NewRichEdit( Form, [eoNoHScroll, eoReadonly] );
      RichEdit1.SetAlign(caClient);
      {
      Style := GetWindowLong(RichEdit1.GetWindowHandle, GWL_STYLE);
      SetWindowLong(RichEdit1.Handle, GWL_STYLE, Style and not ES_AUTOVSCROLL);
      }

      RichEdit1.AttachProc(WndProcRichEdit);

      Timer1 := NewTimer(1000);
      Timer1.OnTimer := Timer1Timer;
      Timer1.Enabled := True;
    end;
    end;

    procedure TForm1.Timer1Timer(Sender: PObj);
    var
    I: Integer;
    begin
    if not AutoScroll then
      I := RichEdit1.SelStart;

    RichEdit1.SelStart := RichEdit1.TextSize;
    RichEdit1.RE_FmtFontColor := clGray;
    RichEdit1.RE_FmtBold := False;
    RichEdit1.RE_Append( Time2StrFmt('HH:mm:ss', Now) + ' ', False );
    RichEdit1.RE_FmtFontColor := clBlack;
    RichEdit1.RE_FmtBold := True;
    RichEdit1.RE_Append('Nick ', False);
    RichEdit1.RE_FmtFontColor := clNavy;
    RichEdit1.RE_FmtBold := False;
    RichEdit1.RE_Append('Message' + sLineBreak, False);

    if not AutoScroll then
      RichEdit1.SelStart := I;
    if AutoScroll then RichEdit1.Perform(EM_SCROLL, SB_BOTTOM, 0);

    VertPos := GetScrollPos(RichEdit1.Handle, SB_VERT);
    end;

    begin
    NewForm1(Form1, nil);
    Run(Form1.Form);
    end.

  • Гость (22.01.11 01:19) [2]
    Спасибо большое, Владимир! :)
 
Конференция "KOL" » RichEdit и автопрокрутка [Delphi, Windows]
Есть новые Нет новых   [120347   +17][b:0][p:0.002]