各位老师好:
我想实现如下功能:
当鼠标在listbox窗体上滑动时使滑过的文字高亮显示,我在listbox控件的Onmousemove(鼠标移动)事件中写了如下代码:
listbox1.itemindex:=y div 20;一个listbox窗体中有许多行文字,一个窗体内显示不完,如只能显示10行。如果鼠标在listbox窗体内的头10行滑动,则listbox中的文字高亮显示,可如果文字超过了第10行,则不能高亮显示,高亮显示的文字又变成了listbox窗体的前10行。我研究了很长时间这个问题,不能解决。望各位老师不吝指点。

解决方案 »

  1.   


    procedure TForm4.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
      FRect: TRect;
    begin
      FRect := ListBox1.ItemRect(Index);
      if odHotLight in State then
      begin
        ListBox1.Canvas.Brush.Color := clLime;
        ListBox1.Canvas.FillRect(FRect);
        ListBox1.Canvas.TextOut(FRect.Left, FRect.Top, ListBox1.Items[Index]);
      end else if odSelected in State then
      begin          
        ListBox1.Canvas.Brush.Color := clBlue;
        ListBox1.Canvas.FillRect(FRect);
        ListBox1.Canvas.DrawFocusRect(FRect);
        ListBox1.Canvas.TextOut(FRect.Left, FRect.Top, ListBox1.Items[Index]);
      end else
      begin
        ListBox1.Canvas.Brush.Color := clWhite;
        ListBox1.Canvas.FillRect(FRect);
        ListBox1.Canvas.TextOut(FRect.Left, FRect.Top, ListBox1.Items[Index]);
      end;
    end;procedure TForm4.ListBox1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    var
      FRect: TRect;
      I, FItem: Integer;
      State: TOwnerDrawState;
    begin
      FItem := ListBox1.ItemAtPos(Point(X, Y), True);
      if FItem > -1 then
      begin
        FRect := ListBox1.ItemRect(FItem);
        for I := 0 to ListBox1.Count - 1 do
        begin
          State := [odDefault];
          if I = FItem then
            include(State, odHotLight);
          if ListBox1.ItemIndex = I then
            include(State, odSelected);
          ListBox1DrawItem(ListBox1, I, FRect, State)
        end;
      end;
    end;