Zoom to the center of the mouse, just like when using Baidu maps. This approach is more consistent with people's thinking inertia than the general image center-centric scaling.
Found on the internet did not find ready-made, their own research for half a day, around to go around, dizzy. Later, the help of a group of friends to solve the problem.
Here is the code that is rewritten by the method provided by the cyber-Decepticons brothers:
Procedure Tform1.zoom (I:single); var neww, Newh:integer; Newx, Newy, SX, Sy:integer; A, B:double;begin fscale: = Fscale + Round (Fscale * I); NEWW: = Trunc (IMGW * (fscale/1000)); NEWH: = Trunc (IMGH * (fscale/1000)); SX: = Image1.left; SY: = image1.top; if (Neww < clientwidth) and (Newh < clientheight) THEN begin //image is smaller than window, center display newx: = (CLIENTWIDTH-NEWW) s HR 1; Newy: = (CLIENTHEIGHT-NEWH) shr 1; End ELSE begin //window does not display when //newx: = SX; Newy: = SY; GetCursorPos (Mouse); Mouse: = ScreenToClient (Mouse); Mouse: = Image1.clienttoparent (Mouse); A: = (MOUSE.X-SX)/image1.width; B: = (mouse.y-sy)/image1.height; NEWX: = Sx-round ((neww-image1.width) * A); Newy: = Sy-round ((newh-image1.height) * B); End; Image1.setbounds (Newx, Newy, NEWW, NEWH); end;
As you can see from the code above, first get the coordinates of the mouse in the image display client area, and then:
The relative position of the mouse before zooming X = (mouse coordinates. X-the image is scaled before the coordinates. Left)/image width;
The relative position of the mouse before zooming Y = (mouse coordinates. Y-the image is scaled before the coordinates. TOP)/Image height;
The relative position of the mouse before zooming, then using the coordinates before zooming-multiplied by the difference of the size of the scaled image (for example, the original size is a, the new size is B, the difference is b-a).
Full demo Download: Http://pan.baidu.com/s/1eQovAsa
Http://www.cnblogs.com/yangyxd/articles/3984919.html
Mouse-centric scaling of images like maps