Search code examples
delphivclwin32guitrackbar

How to reverse Delphi's TTrackbar.Orientation values?


When a TTrackbar.Orientation property is set to trVertical, the Min value is on top and the Max is on the bottom:

trackbar showing position values on top and bottom

How can I invert the Min/Max positions? If I would like to use a TrackBar in vertical orientation for a Volume control for audio output, for instance, I would need the Min on the bottom and the Max on the top.


Solution

  • There is no built in way to do that. But you could reverse the min-max value by code:

    volume := (trackbar.Max - trackBar.Position) + trackBar.Min;
    

    Regarding the presentation of the ToolTip value, @Victoria kindly provides a solution that intercepts the TTN_NEEDTEXT windows message, and corrects the text output:

    uses
      Winapi.CommCtrl;
    
    type
      TTrackBar = class(Vcl.ComCtrls.TTrackBar)
      private
        procedure WMNotify(var Msg: TWMNotify); message WM_NOTIFY;
      end;
    
    implementation
    
    procedure TTrackBar.WMNotify(var Msg: TWMNotify);
    begin
      if Msg.NMHdr.code = TTN_NEEDTEXTW then
      begin
        PToolTipTextW(Msg.NMHdr)^.hInst := 0;
        PToolTipTextW(Msg.NMHdr)^.lpszText := 
          PChar('Position: ' + IntToStr((Max - Position) + Min));
      end
      else
        inherited;
    end;