Mit dieser Scrollbox ist es möglich, größere Bilder in einem kleinen Fenster anzeigen zu lassen. Mittels Scrollleiste kann der Bildausschnitt mit der Maus verändert werden:
BlitzBasic: [AUSKLAPPEN] [EINKLAPPEN] Graphics 800, 600, 0, 2 SetBuffer BackBuffer()
Global mouse_x, mouse_y
Type Scrollbox Field pic, pic_width, pic_height Field pos_x, pos_y Field width, height Field cR, cG, cB Field h_scroll, v_scroll Field bar, v_scroller, h_scroller, v_scroller_pos, h_scroller_pos Field dragStarted_v, dragStarted_h, o_scroll, dragValue End Type
S1.Scrollbox = createScrollbox(LoadImage("bild.bmp"), 10, 10, 300, 250, 15) S2.Scrollbox = createScrollbox(LoadImage("bild.bmp"), 50, 300, 600, 250, 30, 255, 255, 0)
Repeat
mouse_x = MouseX() mouse_y = MouseY() Cls doScrollbox S1 doScrollbox S2 Flip()
Until KeyDown(1) End
Function createScrollbox.Scrollbox(image, pos_x, pos_y, width, height, bar, r = 155, g = 155, b = 155)
S.Scrollbox = New Scrollbox S\pic = image S\pic_width = ImageWidth(S\pic) S\pic_height = ImageHeight(S\pic) S\width = width S\height = height S\cR = r: S\cG = g: S\cB = b S\pos_x = pos_x S\pos_y = pos_y S\bar = bar S\v_scroll = 0 S\h_scroll = 0 S\h_scroller = (S\width - (2.0 * S\bar)) / S\pic_width * (S\width - (2 * S\bar)): If S\h_scroller < S\bar Then S\h_scroller = S\bar S\v_scroller = (S\height - (2.0 * S\bar)) / S\pic_height * (S\height - (2 * S\bar)): If S\v_scroller < S\bar Then S\v_scroller = S\bar S\dragStarted_v = False S\dragStarted_h = False
Return S
End Function
Function doScrollbox(S.Scrollbox)
DrawImageRect S\pic, S\pos_x, S\pos_y, S\h_scroll, S\v_scroll, S\width, S\height
Color S\cR, S\cG, S\cB Rect S\pos_x, S\pos_y, S\width, S\height, 0
If S\pic_height > S\height Then
Color S\cR, S\cG, S\cB Rect S\pos_x + S\width, S\pos_y, S\bar, S\height, 0 fill = False If mouseInRect(S\pos_x + S\width, S\pos_y + S\bar + S\v_scroller_pos, S\bar, S\v_scroller) Then fill = True If Not S\dragStarted_v And MouseDown(1) Then S\dragStarted_v = True S\dragValue = mouse_y S\o_scroll = S\v_scroller_pos End If End If If S\dragStarted_v And MouseDown(1) Then fill = True S\v_scroller_pos = S\o_scroll + (mouse_y - S\dragValue) If S\v_scroller_pos < 0 Then S\v_scroller_pos = 0 If S\v_scroller_pos > S\height - 2 * S\bar - S\v_scroller Then S\v_scroller_pos = S\height - 2 * S\bar - S\v_scroller S\v_scroll = S\v_scroller_pos / (S\height - 2.0 * S\bar - S\v_scroller) * (S\pic_height - S\height) End If If Not MouseDown(1) And S\dragStarted_v Then S\dragStarted_v = False End If Rect S\pos_x + S\width + 2, S\pos_y + S\bar + S\v_scroller_pos, S\bar - 4, S\v_scroller, fill
If ArrowBox(S\pos_x + S\width, S\pos_y, S\bar, S\bar, "UP", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then If S\v_scroll > 0 Then S\v_scroll = S\v_scroll - 1 S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller) End If If ArrowBox(S\pos_x + S\width, S\pos_y + S\height - S\bar, S\bar, S\bar, "DOWN", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then If S\v_scroll < S\pic_height - S\height Then S\v_scroll = S\v_scroll + 1 S\v_scroller_pos = (S\v_scroll / Float(S\pic_height - S\height)) * (S\height - 2 * S\bar - S\v_scroller) End If
End If
If S\pic_width > S\width Then
Color S\cR, S\cG, S\cB Rect S\pos_x, S\pos_y + S\height, S\width, S\bar, 0 fill = False If mouseInRect(S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height, S\h_scroller, S\bar) Then fill = True If Not S\dragStarted_h And MouseDown(1) Then S\dragStarted_h = True S\dragValue = mouse_x S\o_scroll = S\h_scroller_pos End If End If If S\dragStarted_h And MouseDown(1) Then fill = True S\h_scroller_pos = S\o_scroll + (mouse_x - S\dragValue) If S\h_scroller_pos < 0 Then S\h_scroller_pos = 0 If S\h_scroller_pos > S\width - 2 * S\bar - S\h_scroller Then S\h_scroller_pos = S\width - 2 * S\bar - S\h_scroller S\h_scroll = S\h_scroller_pos / (S\width - 2.0 * S\bar - S\h_scroller) * (S\pic_width - S\width) End If If Not MouseDown(1) And S\dragStarted_h Then S\dragStarted_h = False End If Rect S\pos_x + S\bar + S\h_scroller_pos, S\pos_y + S\height + 2, S\h_scroller, S\bar - 4, fill
If ArrowBox(S\pos_x, S\pos_y + S\height, S\bar, S\bar, "LEFT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then If S\h_scroll > 0 Then S\h_scroll = S\h_scroll - 1 S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller) End If If ArrowBox(S\pos_x + S\width - S\bar, S\pos_y + S\height, S\bar, S\bar, "RIGHT", S\dragStarted_v Or S\dragStarted_h, S\cR, S\cG, S\cB) And MouseDown(1) Then If S\h_scroll < S\pic_width - S\width Then S\h_scroll = S\h_scroll + 1 S\h_scroller_pos = (S\h_scroll / Float(S\pic_width - S\width)) * (S\width - 2 * S\bar - S\h_scroller) End If
End If
End Function
Function ArrowBox(x, y, w, h, dir$, noHighlight = True, r = 55, g = 155, b = 155)
Color r, g, b mouseIn = False If mouseInRect(x, y, w, h) And noHighlight = False Then Rect x, y, w, h, 1 Color 0, 0, 0 mouseIn = True Else Rect x, y, w, h, 0 Color r, g, b End If
Select Upper(dir$) Case "UP" Line x + 0.5 * w, y + 0.25 * h, x + 0.25 * w, y + 0.75 * h Line x + 0.5 * w, y + 0.25 * h, x + 0.75 * w, y + 0.75 * h Case "DOWN" Line x + 0.5 * w, y + 0.75 * h, x + 0.25 * w, y + 0.25 * h Line x + 0.5 * w, y + 0.75 * h, x + 0.75 * w, y + 0.25 * h Case "LEFT" Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.25 * h Line x + 0.25 * w, y + 0.5 * h, x + 0.75 * w, y + 0.75 * h Case "RIGHT" Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.25 * h Line x + 0.75 * w, y + 0.5 * h, x + 0.25 * w, y + 0.75 * h End Select
Return mouseIn And noHighlight = False
End Function
Function mouseInRect(x, y, w, h) If mouse_x <= x Then Return False If mouse_y <= y Then Return False If mouse_x >= x + w Then Return False If mouse_y >= y + h Then Return False Return True End Function
EDIT:
Danke Darth, hab in meiner Lösung nun auch einen Konstruktor drin, aber so viel Arbeit wie du hab ich nicht mehr reininvestiert... Dafür ist es ja hier das Codearchiv, darf jeder dran rumwerkeln. Hab aber nun die Abfrage drin, dass die Scrollbalken nur angezeigt werden, wenn das Bild auch tatsächlich größer ist, als die Box.
|