Mega Code Archive

 
Categories / Delphi / Hardware
 

Set the Progress Bar Position Using Mouse in Delphi

Title: Set the Progress Bar Position Using Mouse in Delphi By default, the TProgressBar control is used to provide visual feedback about the progress of a procedure within an application. Programmatically, a developer can set Min and Max properties to specify the lower and the upper limit of the range of possible positions. The Position property (or the Step, StepBy methods) is then used to set the current position of the progress bar. If you want to enable the users of you application to set the position of the progress bar visually, using mouse, you can use the next trick: //ProgressBar1 OnMouseMove procedure TForm1.ProgressBar1MouseMove( Sender: TObject; Shift: TShiftState; X, Y: Integer) ; var newPosition : integer; begin if ssShift in Shift then begin ProgressBar1.Cursor := crHSplit; newPosition := Round(x * ProgressBar1.Max / ProgressBar1.ClientWidth) ; ProgressBar1.Position := newPosition; end else begin ProgressBar1.Cursor := crDefault; end; end; The OnMouseMove event for the TProgressBar component named ProgressBar1 is handled, when the mouse is over the ProgressBar control, and the Shift key is pressed, a user can dynamically change the value of the Position property. Note: the code above assumes that "pbHorizontal" is set for the Orientation property of the ProgressBar - the "X" parameter is used to calculate the new Position value. If "pbVertical" is specified, the "Y" parameter should be used.