Recent

Author Topic: Custom Trackbar  (Read 5802 times)

DmedZ

  • New Member
  • *
  • Posts: 41
Custom Trackbar
« on: November 23, 2015, 12:23:38 pm »
So ive been trying to create a custom Trackbar for a music player to track the progress, duration, fastforward...etc. I tried using a panel, placing an image on the panel and changing the Image.Left value to match the duration of the song but that hasnt worked so what i want to know is:
 
1. Is there a way to create a custom Trackbar?
2. Can i change the default Lazarus Trackbars appearance?
3. Is it possible to use a BGRAProgressbar as a substitute: Problem with this is it doesnt have a OnChange value. Is there a work around for this?

i attached an image for you to give you an example of this.

DmedZ

  • New Member
  • *
  • Posts: 41
Re: Custom Trackbar
« Reply #1 on: November 23, 2015, 03:35:01 pm »
Anyone?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Custom Trackbar
« Reply #2 on: November 24, 2015, 08:12:42 am »
I don't understand what exactly you ask here so I'm going to default to a technical direction. If by any chance you want something different please clarify.

There are 2 major group of visual controls TGraphiControl descendants and TWinControl descendants. The main difference is that TGraphicControl descendants
are a light weight visual control it needs a parent to function while a TWinControl descendant is the full blown thing that represents an OS surface that can handle everything from mouse and keyboard to system events everything you might need. In this case and because the most important part of your questions seems to be custom drawing of the control I recommend to use a TGraphicControl descendant. This gives you the ability to have a transparent control easily and full control on drawing and mouse handling. to take over the drawing you override the paint method to manage the mouse you override the methods mousedown, mouseup and mousemove and do your processing there. If you need to redraw the control after the properties have changed you call its invalidate method flagging it as "needs repaint" for the system. Here is a very basic control that paints it sel and handles mouse events to get you started. If I missed anything fill free to clarify as you see fit.

Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Controls, Graphics;
  3.  
  4. type
  5.  
  6.   { TMyTracker }
  7.  
  8.   TMyTracker = class(TGraphicControl)
  9.   private
  10.     FmouseDown :Boolean;
  11.     FMaxX      :Integer;
  12.   protected
  13.     function Constrain(aValue, aMin, aMax:Integer):Integer; //always inclusive.
  14.   public
  15.     procedure MouseDown(Button :TMouseButton; Shift :TShiftState; X, Y :Integer);override;
  16.     procedure MouseMove(Shift :TShiftState; X, Y :Integer);                      override;
  17.     procedure MouseUp(Button :TMouseButton; Shift :TShiftState; X, Y :Integer);  override;
  18.     procedure Paint;                                                             override;
  19.     constructor Create(aOwner :TComponent);                                      override;
  20.  
  21.     property OnMouseMove;
  22.     property OnMouseDown;
  23.     property OnMouseUp;
  24.     property OnMouseEnter;
  25.     property OnMouseLeave;
  26.   end;
  27.  
  28.  
  29. implementation
  30.  
  31. uses math;
  32.  
  33. { TMyTracker }
  34.  
  35. function TMyTracker.Constrain(aValue, aMin, aMax :Integer) :Integer;
  36. begin
  37.   Result := Max(aMin, Min(aMax, aValue));
  38. end;
  39.  
  40. procedure TMyTracker.MouseDown(Button :TMouseButton; Shift :TShiftState; X, Y :Integer);
  41. begin
  42.   inherited MouseDown(Button, Shift, X, Y);
  43.   FMouseDown := True;
  44. end;
  45.  
  46. procedure TMyTracker.MouseMove(Shift :TShiftState; X, Y :Integer);
  47. begin
  48.   inherited MouseMove(Shift, X, Y);
  49.   if FMouseDown then begin
  50.     FMaxX       := Constrain(X, 1, Width-1);
  51.     Invalidate;
  52.   end;
  53. end;
  54.  
  55. procedure TMyTracker.MouseUp(Button :TMouseButton; Shift :TShiftState; X, Y :Integer);
  56. begin
  57.   FMouseDown := False;
  58.   FMaxX      := Constrain(X, 1, Width-1);
  59.   Invalidate;
  60. end;
  61.  
  62. procedure TMyTracker.Paint;
  63. begin
  64.   inherited Paint;
  65.   Canvas.Brush.Style := bsClear;
  66.   Canvas.Pen.Color   := clGreen;
  67.   Canvas.Rectangle(ClientRect);
  68.   Canvas.Brush.Color := clSkyBlue;
  69.   Canvas.Brush.Style := bsSolid;
  70.   Canvas.Pen.Style   := psClear;
  71.   Canvas.Rectangle(1, 1, FMaxX, Height-1);
  72.   Canvas.Pen.Style   := psSolid;
  73. end;
  74.  
  75. constructor TMyTracker.Create(aOwner :TComponent);
  76. begin
  77.   inherited Create(aOwner);
  78.   FMaxX := Width div 2;
  79. end;
  80.  
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: Custom Trackbar
« Reply #3 on: November 24, 2015, 09:03:16 am »
Just write your own TGraphicControl, it's not that hard.

