画布/位图滚动问题
我正在尝试制作一个基于 Delphi 画布的小游戏。基本上,我想制作一个相当大的位图(例如,3000x3000),然后将其加载到画布中,并且能够像普通图像查看器一样向右/向左/向上/向下滚动,但是我不能似乎找到了我要找的东西。有什么想法吗?
I'm trying to make a small game based on the canvas in Delphi. Basically, I'd like to make a fairly large bitmap ( 3000x3000, for example ), then load it into the canvas, and being able to scroll right/left/up/down just like an ordinary image viewer, however I can't seem to find what I'm looking for. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将图像加载到屏幕外的
TBitmap
对象。然后,OnPaint
,或者只要适合您的特定应用程序,就使用BitBlt
或Canvas.Draw
绘制的矩形子图像TBitmap
到画布上。子部分应从TBitmap
上的(X, Y)
开始,并且宽度和高度等于ClientWidth
和ClientHeight 的形式,分别。
现在,响应键盘事件。编写
FormKeyDown
事件处理程序,并监听Key = VK_LEFT
、Key = VK_RIGHT
、Key = VK_UP
和Key = VK_DOWN
(使用case
语句)。当您检测到按下此类键时,根据需要增加/减少X
或Y
,并使用此起点再次绘制场景。您还可以响应
MouseDown
、MouseMove
和MouseUp
事件以使用鼠标滚动。您可以只使用中间的一个(MouseMove
):您可以检查光标是否靠近表单边缘,如果是,则沿该方向平滑滚动(使用TTimer< /code>,例如)。或者,您可以在
MouseDown
中将FMouseDown
标志设置为true
,并在中将其重置为
。然后,在false
鼠标按下MouseMove
中,如果FMouseDown
为true
,则将位图沿 x 方向滚动增量X-XOld
,以及 y 方向上的增量Y-YOld
。 (这里,X
和Y
是MouseMove
事件处理程序的参数;(X, Y)
是当前光标的位置。)无论
FMouseDown
打开还是关闭,MouseMove
过程都应该结束。Load the image to an off-screen
TBitmap
object. Then,OnPaint
, or whenever is suitable in your particular application, useBitBlt
orCanvas.Draw
to draw a rectangular subimage of theTBitmap
onto the canvas. The subpart should start at(X, Y)
on theTBitmap
and have a width and height equal toClientWidth
andClientHeight
of the form, respectively.Now, respond to keyboard events. Write a
FormKeyDown
event handler, and listen toKey = VK_LEFT
,Key = VK_RIGHT
,Key = VK_UP
, andKey = VK_DOWN
(use acase
statement). When you detect such a key being pressed, increase/decreaseX
orY
, as appropriate, and paint the scene again using this starting point.You can also respond to the
MouseDown
,MouseMove
, andMouseUp
events to scroll using the mouse. Either you can use the middle one only (MouseMove
): You can check if the cursor is near an edge of the form, and if so, scroll in this direction smoothly (using aTTimer
, for instance). Alternatively, you can set aFMouseDown
flag totrue
inMouseDown
, and reset it tofalse
inMouseUp
. Then, inMouseMove
, scroll the bitmap by a deltaX-XOld
in the x direction ifFMouseDown
istrue
, and a deltaY-YOld
in the y direction. (Here,X
andY
are parameters of theMouseMove
event handler;(X, Y)
is the current position of the cursor.) TheMouseMove
procedure should end withno matter if
FMouseDown
is on or off.我也有同样的问题。我的位图约为 5000x5000 像素,加载到 500x500 像素的 Timage 中。
我编写了一个代码来移动Timage中的位图周围,它不能超出
在开头的Form1 var中声明的AlteMausPos的“边界”。
Kerzenbitmap 是包含 5000x5000 图片的位图。
MausPosDifferenz 包含按下鼠标键时移动鼠标的绝对像素数 (x,y)。
然后它会检查所有内容是否都在位图的范围内,然后再使用 CopyRect 进行复制。
我的大脑花了一些时间才发现复制右方以使用绝对改变的鼠标位置的最佳方法。
I had the same problem. My Bitmap is about 5000x5000 pixel, loaded into an Timage of 500x500 pixels.
I wrote a code to move the bitmap arround in the Timage, and it cant go out of the "borders"
AlteMausPos is declared in Form1 var at the beginning.
Kerzenbitmap is your bitmap that contains the 5000x5000 picture.
MausPosDifferenz contains the absolut amount of pixels(x,y) you have moved your mouse while mousekey down.
Then it checks if everything is in Range of the bitmap before copying it with CopyRect.
It took some time for my brain to find out that the best way to copy the rect ist to use the absolut changed mouseposition.