Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

syncing mouse pointer & scrollbar movement

Posted on 1997-05-16
7
Medium Priority
?
293 Views
Last Modified: 2010-04-04
Hello,

I have an image in a scrollbox and I have code in place to move the image if the user "grabs" it.  My porblem is that I want to syncrhonize the mouse pointer and the movement of my image [much like adobe acrobat reader] so that as I move the image the mouse pointer remains in the same position as it started.  I've tried to speed up the image and just about had it but then the image got very jumpy because I was making it move to fast.
0
Comment
Question by:wilee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 3

Expert Comment

by:sperling
ID: 1336352
Not entirely sure what you're asking, but if I understand your question, this unit does what you wan't.

-------------
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure Image1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    FOffsetX,
    FOffsetY  : INTEGER;
    FMoving   : BOOLEAN;
  public
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if Button=mbLeft then begin
    FOffsetX := X;
    FOffsetY := Y;
    FMoving := TRUE;
  end;
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  pt : TPoint;
begin
  if FMoving then begin
    pt := Image1.ClientToScreen(Point(x, y));
    pt := ScreenToClient(pt);
    Image1.SetBounds(pt.X - FOffsetX, pt.Y-FOffsetY, Image1.Width, Image1.Height);
  end;
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if FMoving and (Button = mbLeft) then begin
    FMoving := FALSE;
  end;
end;

end.
-------------


Regards,

Erik.
0
 

Author Comment

by:wilee
ID: 1336353
It works great except the image flickers terribly.  And I've tried to make it smaller and it still flickers.  If there is a way to get rid of the flicker this will work splendidly.  But to have the image flciker is totally unacceptable.  And I'm running a cyrus 686 200 Mhz machine so if this can't redraw it my user machines sure won't.
Just fix the flicker and I'll give you an A Thanks.
wilee
0
 

Author Comment

by:wilee
ID: 1336354
To whom it may concern,

I have figured the problem out myself.  I simply took the code I have and inside my mouse down event handler I just set the X & Y values for the mouse to my origin point.

Thanks for the help and I'd be glad to give you 50 points cause you were on the right track just e-mail me back and we can discuss it.

wilee
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 3

Accepted Solution

by:
sperling earned 300 total points
ID: 1336355
A "standard" method to fix flickering is to handle the WM_ERASEBKGND message...

In protected part of an TImage ancestor:
  procedure WMEraseBkgnd(var Message: TWMEraseBkgnd);
    message WM_ERASEBKGND;

Implemented as:

procedure TNoFlickerImage.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
  Message.Result := 1;
end;


This'll probably fix the flickering. Or had you already fixed it?


Regards,

Erik.
0
 

Author Comment

by:wilee
ID: 1336356
Could you explain a little bit behind what each line of code does in the first section.  Actually what the 3 lines of code in the on mouse move event do?  I fixed mine by actually manipulating the scrollbars in a scrollbox to move my image.  I had to do some intricate coding to actually revers the scroll bars.  When you move a scroll bar down the image moves up.  Then I had to reset the X & Y coordinates of the mouse in the onmousemove event handler.  But yours is much more efficient and simple.  But I'd like to understand how it works.  Thanks for your help.
0
 
LVL 3

Expert Comment

by:sperling
ID: 1336357
All visible controls has a ClientToScreen and ScreenToClient method. These converts any TPoint between the coordinate system of the control and the coordinate system of the entire screen.

pt := Image1.ClientToScreen(Point(x, y));

Convert X and Y, which are relative to the upper left corner of Image1, to the coordinate system of the screen.

E.g. if Form1.Left = 100 and Image1.Left = 50:

X = 20 in Image1's coordinate system -> X=100+50+20 = 170 in screens coordinate system.

pt := ScreenToClient(pt);  

This converts the screen x and y back to the form's coordinate system. The result of these two lines is the X and Y coordinate passed to the event, but relative to the forms upper left corner. The X from before would now be 70.

Image1.SetBounds(pt.X - FOffsetX, pt.Y-FOffsetY, Image1.Width, Image1.Height);

The offset values are simply the position of the mouse pointer relative to the images top left corner. The X (which is now relative to the Forms left edge) is reduced by the amount of pixels from the images left edge to the initial position of the mouse on the image, giving the new position for the images left edge, relative to the form's left edge.

Setting Left first, and Top afterwards, requires the Image to redraw twice.

SetBounds sets left, top, width and height all in once. This means that the Image won't have to redraw after each of these values are changed.


Regards,

Erik.
0
 

Author Comment

by:wilee
ID: 1336358
Thanks for all your help.  I'm using your solution instead of mine just cause like I said it is easier to understand.

Jeff
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question