Example of such a slider (TTrackBar like class) I wrote:

http://cache.getlazarus.org/images/cpu-graph-sizes-small.png

eny

  • Hero Member
  • *****
  • Posts: 1634
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

sysrpl

  • Sr. Member
  • ****
  • Posts: 315
    • Get Lazarus
Re: Custom Trackbar
« Reply #5 on: November 24, 2015, 05:01:00 pm »
envy, that documentation is very old. i built new documentation last friday. additoinally i created an incremental search index.

ublock origin warns of malware by default at sourceforge:

http://cache.getlazarus.org/images/sourceforge-malware.png

wikipeida details sourceforge hijackings:

http://cache.getlazarus.org/images/sourceforge-hijack.png
« Last Edit: November 24, 2015, 05:34:30 pm by sysrpl »

DmedZ

  • New Member
  • *
  • Posts: 41
Re: Custom Trackbar
« Reply #6 on: November 24, 2015, 05:47:48 pm »
Hey Taazz

Im not sure if you've seen modern mp3 player trackbars its usually a line with white filling up the bar to show the duration of the song. The best example would be the windows 10 mp3 player (check out the trackbar in the picture). Like i said i  the effect can be accomplished with BGRAProgressbar but i just dont know how to do it.

Quote
procedure TForm1.MPlayerControl1Play(Sender: TObject);
begin
  TrackBar1.Max := Trunc(MPlayerControl1.Duration);
end;
 
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  MPlayerControl1.Position := TrackBar1.Position;
end; 

procedure TForm1.MPlayerControl1Playing(ASender: TObject; APosition: Single);
begin
  TrackBar1.OnChange := nil;
  TrackBar1.Position := Trunc(APosition);
  TrackBar1.OnChange := @TrackBar1Change;
end;


This is the code i use on the trackbar to make it move to a song when its played.

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Custom Trackbar
« Reply #7 on: November 24, 2015, 09:31:46 pm »
Hey Taazz

Im not sure if you've seen modern mp3 player trackbars its usually a line with white filling up the bar to show the duration of the song. The best example would be the windows 10 mp3 player (check out the trackbar in the picture). Like i said i  the effect can be accomplished with BGRAProgressbar but i just dont know how to do it.
Yet again you are not clear of what you do not know how to do. Do you not know how to paint a line to calculate the line's size based on a Max and current value? You do not know how to use the mouse to control the songs position?
Quote
procedure TForm1.MPlayerControl1Play(Sender: TObject);
begin
  TrackBar1.Max := Trunc(MPlayerControl1.Duration);
end;
 
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
  MPlayerControl1.Position := TrackBar1.Position;
end; 

procedure TForm1.MPlayerControl1Playing(ASender: TObject; APosition: Single);
begin
  TrackBar1.OnChange := nil;
  TrackBar1.Position := Trunc(APosition);
  TrackBar1.OnChange := @TrackBar1Change;
end;


This is the code i use on the trackbar to make it move to a song when its played.
Yeah my over simplified control was to get you started on writing your own tracker with what ever paint requirements you need. You have to add your own properties and logic to it.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

 

TinyPortal © 2005-2018