Move ScrollAreaGadget with mouse without scrollbars?

Just starting out? Need help? Post your questions and find answers here.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Move ScrollAreaGadget with mouse without scrollbars?

Post by Dude »

I've just used a ScrollAreaGadget for the first time, and even though I can scroll it around with the scrollbars, my natural instinct is the click and drag the image inside it to move the image around instead. Scrollbars are soooo 1990s. ;)

I've read about 20 posts in these forums about ScrollAreaGadgets with the mouse, but they were all to do with other mouse operations with it. :(

Surely there's an example somewhere here of scrolling the ScrollAreaGadget by clicking and dragging the mouse inside it? :shock:
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by RASHAD »

LeftButtonDown and Drag
I think that this will be the first one in the forum and the globe too :mrgreen:
Adapt it for your needs
Windows only :P

Code: Select all

#SC_DragMove      = $F012
  
Procedure IsMouseOver(hWnd) 
  GetWindowRect_(hWnd,mr.RECT)
  GetCursorPos_(mp.POINT) 
  Result = PtInRect_(mr,mp\y << 32 + mp\x) 
  ProcedureReturn Result 
EndProcedure

LoadFont(0,"Georgia",12) 
  
If OpenWindow(0, 0, 0, 800,600, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  cont = ContainerGadget(#PB_Any,10,10,780,530,#PB_Container_Flat )
    ScrollAreaGadget(0, 10, 10, 1920, 1200, 3000,3000, 30,#PB_ScrollArea_BorderLess )
      ShowScrollBar_(GadgetID(0), #SB_BOTH, #False)
      ButtonGadget  (1, 10, 10, 230, 30,"Button 1")
      ButtonGadget  (2, 50, 50, 230, 30,"Button 2")
      ButtonGadget  (3, 890, 90, 230, 30,"Button 3")
      TextGadget    (4,1000,130, 230, 20,"This is the content of a ScrollAreaGadget!",#PB_Text_Right)
    CloseGadgetList()
  CloseGadgetList()
  txt1 = TextGadget(#PB_Any,10,550, 200, 40,"ScrollArea X Position :"+#CRLF$+"ScrollArea Y Position :")
  SetGadgetFont(txt1,FontID(0))
  txt2 = TextGadget(#PB_Any,220,550, 60, 40,"")
  SetGadgetFont(txt2,FontID(0))
  SetGadgetColor(txt2,#PB_Gadget_FrontColor ,$0000FF)
  
Repeat
    Select WaitWindowEvent()
      Case  #PB_Event_CloseWindow
        End          
          
      Case #WM_LBUTTONDOWN
        If IsMouseOver(GadgetID(1)) = 0 And IsMouseOver(GadgetID(2)) = 0
          SetCursor_(LoadCursor_(0, #IDC_SIZEALL))
          SendMessage_(GadgetID(0), #WM_SYSCOMMAND , #SC_DragMove,0)
        EndIf
        GetWindowRect_(GadgetID(0),r.RECT)
        SetGadgetText(txt2,Str(3000-r\left)+#CRLF$+Str(3000-r\top))
        
      Case  #PB_Event_Gadget
        Select EventGadget()
          Case 1
            MessageRequester("Info","Button 1 was pressed!",#PB_MessageRequester_Ok)
          Case 2
            MessageRequester("Info","Button 2 was pressed!",#PB_MessageRequester_Ok)
          Case 3
            MessageRequester("Info","Button 3 was pressed!",#PB_MessageRequester_Ok)
        EndSelect
    EndSelect
  ForEver
EndIf
Egypt my love
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by said »

RASHAD wrote: I think that this will be the first one in the forum and the globe too :mrgreen:
Nice one RASHAD :lol: :lol: :lol: :lol:
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by Dude »

Thanks, Rashad! :) I knew there must be some easy API way.

BTW, here's the hack solution I was using (to prove that I do try before asking for help).

You can see why I wanted a more efficient way. :lol:

Code: Select all

; Executed when user clicks on ImageGadet inside ScrollAreaGadget.
ox=WindowMouseX(0)
oy=WindowMouseY(0)
Repeat
  Sleep_(1)
  redraw=0
  nx=WindowMouseX(0)
  ny=WindowMouseY(0)
  If nx<ox
    redraw=1
    shotscrollposx+10
    If shotscrollposx>shotimagew
      shotscrollposx=shotimagew
    EndIf
  ElseIf nx>ox
    redraw=1
    shotscrollposx-10
    If shotscrollposx<0
      shotscrollposx=0
    EndIf
  EndIf
  If ny<oy
    redraw=1
    shotscrollposy+10
    If shotscrollposy>shotimageh
      shotscrollposy=shotimageh
    EndIf
  ElseIf ny>oy
    redraw=1
    shotscrollposy-10
    If shotscrollposy<0
      shotscrollposy=0
    EndIf
  EndIf
  If redraw=1
    SetGadgetAttribute(#shotthumbscroller,#PB_ScrollArea_X,shotscrollposx)
    SetGadgetAttribute(#shotthumbscroller,#PB_ScrollArea_Y,shotscrollposy)
    UpdateWindow_(shotthumbscroller)
  EndIf
  ox=nx
  oy=ny
Until GetAsyncKeyState_(#VK_LBUTTON)=0
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by RASHAD »

@said
Thanks mate

@Dude
I never had any doubt about how much you are good in programing :)
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by Dude »

Rashad, unfortunately I can't make your example work with images. :(

I'd like to click and drag the image in this code, but it doesn't work:

Code: Select all

#SC_DragMove = $F012

CreateImage(0,500,500)
StartDrawing(ImageOutput(0))
For x=0 To 499
  For y=0 To 499
    Plot(x,y,RGB(Random(255),Random(255),Random(255)))
  Next
Next
StopDrawing()

Procedure IsMouseOver(hWnd)
  GetWindowRect_(hWnd,mr.RECT)
  GetCursorPos_(mp.POINT)
  Result = PtInRect_(mr,mp\y << 32 + mp\x)
  ProcedureReturn Result
EndProcedure

If OpenWindow(0, 0, 0, 270, 270, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ScrollAreaGadget(0, 10, 10, 250, 250, 750, 750, 30)
  ImageGadget(1, 0, 0, 500, 500, ImageID(0))
  CloseGadgetList()
  Repeat
    Select WaitWindowEvent()
      Case  #PB_Event_CloseWindow
        End         
      Case #WM_LBUTTONDOWN
        If IsMouseOver(GadgetID(0))
          SetCursor_(LoadCursor_(0, #IDC_SIZEALL))
          SendMessage_(GadgetID(0), #WM_SYSCOMMAND , #SC_DragMove, 0)
        EndIf
    EndSelect
  ForEver
EndIf
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by RASHAD »

Hi Dude

Code: Select all

#SC_DragMove = $F012

LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/Geebee2.bmp")
ResizeImage(0, 500,500,#PB_Image_Smooth)

Procedure IsMouseOver(hWnd)
  GetWindowRect_(hWnd,mr.RECT)
  GetCursorPos_(mp.POINT)
  Result = PtInRect_(mr,mp\y << 32 + mp\x)
  ProcedureReturn Result
EndProcedure

If OpenWindow(0, 0, 0, 270, 270, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  cont = ContainerGadget(#PB_Any,10,10,250,250,#PB_Container_Flat)
    ScrollAreaGadget(0, 0, 0, 500, 500, 2000,2000)
      ShowScrollBar_(GadgetID(0), #SB_BOTH, #False)
      ImageGadget(1, 0, 0, 500, 500, ImageID(0))    
      DisableGadget(1,1)    
    CloseGadgetList()
  CloseGadgetList()
  Repeat
    Select WaitWindowEvent()
      Case  #PB_Event_CloseWindow
        End
                 
      Case #WM_LBUTTONDOWN
        If IsMouseOver(GadgetID(1))
          SetCursor_(LoadCursor_(0, #IDC_SIZEALL))
          SendMessage_(GadgetID(0), #WM_SYSCOMMAND , #SC_DragMove, 0)
        EndIf
        Debug "Latitude  :" + Str(DesktopMouseX())
        Debug "Longitude :" + Str(DesktopMouseY())             
    EndSelect
  ForEver
EndIf
Last edited by RASHAD on Wed Jan 17, 2018 2:19 pm, edited 1 time in total.
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by Dude »

Getting there... :) But how to stop the image scrolling outside the borders of the ScrollAreaGadget? :)

I did try for a while by checking GadgetX(0) with GadgetX(1) to compare, but couldn't figure anything out.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4663
Joined: Sun Apr 12, 2009 6:27 am

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by RASHAD »

Hi again

Code: Select all

#SC_DragMove = $F012

LoadImage(0, #PB_Compiler_Home + "examples/sources/Data/Geebee2.bmp")
ResizeImage(0, 500,500,#PB_Image_Smooth)

Procedure IsMouseOver(hWnd)
  GetWindowRect_(hWnd,mr.RECT)
  GetCursorPos_(mp.POINT)
  Result = PtInRect_(mr,mp\y << 32 + mp\x)
  ProcedureReturn Result
EndProcedure

Procedure scrollCB()
  SetCursor_(LoadCursor_(0, #IDC_SIZEALL))
  SendMessage_(GadgetID(0), #WM_SYSCOMMAND , #SC_DragMove, 0)
  If GadgetX(0) < 0 Or GadgetY(0) < 0 Or GadgetX(0)+GadgetWidth(1) > 2000  Or GadgetY(0) + GadgetHeight(1) > 2000 
    ResizeGadget(0,0,0,#PB_Ignore,#PB_Ignore)
  EndIf
EndProcedure

Procedure sizeCB()
  ResizeGadget(10,10,10,WindowWidth(0)-20,WindowHeight(0)-20)
EndProcedure

If OpenWindow(0, 0, 0, 270, 270, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
  ContainerGadget(10,10,10,250,250,#PB_Container_Flat)
    ScrollAreaGadget(0, 0, 0, 500, 500, 2000,2000)
      ShowScrollBar_(GadgetID(0), #SB_BOTH, #False)
      ImageGadget(1, 0, 0, 500, 500, ImageID(0)) 
    CloseGadgetList()
  CloseGadgetList()
  
  BindGadgetEvent(1,@scrollCB(),#PB_All )
  BindEvent(#PB_Event_SizeWindow,@sizeCB())
  Repeat
    Select WaitWindowEvent()
      Case  #PB_Event_CloseWindow
        End           
    EndSelect
  ForEver
EndIf
Egypt my love
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: Move ScrollAreaGadget with mouse without scrollbars?

Post by Dude »

Thanks again, I'll play with that. Cheers! 8)
Post Reply