I’m trying to make a small game based on the canvas in Delphi. Basically, I’d like to make a fairly large bitmap ( 3000×3000, 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?
Share
Load the image to an off-screen
TBitmapobject. Then,OnPaint, or whenever is suitable in your particular application, useBitBltorCanvas.Drawto draw a rectangular subimage of theTBitmaponto the canvas. The subpart should start at(X, Y)on theTBitmapand have a width and height equal toClientWidthandClientHeightof the form, respectively.Now, respond to keyboard events. Write a
FormKeyDownevent handler, and listen toKey = VK_LEFT,Key = VK_RIGHT,Key = VK_UP, andKey = VK_DOWN(use acasestatement). When you detect such a key being pressed, increase/decreaseXorY, as appropriate, and paint the scene again using this starting point.You can also respond to the
MouseDown,MouseMove, andMouseUpevents 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 aFMouseDownflag totrueinMouseDown, and reset it tofalseinMouseUp. Then, inMouseMove, scroll the bitmap by a deltaX-XOldin the x direction ifFMouseDownistrue, and a deltaY-YOldin the y direction. (Here,XandYare parameters of theMouseMoveevent handler;(X, Y)is the current position of the cursor.) TheMouseMoveprocedure should end withno matter if
FMouseDownis on or off.