### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: ProgressBar drag detection  (Read 3573 times)

#### Pe3s

• Full Member
• Posts: 145
##### ProgressBar drag detection
« on: January 09, 2022, 12:36:40 pm »
Hello, how can I detect a drag and add events in the case of no drag and in case we detect a drag?
Code: Pascal  [Select][+][-]
1. procedure TForm1.volumeProgressMouseMove(Sender: TObject; Shift: TShiftState;
2.   X, Y: Integer);
3. begin
4.   if ssLeft in Shift then
5.   begin
6.     newPos := Round(X * Progress.MaxValue / Progress.ClientWidth);
7.     Progress.Value := newPos;
8.   end;
9. end;
10.

#### GetMem

• Hero Member
• Posts: 3741
##### Re: ProgressBar drag detection
« Reply #1 on: January 09, 2022, 12:59:31 pm »
@ Pe3s
Quote
Hello, how can I detect a drag and add events in the case of no drag and in case we detect a drag?
It's not clear(at least for me) what are you trying to achieve. You wish to drag the Progressbar over another control or you wish to move the progress from 30% to 60%?

#### GetMem

• Hero Member
• Posts: 3741
##### Re: ProgressBar drag detection
« Reply #2 on: January 09, 2022, 03:04:23 pm »
@jamie

Quote
he wants to use it like a TrackBar.
Thanks. I don't think using a progressbar as a trackbar is a good idea, especially on windows where since vista there is an animation bug.

@Pe3s
Try something like this:
Code: Pascal  [Select][+][-]
1. procedure TForm1.pbMouseMove(Sender: TObject; Shift: TShiftState; X,
2.   Y: Integer);
3. var
4.   ProgressX: Integer;
5. begin
6.   if FMouseDown and FCanDrag then
7.   begin
8.     Screen.Cursor := crCross;
9.     ProgressX := Round(X*100/pb.Width);
10.     StepIt(ProgressX);
11.     Caption := IntToStr(ProgressX) + '    ' + IntToStr(X);
12.     Exit;
13.   end;
14.   ProgressX := Round(pb.Width*pb.Position/100);
15.   FCanDrag := (X - 5 <= ProgressX) and (ProgressX < X + 5);
16.   if FCanDrag  then
17.     Screen.Cursor := crCross
18.   else
19.     Screen.Cursor := crDefault;
20. end;
21.
22. procedure TForm1.pbMouseDown(Sender: TObject; Button: TMouseButton;
23.   Shift: TShiftState; X, Y: Integer);
24. begin
25.   if FCanDrag then
26.     FMouseDown := True;
27. end;
28.
29. procedure TForm1.pbMouseLeave(Sender: TObject);
30. begin
31.   Screen.Cursor := crDefault;
32. end;
33.
34. procedure TForm1.pbMouseUp(Sender: TObject; Button: TMouseButton;
35.   Shift: TShiftState; X, Y: Integer);
36. begin
37.   FMouseDown := False;
38. end;
39.
40. procedure TForm1.FormCreate(Sender: TObject);
41. begin
42.   pb.Position := 45;
43. end;

#### Pe3s

• Full Member
• Posts: 145
##### Re: ProgressBar drag detection
« Reply #3 on: January 09, 2022, 03:51:02 pm »
I meant something like that, if we start to drag the bar, for example, pause, if we stop dragging the bar but do not release the left mouse button, we start playing

#### Pe3s

• Full Member
• Posts: 145
##### Re: ProgressBar drag detection
« Reply #4 on: January 09, 2022, 08:05:34 pm »
I don't understand how to do it.
I have a timer
i want when I move the mouse bar
Code: Pascal  [Select][+][-]
1. Timer.enabled: = False;
but how do I stop moving the bar
Code: Pascal  [Select][+][-]
1. Timer.enabled: = True;

#### Pe3s

• Full Member
• Posts: 145
##### Re: ProgressBar drag detection
« Reply #5 on: January 11, 2022, 06:01:27 pm »
@Jamie Thanks for the explanation