Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

pierrotsc (Programmer) (OP)
28 Oct 11 10:41
I have a spinedit that has integer values from 0 to 100. I would like when i press and hold the right mouse button for the spinedit to increase by one and decrease by one when i move the mouse to the left still holding down the right mouse button.
I have an onmousedown and onmousemove in the image component i am using from Imageen Library.
Would someone has an idea on how I could do that?
thanks.
P
Helpful Member!  majlumbo (Programmer)
28 Oct 11 11:13

CODE

type
   TForm1 = Class(TForm)
     ..
   private
      fStartX, fStartY: Integer;
      fRightMouseDown: Boolean;
   end;


procedure TForm1.FormCreate(Sender: TObject);
begin
   ...
   fRightMouseDown := False;
end;

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if Button = mbRight then
   begin
      fStartX := X;
      fStartY := Y;
      fRightMouseDown := True;
   end;
end;

procedure TForm1.Image1MouseUP(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   fRigthMouseDown := False;
end;

procedure TForm1.ImageMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
begin
   if fRightMouseDown then
   begin
      if X < fStartX then
         //moved to the left
      else if X > fStartX then
         //moved to the right
      //if X is the same as fStartX then the mouse was moved up or down
   end;
end;
hopefully this works, but haven't tested it....
majlumbo (Programmer)
28 Oct 11 11:17
Sorry, typo..

CODE

procedure TForm1.Image1MouseUP(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   fRightMouseDown := False;
end;
pierrotsc (Programmer) (OP)
28 Oct 11 11:49
Ok, i have a small glitch.
This is my modified code for the spinedit
if fRightMouseDown then
    begin
      if X < fStartX then
      begin
        if SpinEdit_Effectamount.Value >= 0 then
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value - 1
      end
      else if X > fStartX then
      begin
        if SpinEdit_Effectamount.Value <= 100 then
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value + 1;
      end;
    end;

When i move the mouse to the right, value increases but if i move it to the left, it does not decrease right away. I will have to pass the point when i starting moving to the right for the value to start decreasing.

What do i need to modify?
Thanks.
P
majlumbo (Programmer)
28 Oct 11 12:02
I see what your saying,

CODE

if fRightMouseDown then
    begin
      if X < fStartX then
      begin
        if SpinEdit_Effectamount.Value >= 0 then
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value - 1
      end
      else if X > fStartX then
      begin
        if SpinEdit_Effectamount.Value <= 100 then
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value + 1;
      end;
      fStartX := X;<--Save the new X position
    end;
majlumbo (Programmer)
28 Oct 11 12:31
Just noticed, you'll also want to change your if statements a bit

CODE

if fRightMouseDown then
    begin
      if X < fStartX then
      begin
        if SpinEdit_Effectamount.Value > 0 then//change to greater than
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value - 1
      end
      else if X > fStartX then
      begin
        if SpinEdit_Effectamount.Value < 100 then//change to less than
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value + 1;
      end;
      fStartX := X;<--Save the new X position
    end;
pierrotsc (Programmer) (OP)
28 Oct 11 13:43
Works great...Thanks. If i another spinedit. Could i alter its value too by moving the mouse up and down (holding the right button down)?
P
majlumbo (Programmer)
28 Oct 11 13:57
If you want both spinedits to respond to the changes in mouse position at the same time, then just add:

CODE

if fRightMouseDown then
    begin
      if X < fStartX then
      begin
        if SpinEdit_Effectamount.Value > 0 then//change to greater than
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value - 1
      end
      else if X > fStartX then
      begin
        if SpinEdit_Effectamount.Value < 100 then//change to less than
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value + 1;
      end;
      if  Y < fStartY then
         //update the other spin edit down 1
      else if X < fStartY then
         //update the other spin edit up 1
      fStartX := X;
      fStartY := Y;
    end;

If on the other hand you only want one spin edit to change value at a time - only SpinEdit_Effectamount or the other spin edit but not both, then you're going to need some other mechanism to figure out which one you want to alter.
majlumbo (Programmer)
28 Oct 11 13:59
Need an edit capability.. Another typo

CODE

      if  Y < fStartY then
         //update the other spin edit down 1
      else if Y > fStartY then
         //update the other spin edit up 1
pierrotsc (Programmer) (OP)
28 Oct 11 14:12
ok, i have not made the latest modifications because i just realized that moving the mouse up and down modifies the spinedit too. i guess there's no way you can keep your mouse straight to keep the x constant. so if i move my mouse up but a little bit to the right, then the value changes.

it may be hard to do though. if i check the y values and they are increasing, it could also mean that i am moving my mouse to the right with a small incline upward.

What do you think?
p
majlumbo (Programmer)
28 Oct 11 14:40
Absolutely, the check is if the mouse's X position changed by any amount, so even if you move it up and down, there will probably be some small but measurable change in the X position also.

You can possibly add a small fudge factor that the mouse must move by before you update the spin edits.

CODE

type
   TForm1 = class(TObject)
    ...
   end;

const
   delta = 5;



if fRightMouseDown then
    begin
      if X < fStartX - delta then
      begin
        if SpinEdit_Effectamount.Value > 0 then
          SpinEdit_Effectamount.Value := SpinEdit_Effectamount.Value - 1;
         fStartX := X;
      end
      else if X > fStartX + delta then
      begin
        if SpinEdit_Effectamount.Value < 100 than
          SpinEdit_Effectamount.Value :=  SpinEdit_Effectamount.Value + 1;
        fStartX := X;
      end;
      if  Y < fStartY - delta then
      begin
         if Other_SpinEdit.Value < 100 than
            Other_SpinEdit_Effectamount.Value := Other_SpinEdit.Value - 1;
         fStartY := Y;
      end
      else if Y > fStartY + delta then
      begin
         if Other_SpinEdit.Value < 100 than
            Other_SpinEdit_Effectamount.Value := Other_SpinEdit.Value + 1;
         fStartY := Y;
      end
    end;

Play with the value of Delta until you have the effect you want
 
majlumbo (Programmer)
28 Oct 11 14:45
dang typos...

CODE

if  Y < fStartY - delta then
begin
   if Other_SpinEdit.Value > 0 then
      Other_SpinEdit_Effectamount.Value := Other_SpinEdit.Value - 1;
      fStartY := Y;
   end
   else if Y > fStartY + delta then
   begin
      if Other_SpinEdit.Value < 100 than
         Other_SpinEdit_Effectamount.Value := Other_SpinEdit.Value + 1;
      fStartY := Y;
   end
pierrotsc (Programmer) (OP)
28 Oct 11 15:08
Well appreciated...works like i wanted.would you happen to know what to modify in my onkeypress event if i want to hold the alt key at the same time i am holding down the right mouse button?
majlumbo (Programmer)
28 Oct 11 15:40
You don't have to do that in the onkeypress event, the mousedown event also provides the status of which (if any) of the shift keys are pressed.

CODE

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if ((Button = mbRight) and (ssAlt in Shift)) then
   begin
      fStartX := X;
      fStartY := Y;
      fRightMouseDown := True;
   end;
end;

Like this, Alt key must also have been down when you clicked the right right mouse button (although it does not have to be maintained down to continue to work.)  

If you want releasing the Alt key to also terminate this action, then add a KeyUp Event

CODE

procedure TForm1.Image1KeyUp(Sender: TObject; var Key: Word; Shift: TShiftState);
begin
   if ((fRightMouseDown) and (not(ssAlt in Shift))) then
      fRightMouseDown := False
end;

procedure TForm1.Image1MouseUP(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   if fRigthMouseDown then
      fRigthMouseDown := False;
end;

This way, releasing Alt, or releasing the right mouse button will terminate the action.
pierrotsc (Programmer) (OP)
28 Oct 11 15:58
It works perfectly. Would it be possible if i release the alt key by mistake while holding down the right mouse button and i press it back for the process to start again? (without to have to relesae the right-mouse button)
Do you see what i mean?
Thanks.
P
majlumbo (Programmer)
28 Oct 11 16:39
That makes it a bit messy, there you'd rely on the keydown event (repressing the Alt Key) which does not have the current position of the mouse, so you'd need to get it via Mouse.CursorPosition and change that position which is relative to the screen via screentoclient() so that it is relative to the form.

You'd also need to track the fact that you were within the event, but didn't release the mouse with another flag, but mostly, I don't think that would really comply with the way most Windows applications work, which if you release the button, the action is terminated is standard.

And in my mind, I can see where this introduces the possibility that the different flags (fRightMouseDown and a second to signify if the Alt was mistakenly released) can end up in an undefined state relative to each other.
pierrotsc (Programmer) (OP)
28 Oct 11 16:47
Ok...I get it...That's ok. I was trying too much..Everything is good now.
Thanks for all the help.
P
majlumbo (Programmer)
28 Oct 11 16:47
Anytime..

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close