Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Right-Mouse Behavior 1

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
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
 
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....
 
Sorry, typo..

Code:
procedure TForm1.Image1MouseUP(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
   fRightMouseDown := False;
end;
 
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
 
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;
 
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;
 
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
 
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.
 
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
 
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
 
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
 
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
 
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?
 
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.
 
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
 
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.
 
Ok...I get it...That's ok. I was trying too much..Everything is good now.
Thanks for all the help.
P
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top