Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Для новичков > Выделить название в медиа плеере


Автор: Elays 2.2.2009, 18:22
Господа, решил сделать Цвой небольшой мп3 проигрователь,
(точней подредактировать скаченный исходник)

но вот незадача незнаю как сделать чтобы проирговающийся трек был выделен другим цветом

и какую функцию вообще использовать чтобы определить выделенную строку в ListBox??

Плиз хелп.....


Автор: StingWINg 3.2.2009, 10:22
ListBox1.Selected[index: integer]:boolean.
Если она = true, то строка выделена и наоборот.

Автор: Alexeis 3.2.2009, 10:30
  У листбокса есть обработчик OnDrawItem, его можно перекрыть и выводить текст самостоятельно, например при помощи API функции DrawText. Но чтобы он работал нужно установить Style в lbOwnerDrawFixed.


Автор: Alexeis 3.2.2009, 10:47
  Заодно накидал пример

Код

procedure TForm1.FormCreate(Sender: TObject);
begin
  ListBox1.Style := lbOwnerDrawFixed;
  ListBox1.AddItem('item1', nil);
  ListBox1.AddItem('item2', nil);
  ListBox1.AddItem('item3', nil);
  ListBox1.AddItem('item4', nil);
  ListBox1.AddItem('item5', nil);
  ListBox1.AddItem('item6', nil);
end;

var playingItem : integer = 2;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
begin
  if (Control is TListBox)
  then
    With Control as TListBox do
    try
        Canvas.Pen.Color := clWhite;
        Canvas.Rectangle(Rect);
        if (Index = playingItem - 1)
        then
          Canvas.Font.Color := clRed
        else
          Canvas.Font.Color := clBlack;
        DrawText(Canvas.Handle, PChar(Items[Index]), Length(Items[Index]),
                 Rect, DT_LEFT or DT_SINGLELINE or DT_VCENTER);
    finally end;
  State := [odNoFocusRect];
end;


Автор: Rrader 3.2.2009, 11:04
Код

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  with ListBox1, Items do
  begin
    Style := lbOwnerDrawFixed;
    Append('First');
    Append('Second');
    Append('Third');
  end;
end;

procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
  Rect: TRect; State: TOwnerDrawState);
var
  Flags: Longint;
  S: String;
begin
  with (Control as TListBox), Canvas do
  begin
    { Условие определяет, какие итемы выделять }
    if odSelected in State then
      Brush.Color := clRed;
    FillRect(Rect);
    Flags := DrawTextBiDiModeFlags(DT_SINGLELINE or DT_VCENTER or DT_NOPREFIX);
    if not UseRightToLeftAlignment then
       Inc(Rect.Left, 2)
     else
       Dec(Rect.Right, 2);
    S := Items[Index];
    DrawText(Canvas.Handle, PChar(S), Length(S), Rect, Flags);
  end;
end;

end.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)