home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 September / Chip_2001-09_cd1.bin / zkuste / delphi / kolekce / d56 / RMCTL.ZIP / rmTrackBar.pas < prev    next >
Pascal/Delphi Source File  |  2001-06-22  |  26KB  |  741 lines

  1. {================================================================================
  2. Copyright (C) 1997-2001 Mills Enterprise
  3.  
  4. Unit     : rmTrackBar
  5. Purpose  : An enhanced Trackbar allowing for multiple new styles
  6. Date     : 12-01-1998
  7. Author   : Ryan J. Mills
  8. Version  : 1.80
  9. ================================================================================}
  10.  
  11. unit rmTrackBar;
  12.  
  13. interface
  14.  
  15. {$I CompilerDefines.INC}
  16.  
  17. uses
  18.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, extctrls,
  19.   rmLibrary;
  20.  
  21. type
  22.   TMarkPosition = (mpTopLeft, mpBottomRight, mpNone);
  23.   TTrackOrientation = (toHorizontal, toVertical);
  24.   TTrackPosition = (tpCenter, tpTopLeft, tpBottomRight);
  25.   TrmTrackBar = class(TCustomControl)
  26.   private
  27.     { Private declarations }
  28.     fTrackOrientation : TTrackOrientation;
  29.     fTrackSize : integer;
  30.     fTrackColor : TColor;
  31.     fMinValue : integer;
  32.     fMaxValue : integer;
  33.     fThumbPosition : integer;
  34.     fTrackPosition : TTrackPosition;
  35.     fPageSize : integer;
  36.     fMarkFrequency : integer;
  37.     fMarkSpacing : integer;
  38.     fMarkPosition : TMarkPosition;
  39.     fThumb : tbitmap;
  40.     fChanged : TNotifyEvent;
  41.     fMarkData : TStrings;
  42.     fmouseon : boolean;
  43.     function ptinthumb(x,y:integer):boolean;
  44.     function ptintrack(x,y:integer):boolean;
  45.     procedure SetTrackOrientation(value:TTrackOrientation);
  46.     procedure SetTrackSize(value:integer);
  47.     procedure SetTrackColor(value:TColor);
  48.     procedure SetMinValue(value:integer);
  49.     procedure SetMaxValue(value:integer);
  50.     procedure SetThumbPos(value:integer);
  51.     procedure SetTrackPosition(value:TTrackPosition);
  52.     procedure SetThumb(value:tbitmap);
  53.     procedure SetMarkData(value:TStrings);
  54.     procedure setmarkfrequency(value:integer);
  55.     procedure SetMarkPosition(value:TMarkPosition);
  56.     procedure SetPagesize(value:integer);
  57.     procedure SetMarkSpacing(value:integer);
  58.     function GetTrackRect:TRect;
  59.     function PointPosition(x,y:integer):integer;
  60.     procedure wmSetFocus(var msg:TWMSetFocus); message wm_setfocus;
  61.     procedure wmKillFocus(var msg:TWMKillFocus); message wm_killfocus;
  62.     procedure wmEraseBkGnd(var msg:TWMEraseBkgnd); message wm_erasebkgnd;
  63.     procedure wmGetDLGCode(var msg:TWMGetDLGCode); message wm_GetDLGCode;
  64.     procedure wmMouseActivate(var msg:TWMMouseActivate); message wm_MouseActivate;
  65.   protected
  66.     { Protected declarations }
  67.     procedure paint; override;
  68.     procedure KeyDown(var Key: Word; Shift: TShiftState); override;
  69.     procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  70.     procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  71.     procedure MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
  72.   public
  73.     { Public declarations }
  74.     constructor create(aowner:TComponent); override;
  75.     destructor destroy; override;
  76.   published
  77.     { Published declarations }
  78.     property align;
  79.     property Color;
  80.     property Font;
  81.     property MarkFrequency:integer read fmarkfrequency write setmarkfrequency default 1;
  82.     property MarkPosition : TMarkPosition read fMarkPosition write SetMarkPosition default mpTopLeft;
  83.     property MarkSpacing : integer read fmarkspacing write setmarkspacing default 2;
  84.     property MaxValue : integer read fMaxValue write SetMaxValue default 10;
  85.     property MinValue : integer read fminvalue write SetMinValue default 0;
  86.     property PageSize:integer read fpagesize write setpagesize default 2;
  87.     property TabStop;
  88.     property TabOrder;
  89.     property Thumb : tbitmap read fthumb write SetThumb stored true;
  90.     property ThumbPosition : integer read fThumbPosition write SetThumbPos default 0;
  91.     property TrackColor : TColor read ftrackcolor write SetTrackColor default clWindow;
  92.     property TrackOrientation : TTrackOrientation read fTrackOrientation write SetTrackOrientation default toHorizontal;
  93.     property TrackPosition : TTrackPosition read ftrackposition write SetTrackPosition default tpcenter;
  94.     property TrackSize : integer read ftracksize write SetTrackSize default 8;
  95.     property OnChange:TNotifyEvent read fchanged write fchanged;
  96.     property MarkData : TStrings read fMarkData write SetMarkData stored true;
  97.   end;
  98.  
  99. implementation
  100.  
  101. { TrmTrackBar }
  102. {$R rmTrackBar.res}
  103.  
  104. constructor TrmTrackBar.create(aowner:TComponent);
  105. begin
  106.      inherited create(aowner);
  107.      fmouseon := false;
  108.      ControlStyle := [csCaptureMouse,csClickEvents];
  109.      fThumb := tbitmap.create;
  110.      fMarkData := TStringList.Create;
  111.      height := 50;
  112.      width := 150;
  113.      fmarkspacing := 2;
  114.      fTrackOrientation := toHorizontal;
  115.      fMarkPosition := mpTopLeft;
  116.      ftracksize := 8;
  117.      ftrackcolor := clwindow;
  118.      ftrackposition := tpcenter;
  119.      fminvalue := 0;
  120.      fmaxvalue := 10;
  121.      fThumbPosition := 0;
  122.      fPageSize := 1;
  123.      tabstop := true;
  124.      fmarkfrequency := 1;
  125.      Thumb := nil;
  126. end;
  127.  
  128. destructor TrmTrackBar.destroy;
  129. begin
  130.      fMarkData.free;
  131.      fThumb.free;
  132.      inherited;
  133. end;
  134.  
  135. procedure TrmTrackBar.Paint;
  136. var
  137.    wr, wRect : TRect;
  138.    hcenter, vcenter : integer;
  139.    loop : integer;
  140.    xstart, xend, xadj, xwidth, ystart, yend, yadj, yheight, calcstep : single;
  141.    wmaxvalue, wminvalue, wmarkfrequency, wTextMeasure : integer;
  142.    newimage : tbitmap;
  143.    xspacing, yspacing : integer;
  144.    wText : string;
  145. begin
  146.      newimage := tbitmap.create;
  147.      try
  148.         newimage.height := height;
  149.         newimage.width := width;
  150.         if fmarkdata.count > 0 then
  151.         begin
  152.              wmaxvalue := fmarkdata.count-1;
  153.              wminvalue := 0;
  154.              wmarkfrequency := 1;
  155.         end
  156.         else
  157.         begin
  158.              wmaxvalue := fmaxvalue;
  159.              wminvalue := fminvalue;
  160.              wmarkfrequency := fmarkfrequency;
  161.         end;
  162.         calcstep := (wmaxvalue - wminvalue) / wmarkfrequency;
  163.         newimage.canvas.font := font;
  164.         newimage.canvas.brush.color := color;
  165.         newimage.canvas.fillrect(GetClientRect);
  166.         wr := GetTrackRect;
  167.         ystart := 0;
  168.         xstart := 0;
  169.         xwidth := 0;
  170.         yheight := 0;
  171.         hcenter := 0;
  172.         vcenter := 0;
  173.         if fTrackOrientation = tovertical then
  174.         begin
  175.              if fmarkposition = mptopleft then
  176.              begin
  177.                   xstart := wr.Left-3;
  178.                   xwidth := -5;
  179.              end
  180.              else
  181.              if fmarkposition = mpbottomright then
  182.              begin
  183.                   xstart := wr.Right+2;
  184.                   xwidth := 5;
  185.              end;
  186.              xend := xstart;
  187.              xadj := 0;
  188.              ystart :=wr.top + 2;
  189.              yend := wr.bottom - 2;
  190.              yadj := (yend - ystart) / calcstep;
  191.              yheight := 0;
  192.              hcenter := wr.left + ((wr.right-wr.left) shr 1);
  193.              if (fThumbPosition >= wminvalue) and (fThumbPosition <= wmaxvalue) then
  194.                 vcenter := round( ystart + round ( (yend-ystart) * ( (fThumbPosition - wminvalue) / (wmaxvalue - wminvalue) ) ) )
  195.              else
  196.              if (fThumbPosition < wminvalue) then
  197.                 vcenter := round(ystart)
  198.              else
  199.              if (fThumbPosition > wmaxvalue) then
  200.                 vcenter := round(yend);
  201.         end
  202.         else
  203.         begin
  204.              if fmarkposition = mptopleft then
  205.              begin
  206.                   ystart := wr.Top - 3;
  207.                   yheight := 5;
  208.              end
  209.              else
  210.              if fmarkposition = mpbottomright then
  211.              begin
  212.                   ystart := wr.Bottom + 2;
  213.                   yheight := -5;
  214.              end;
  215.  
  216.              yend := ystart;
  217.              yadj := 0;
  218.              xstart := wr.Left+2;
  219.              xend := wr.right-2;
  220.              xadj := (xend - xstart) / calcstep;
  221.              xwidth := 0;
  222.              vcenter := wr.top + ((wr.bottom-wr.top) shr 1);
  223.              if (fThumbPosition >= wminvalue) and (fThumbPosition <= wmaxvalue) then
  224.                 hcenter := round( xstart + round ( (xend-xstart) * ( (fThumbPosition - wminvalue) / (wmaxvalue - wminvalue) ) ) )
  225.              else
  226.              if (fThumbPosition > wmaxvalue) then
  227.                 hcenter := round(xend)
  228.              else
  229.              if (fThumbPosition < wminvalue) then
  230.                 hcenter := round(xstart);
  231.         end;
  232.         if fmarkposition <> mpNone then
  233.         begin
  234.              if fmarkdata.count > 0 then
  235.              begin
  236.                   if fTrackOrientation = tovertical then
  237.                   begin
  238.                        xspacing := fmarkspacing;
  239.                        yspacing := -(newimage.canvas.TextHeight('X') shr 1);
  240.                   end
  241.                   else
  242.                   begin
  243.                        if fMarkPosition = mpTopLeft then
  244.                           yStart := yStart - newimage.Canvas.TextHeight('X');
  245.                        
  246.                        xspacing := -(newimage.canvas.TextHeight('X') shr 1);
  247.                        yspacing := fmarkspacing;
  248.                   end;
  249.                   loop := 0;
  250.                   while loop < fmarkdata.count do
  251.                   begin
  252.                        wText := fmarkdata[loop];
  253.                        if fTrackOrientation = toHorizontal then
  254.                        begin
  255. {                          Case MarkRotationAngle of
  256.                              ra0, ra180 :
  257.                                 begin }
  258.                                    wTextMeasure := (newimage.Canvas.TextWidth(wText) div 2);
  259.                                    wRect.Left := (round(xstart) + xspacing) - wTextMeasure;
  260.                                    wRect.Right := (round(xstart) + xspacing) + wTextMeasure;
  261.  
  262.                                    if fMarkPosition = mpTopLeft then
  263.                                    begin
  264.                                       wRect.Top := round(ystart) - yspacing;
  265.                                       wRect.Bottom := round(ystart) - yspacing + newimage.Canvas.TextHeight(wText);
  266.                                    end
  267.                                    else
  268.                                    begin
  269.                                       wRect.Top := round(ystart) + yspacing;
  270.                                       wRect.Bottom := round(ystart) + yspacing + newimage.Canvas.TextHeight(wText);
  271.                                    end
  272. {                                end;
  273.                              ra90, ra270 :
  274.                                 begin
  275.                                    wTextMeasure := newimage.Canvas.TextHeight(wText);
  276.                                    wRect.Left := (round(xstart) + xspacing) - wTextMeasure;
  277.                                    wRect.Right := (round(xstart) + xspacing) + wTextMeasure;
  278.  
  279.                                    wRect.Top := round(ystart) + yspacing;
  280.                                    wRect.Bottom := round(ystart) + yspacing + newimage.Canvas.TextWidth(wText);
  281.                                 end;
  282.                           end;}
  283.                        end
  284.                        else
  285.                        begin
  286. {                          Case MarkRotationAngle of
  287.                              ra0, ra180 :
  288.                                 begin}
  289.                                    wTextMeasure := (newimage.Canvas.TextHeight(wText) div 2);
  290.                                    wRect.Top := round(ystart) + yspacing + wTextMeasure;
  291.                                    wRect.Bottom := round(ystart) + yspacing + wTextMeasure;
  292.  
  293.                                    if MarkPosition = mpTopLeft then
  294.                                    begin
  295.                                       wRect.Left := (round(xstart) - xspacing) - newimage.Canvas.TextWidth(wText);
  296.                                       wRect.Right := (round(xstart) - xspacing);
  297.                                    end
  298.                                    else
  299.                                    begin
  300.                                       wRect.Left := (round(xstart) + xspacing) ;
  301.                                       wRect.Right := (round(xstart) + xspacing) + newimage.Canvas.TextWidth(wText);
  302.                                    end;
  303. {                                end;
  304.                              ra90, ra270 :
  305.                                 begin
  306.                                    wTextMeasure := newimage.Canvas.TextHeight(wText);
  307.                                    wRect.Left := (round(xstart) + xspacing) - wTextMeasure;
  308.                                    wRect.Right := (round(xstart) + xspacing) + wTextMeasure;
  309.  
  310.                                    wRect.Top := round(ystart) + yspacing;
  311.                                    wRect.Bottom := round(ystart) + yspacing + newimage.Canvas.TextWidth(wText);
  312.                                 end;
  313.                           end;}
  314.                        end;
  315. //                       RotateText(wText,frotationangle,newimage.canvas,wRect);
  316.                        RotateText(newimage.canvas,wText,wRect,0);
  317.                        xstart := xstart+xadj;
  318.                        ystart := ystart+yadj;
  319.                        inc(loop);
  320.                   end;
  321.              end
  322.              else
  323.              begin
  324.                   if fTrackOrientation = toHorizontal then
  325.                   begin
  326.                        ystart := yStart - yheight;
  327.                        yend := yend - yheight;
  328.                   end;
  329.                   newimage.canvas.Pen.color := clbtntext;
  330.                   loop := 0;
  331.                   while loop < round(calcstep) do
  332.                   begin
  333.                        newimage.canvas.moveto(round(xstart), round(ystart));
  334.                        newimage.canvas.lineto(round(xstart+xwidth), round(ystart+yheight));
  335.                        xstart := xstart+xadj;
  336.                        ystart := ystart+yadj;
  337.                        inc(loop);
  338.                   end;
  339.                   newimage.canvas.moveto(round(xend), round(yend));
  340.                   newimage.canvas.lineto(round(xend+xwidth), round(yend+yheight));
  341.              end;
  342.         end;
  343.         frame3d(newimage.canvas,wr,clBtnShadow,clBtnhighlight,1);
  344.         frame3d(newimage.canvas,wr,cl3ddkshadow,cl3dlight,1);
  345.         newimage.canvas.brush.color := ftrackColor;
  346.         newimage.canvas.FillRect(wr);
  347.         fThumb.Transparent := true;
  348.         if not fThumb.Empty then
  349.         begin
  350.              newimage.canvas.draw(hcenter-(fThumb.Width shr 1),vcenter-(fThumb.height shr 1),fThumb);
  351.         end;
  352.         canvas.Draw(0,0,newimage);
  353.         if Focused then canvas.drawfocusrect(GetClientRect);
  354.      finally
  355.         newimage.free;
  356.      end;
  357. end;
  358.  
  359. procedure TrmTrackBar.SetTrackOrientation(value:TTrackOrientation);
  360. begin
  361.      if value <> fTrackOrientation then
  362.      begin
  363.           fTrackOrientation := value;
  364.           if not Thumb.transparent then
  365.              Thumb := nil;
  366.      end;
  367.      invalidate;
  368. end;
  369.  
  370. procedure TrmTrackBar.SetMarkPosition(value:TMarkPosition);
  371. begin
  372.      if value <> fMarkPosition then
  373.      begin
  374.           fMarkPosition := value;
  375.           invalidate;
  376.      end;
  377. end;
  378.  
  379. procedure TrmTrackBar.SetTrackSize(value:integer);
  380. begin
  381.      if value <> ftracksize then
  382.      begin
  383.           ftracksize := value;
  384.           invalidate;
  385.      end;
  386. end;
  387.  
  388. procedure TrmTrackBar.SetTrackColor(value:TColor);
  389. begin
  390.      if value <> ftrackcolor then
  391.      begin
  392.           ftrackcolor := value;
  393.           invalidate;
  394.      end;
  395. end;
  396.  
  397. procedure TrmTrackBar.SetMinValue(value:integer);
  398. begin
  399.      if fmarkdata.count > 0 then exit;
  400.      if value >= fmaxvalue then exit;
  401.      if value <> fminvalue then
  402.      begin
  403.           fminvalue := value;
  404.           invalidate;
  405.      end;
  406. end;
  407.  
  408. procedure TrmTrackBar.SetMaxValue(value:integer);
  409. begin
  410.      if fmarkdata.count > 0 then exit;
  411.      if value <= fminvalue then exit;
  412.      if value <> fmaxvalue then
  413.      begin
  414.           fmaxvalue := value;
  415.           invalidate;
  416.      end;
  417. end;
  418.  
  419. procedure TrmTrackBar.SetThumbPos(value:integer);
  420. begin
  421.      if fmarkdata.count > 0 then
  422.      begin
  423.           if value < 0 then value := 0;
  424.           if value >= fmarkdata.count then value := fmarkdata.count-1;
  425.      end
  426.      else
  427.      begin
  428.           if value < fminvalue then value := fminvalue;
  429.           if value > fmaxvalue then value := fmaxvalue;
  430.      end;
  431.      if value <> fThumbPosition then
  432.      begin
  433.           fThumbPosition := value;
  434.           invalidate;
  435.           if assigned(fchanged) then fchanged(self);
  436.      end;
  437. end;
  438.  
  439. procedure TrmTrackBar.SetMarkSpacing(value:integer);
  440. begin
  441.      if value <> fmarkspacing then
  442.      begin
  443.           fmarkspacing := value;
  444.           invalidate;
  445.      end;
  446. end;
  447.  
  448. procedure TrmTrackBar.SetTrackPosition(value:TTrackPosition);
  449. begin
  450.      if value <> ftrackposition then
  451.      begin
  452.           ftrackposition := value;
  453.           invalidate;
  454.      end;
  455. end;
  456.  
  457. procedure TrmTrackBar.SetPageSize(value:integer);
  458. begin
  459.      if value <= 0 then exit;
  460.      if value <> fPageSize then
  461.      begin
  462.           fPageSize := value;
  463.           invalidate;
  464.      end;
  465. end;
  466.  
  467. procedure TrmTrackBar.SetThumb(value:tbitmap);
  468. begin
  469.      if value = nil then
  470.         fThumb.LoadFromResourceName(HInstance, 'RMTRACKBAR')
  471.      else
  472.         fThumb.assign(value);
  473.         
  474.      fThumb.Transparent := true;
  475.      invalidate;
  476. end;
  477.  
  478. procedure TrmTrackBar.SetMarkData(value:TStrings);
  479. begin
  480.      if value.Count = 1 then
  481.      begin
  482.           showmessage('More than one point is required');
  483.           exit;
  484.      end;
  485.      fmarkdata.assign(value);
  486.      invalidate;
  487. end;
  488.  
  489. procedure TrmTrackBar.setmarkfrequency(value:integer);
  490. begin
  491.      if value <= 0 then exit;
  492.      if fmarkdata.count > 0 then exit;
  493.      if value <> fmarkfrequency then
  494.      begin
  495.           fmarkfrequency := value;
  496.           invalidate;
  497.      end;
  498. end;
  499.  
  500. function TrmTrackBar.GetTrackRect:TRect;
  501. var
  502.    wr : TRect;
  503.    TCenter : integer;
  504.    fVerticalIndent,
  505.    fHorizontalIndent : integer;
  506. begin
  507.      fVerticalIndent :=  GreaterThanInt(Thumb.Height, Canvas.TextHeight('X'));
  508.      fHorizontalIndent := GreaterThanInt(Thumb.Width, Canvas.TextWidth('X'));
  509.      wr := Rect(0,0,width,height);
  510.      if fTrackOrientation = tovertical then
  511.      begin
  512.           wr.top := wr.top + fVerticalindent;
  513.           wr.bottom := wr.bottom - fVerticalIndent;
  514.           case ftrackposition of
  515.           tpcenter:
  516.              begin
  517.                   tcenter := (wr.left + ((wr.right-wr.left) shr 1));
  518.                   wr.left :=  tcenter - (ftracksize shr 1);
  519.                   wr.right := tcenter + (ftracksize shr 1);
  520.              end;
  521.           tpTopLeft:
  522.              begin
  523.                   wr.left := wr.left + fHorizontalIndent;
  524.                   wr.right := wr.left + ftracksize;
  525.              end;
  526.           tpBottomRight:
  527.              begin
  528.                   wr.right := wr.right - fHorizontalIndent;
  529.                   wr.left := wr.right - ftracksize;
  530.              end;
  531.           end;
  532.      end
  533.      else
  534.      begin
  535.           wr.left := wr.left + fHorizontalIndent;
  536.           wr.right := wr.Right - fHorizontalIndent;
  537.           case ftrackposition of
  538.           tpcenter:
  539.              begin
  540.                   tcenter := (wr.top + ((wr.bottom-wr.top) shr 1));
  541.                   wr.top :=  tcenter - (ftracksize shr 1);
  542.                   wr.bottom := tcenter + (ftracksize shr 1);
  543.              end;
  544.           tpTopLeft:
  545.              begin
  546.                   wr.top := wr.top + fVerticalIndent;
  547.                   wr.bottom := wr.top + ftracksize;
  548.              end;
  549.           tpBottomRight:
  550.              begin
  551.                   wr.bottom := wr.bottom - fVerticalIndent;
  552.                   wr.top := wr.bottom - ftracksize;
  553.              end;
  554.           end;
  555.      end;
  556.      result := wr;
  557. end;
  558.  
  559. procedure TrmTrackBar.KeyDown(var Key: Word; Shift: TShiftState);
  560. begin
  561.      case key of
  562.         vk_down     :if fTrackOrientation = tovertical then ThumbPosition := ThumbPosition + 1;
  563.         vk_up       :if fTrackOrientation = tovertical then ThumbPosition := ThumbPosition - 1;
  564.         vk_right    :if fTrackOrientation = tohorizontal then ThumbPosition := ThumbPosition + 1;
  565.         vk_left     :if fTrackOrientation = tohorizontal then ThumbPosition := ThumbPosition - 1;
  566.         vk_next     :ThumbPosition := ThumbPosition + fpagesize;
  567.         vk_prior    :ThumbPosition := ThumbPosition - fpagesize;
  568.         vk_Home     :begin
  569.                           if fMarkData.count > 0 then
  570.                              ThumbPosition := 0
  571.                           else
  572.                              ThumbPosition := fminvalue;
  573.                      end;
  574.         vk_end      :begin
  575.                           if fMarkData.count > 0 then
  576.                              ThumbPosition := fmarkdata.count-1
  577.                           else
  578.                              ThumbPosition := fmaxvalue;
  579.                      end;
  580.      end;
  581. end;
  582.  
  583. procedure TrmTrackBar.wmSetFocus(var msg:TWMSetFocus);
  584. begin
  585.      msg.result := 0;
  586.      invalidate;
  587. end;
  588.  
  589. procedure TrmTrackBar.wmKillFocus(var msg:TWMKillFocus);
  590. begin
  591.      msg.result := 0;
  592.      invalidate;
  593. end;
  594.  
  595. procedure TrmTrackBar.wmEraseBkGnd(var msg:TWMEraseBkGnd);
  596. begin
  597.      msg.result := 1;
  598. end;
  599.  
  600. procedure TrmTrackBar.wmGetDLGCode(var msg:TWMGetDLGCode);
  601. begin
  602.      inherited;
  603.      msg.Result := msg.Result or DLGC_WANTARROWS;
  604. end;
  605.  
  606. procedure TrmTrackBar.wmMouseActivate(var msg:TWMMouseActivate);
  607. begin
  608.      inherited;
  609.      msg.Result := msg.result or MA_ACTIVATE;
  610.      if not (csdesigning in componentstate) then setfocus;
  611. end;
  612.  
  613. procedure TrmTrackBar.MouseMove(Shift: TShiftState; X, Y: Integer);
  614. begin
  615.      inherited;
  616.      if (csLButtonDown in controlstate) and (fmouseon) then thumbposition := pointposition(x,y);
  617. end;
  618.  
  619. procedure TrmTrackBar.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  620. var
  621.    mousepos : integer;
  622. begin
  623.      inherited;
  624.      if button = mbleft then
  625.         if ptinThumb(x,y) then
  626.         begin
  627.              thumbposition := pointposition(x,y);
  628.              fmouseon := true;
  629.         end
  630.         else
  631.         if ptinTrack(x,y) then
  632.         begin
  633.              mousepos := pointposition(x,y);
  634.              if thumbposition > mousepos then
  635.                 thumbposition := thumbposition - fpagesize
  636.              else
  637.                 thumbposition := thumbposition + fpagesize;
  638.         end;
  639. end;
  640.  
  641. procedure TrmTrackBar.MouseUp(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  642. begin
  643.      inherited;
  644.      fmouseon := false;
  645. end;
  646.  
  647. function TrmTrackBar.PointPosition(x,y:integer):integer;
  648. var
  649.    wr : TRect;
  650.    wmaxvalue, wminvalue: integer;
  651.    calcpos : integer;
  652.    pstart, pend : integer;
  653. begin
  654.      if fmarkdata.count > 0 then
  655.      begin
  656.           wmaxvalue := fmarkdata.count-1;
  657.           wminvalue := 0;
  658.      end
  659.      else
  660.      begin
  661.           wmaxvalue := fmaxvalue;
  662.           wminvalue := fminvalue;
  663.      end;
  664.      wr := GetTrackRect;
  665.      if fTrackOrientation = tovertical then
  666.      begin
  667.           pstart :=wr.top + 2;
  668.           pend := wr.bottom - 2;
  669.           calcpos := round((((y-pstart)/(pend-pstart))*(wmaxvalue - wminvalue)))+wminvalue;
  670.      end
  671.      else
  672.      begin
  673.           pstart := wr.Left+2;
  674.           pend := wr.right-2;
  675.           calcpos := round((((x-pstart)/(pend-pstart))*(wmaxvalue - wminvalue)))+wminvalue;
  676.      end;
  677.      result := calcpos;
  678. end;
  679.  
  680. function TrmTrackBar.ptinthumb(x,y:integer):boolean;
  681. var
  682.    wr : TRect;
  683.    hcenter, vcenter : integer;
  684.    xstart, xend, ystart, yend : single;
  685.    wmaxvalue, wminvalue : integer;
  686. begin
  687.      if fmarkdata.count > 0 then
  688.      begin
  689.           wmaxvalue := fmarkdata.count-1;
  690.           wminvalue := 0;
  691.      end
  692.      else
  693.      begin
  694.           wmaxvalue := fmaxvalue;
  695.           wminvalue := fminvalue;
  696.      end;
  697.      wr := GetTrackRect;
  698.      hcenter := 0;
  699.      vcenter := 0;
  700.      if fTrackOrientation = tovertical then
  701.      begin
  702.           ystart :=wr.top + 2;
  703.           yend := wr.bottom - 2;
  704.           hcenter := wr.left + ((wr.right-wr.left) shr 1);
  705.           if (fThumbPosition >= wminvalue) and (fThumbPosition <= wmaxvalue) then
  706.              vcenter := round( ystart + round ( (yend-ystart) * ( (fThumbPosition - wminvalue) / (wmaxvalue - wminvalue) ) ) )
  707.           else
  708.           if (fThumbPosition < wminvalue) then
  709.              vcenter := round(ystart)
  710.           else
  711.           if (fThumbPosition > wmaxvalue) then
  712.              vcenter := round(yend);
  713.      end
  714.      else
  715.      begin
  716.           xstart := wr.Left+2;
  717.           xend := wr.right-2;
  718.           vcenter := wr.top + ((wr.bottom-wr.top) shr 1);
  719.           if (fThumbPosition >= wminvalue) and (fThumbPosition <= wmaxvalue) then
  720.              hcenter := round( xstart + round ( (xend-xstart) * ( (fThumbPosition - wminvalue) / (wmaxvalue - wminvalue) ) ) )
  721.           else
  722.           if (fThumbPosition > wmaxvalue) then
  723.              hcenter := round(xend)
  724.           else
  725.           if (fThumbPosition < wminvalue) then
  726.              hcenter := round(xstart);
  727.      end;
  728.      wr.top := vcenter-round(fThumb.height / 2);
  729.      wr.left := hcenter-round(fThumb.Width / 2);
  730.      wr.bottom := wr.top+fThumb.height;
  731.      wr.right := wr.left+fThumb.Width;
  732.      result := ptinrect(wr,point(x,y));
  733. end;
  734.  
  735. function TrmTrackBar.ptintrack(x,y:integer):boolean;
  736. begin
  737.      result := ptinrect(gettrackrect,point(x,y));
  738. end;
  739.  
  740. end.
  741.