Page 2 of 3

Re: move/drag canvas image

Posted: Tue Mar 26, 2013 7:27 pm
by J. Baker
flaith wrote::lol: Sorry J. Baker, I just put a quick code just after your thread, didn't had a better one at that time :wink:
And Danilo's code is really good :D
No worries. ;)

WOW! I didn't expect to wake up to find more on this posting. I started to add in the "alpha" code part last night and was going to post. Looks like Danilo beat me to it. Thanks again everybody! ;)

Re: move/drag canvas image

Posted: Tue Mar 26, 2013 8:52 pm
by infratec
Hi,

a small contribution:

Code: Select all

Case #PB_EventType_MouseMove
            If Drag = #True
               If isCurrentItem
                  x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
                  y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
                  If LastElement(Images())
                     Images()\x = x - currentItemXOffset
                     Images()\y = y - currentItemYOffset
                     DrawCanvas(#MyCanvas, Images())
                  EndIf
                EndIf
            Else    
              x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
              y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
              If HitTest(Images(), x, y)
                SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Hand) 
              Else
                SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Default) 
              EndIf
            EndIf
Now you can see that you can not move the hole :wink:

Bernd

Re: move/drag canvas image

Posted: Wed Mar 27, 2013 7:24 am
by dige
@Danilo: great example. I had to play a little with the source too...

Now with active border, mousewheel support and drop shadow while moving ;-)

Code: Select all

EnableExplicit

Structure canvasitem
  img.i
  x.i
  y.i
  Width.i
  Height.i
  alphatest.i
  Zoom.f
  Rotate.f
EndStructure

Enumeration
  #MyCanvas = 1   ; just to test whether a number different from 0 works now
EndEnumeration

Define isCurrentItem=#False
Define currentItemXOffset.i, currentItemYOffset.i
Define Event.i, x.i, y.i, hole.i
NewList Images.canvasitem()
Global Drag.i

Procedure AddImage (List Images.canvasitem(), x, y, img, alphatest=0)
  If AddElement(Images())
    Images()\img    = img
    Images()\x      = x
    Images()\y      = y
    Images()\Width  = ImageWidth(img)
    Images()\Height = ImageHeight(img)
    Images()\alphatest = alphatest
    Images()\Zoom      = 1
  EndIf
EndProcedure

Procedure DrawCanvas (canvas.i, List Images.canvasitem())
  If StartDrawing(CanvasOutput(canvas))
      Box(0, 0, GadgetWidth(canvas), GadgetHeight(canvas), RGB(255,255,255))
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      ForEach Images()
        With Images()
          \Width  = ImageWidth(\img)*\Zoom
          \Height = ImageHeight(\img)*\Zoom
          DrawImage(ImageID(\img), \x, \y, \Width, \Height) ; draw all images with z-order
        EndWith
      Next
      If Not Drag
        With Images()
          DrawingMode(#PB_2DDrawing_Outlined)
          Box(\x, \y, \Width, \Height, #Blue)
          DrawingMode(#PB_2DDrawing_Default)
          Box(\x-5, \y-5, 10, 10, #Blue)
          Box(\x + \Width-5, \y-5, 10, 10, #Blue)
          Box(\x + \Width-5, \y + \Height-5, 10, 10, #Blue)
          Box(\x-5, \y + \Height-5, 10, 10, #Blue)
        EndWith
      Else
        With Images()
          DrawingMode(#PB_2DDrawing_AlphaBlend)
          Box(\x+5, \y+5, \Width, \Height, RGBA($42, $42, $42, $50))
          
          DrawImage(ImageID(\img), \x, \y, \Width, \Height)
        EndWith
      EndIf
    StopDrawing()
  EndIf
EndProcedure

Procedure.i HitTest (List Images.canvasitem(), x, y)
  Shared currentItemXOffset.i, currentItemYOffset.i
  Protected Alpha.i, isCurrentItem.i = #False
  
  If LastElement(Images()) ; search for hit, starting from end (z-order)
    Repeat
      If x >= Images()\x And x < Images()\x + Images()\Width
        If y >= Images()\y And y < Images()\y + Images()\Height
          Alpha = 255
          If Images()\alphatest And ImageDepth(Images()\img)>31
            If StartDrawing(ImageOutput(Images()\img))
                DrawingMode(#PB_2DDrawing_AlphaChannel)
                Alpha = Alpha(Point(x-Images()\x,y-Images()\y)) ; get alpha
              StopDrawing()
            EndIf
          EndIf
          If Alpha
            MoveElement(Images(), #PB_List_Last)
            isCurrentItem = #TRUE
            currentItemXOffset = x - Images()\x
            currentItemYOffset = y - Images()\y
            Break
          EndIf
        EndIf
      EndIf
    Until PreviousElement(Images()) = 0
  EndIf
  
  ProcedureReturn isCurrentItem
EndProcedure


AddImage(Images(),  10, 10, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))
AddImage(Images(), 100,100, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/GeeBee2.bmp"))
AddImage(Images(),  50,200, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/AlphaChannel.bmp"))

hole = CreateImage(#PB_Any,100,100,32)
If StartDrawing(ImageOutput(hole))
    DrawingMode(#PB_2DDrawing_AllChannels)
    Box(0,0,100,100,RGBA($00,$00,$00,$00))
    Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
    Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
EndIf
AddImage(Images(),170,70,hole,1)

If OpenWindow(0, 0, 0, 420, 420, "Move/Drag Canvas Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
  MessageRequester("Fatal error", "Program terminated.")
  End
EndIf

CanvasGadget(#MyCanvas, 10, 10, 400, 400, #PB_Canvas_Keyboard)
DrawCanvas(#MyCanvas, Images())

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget And EventGadget() = #MyCanvas

    x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
    y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
    
    Select EventType()
      Case #PB_EventType_MouseWheel
        If HitTest(Images(), x, y)
          If isCurrentItem
            Select GetGadgetAttribute(#MyCanvas, #PB_Canvas_WheelDelta)
              Case 1 : Images()\Zoom + 0.05
              Default: Images()\Zoom - 0.05
            EndSelect
            If Images()\Zoom < 0.1
              Images()\Zoom = 0.1
            EndIf
            DrawCanvas(#MyCanvas, Images())
          EndIf
        EndIf
        
      Case #PB_EventType_LeftButtonDown
        isCurrentItem = HitTest(Images(), x, y)
        If isCurrentItem
          DrawCanvas(#MyCanvas, Images())
        EndIf
        Drag = #TRUE
      Case #PB_EventType_LeftButtonUp
        Drag = #False
        DrawCanvas(#MyCanvas, Images())
        
      Case #PB_EventType_MouseMove
        If Drag = #TRUE
          If isCurrentItem
            If LastElement(Images())
              Images()\x = x - currentItemXOffset
              Images()\y = y - currentItemYOffset
              DrawCanvas(#MyCanvas, Images())
            EndIf
          EndIf
        Else   
          If HitTest(Images(), x, y)
            SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Hand)
          Else
            SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Default)
          EndIf
        EndIf
    EndSelect
  EndIf   
Until Event = #PB_Event_CloseWindow

Re: move/drag canvas image

Posted: Wed Mar 27, 2013 10:53 am
by wilbert
Another version with a drag shadow that also works for images that are (partly) transparent.

Code: Select all

EnableExplicit

Structure canvasitem
  img.i
  x.w
  y.w
  width.w
  height.w
  drag_x.w
  drag_y.w
  alphatest.b
EndStructure

Enumeration
  #MyCanvas = 1   ; just to test whether a number different from 0 works now
EndEnumeration

Define Event.i, x.i, y.i, drag.i, hole.i

Define *currentItem.canvasitem
NewList Images.canvasitem()

Procedure AddImage (List Images.canvasitem(), x, y, img, alphatest = #False)
  If AddElement(Images())
    If alphatest And ImageDepth(img) < 32
      alphatest = #False
    EndIf
    Images()\img    = img
    Images()\x      = x
    Images()\y      = y
    Images()\width  = ImageWidth(img)
    Images()\height = ImageHeight(img)
    Images()\alphatest = alphatest
  EndIf
EndProcedure

Procedure DrawCanvas (canvas.i, List Images.canvasitem(), bgColor.l = $ffffff)
  Shared *currentItem, drag
  Protected shadow, *i.canvasitem = *currentItem
  If drag And *i
    shadow = CopyImage(*i\img, #PB_Any)
    StartDrawing(ImageOutput(shadow))
    Box(0, 0, *i\width, *i\height, 0)
    StopDrawing()
  EndIf
  If StartDrawing(CanvasOutput(canvas))
    Box(0, 0, GadgetWidth(canvas), GadgetHeight(canvas), bgColor)
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    ForEach Images()
      *i = Images()
      If drag And *i = *currentItem
        DrawAlphaImage(ImageID(shadow), *i\x + 3, *i\y + 3, 30)
      EndIf
      DrawImage(ImageID(*i\img), *i\x, *i\y) ; draw all images with z-order
    Next
    StopDrawing()
  EndIf
  If shadow
    FreeImage(shadow)
  EndIf
EndProcedure

Procedure.i HitTest (List Images.canvasitem(), x, y, prepareFordrag = #False) ; search for hit, starting from end (z-order)
  Protected hit_x.w, hit_y.w, alpha.b, *i.canvasitem = LastElement(Images())
  While *i
    hit_x = x - *i\x
    hit_y = y - *i\y
    If hit_x >= 0 And hit_y >= 0 And hit_x < *i\width And hit_y < *i\height
      alpha = 255
      If *i\alphatest And StartDrawing(ImageOutput(*i\img))
        DrawingMode(#PB_2DDrawing_AlphaChannel)
        alpha = Point(hit_x, hit_y) >> 24 ; get alpha
        StopDrawing()
      EndIf
      If alpha
        If prepareFordrag
          MoveElement(Images(), #PB_List_Last)
          *i\drag_x = hit_x
          *i\drag_y = hit_y          
        EndIf
        Break
      EndIf
    EndIf
    *i = PreviousElement(Images())
  Wend
  ProcedureReturn *i
EndProcedure


AddImage(Images(),  10, 10, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))
AddImage(Images(), 100,100, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/GeeBee2.bmp"))
AddImage(Images(),  50,200, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/AlphaChannel.bmp"))

hole = CreateImage(#PB_Any,100,100,32)
If StartDrawing(ImageOutput(hole))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,100,100,RGBA($00,$00,$00,$00))
  Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
  Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
EndIf
AddImage(Images(),170,70,hole,#True)

If OpenWindow(0, 0, 0, 420, 420, "Move/drag Canvas Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
  MessageRequester("Fatal error", "Program terminated.")
  End
EndIf

CanvasGadget(#MyCanvas, 10, 10, 400, 400)
DrawCanvas(#MyCanvas, Images(), $f0fff0)

Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget And EventGadget() = #MyCanvas
    Select EventType()
      Case #PB_EventType_LeftButtonDown
        x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
        y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
        *currentItem = HitTest(Images(), x, y, #True)
        If *currentItem
          drag = #True
          DrawCanvas(#MyCanvas, Images(), $f0fff0)
        EndIf
      Case #PB_EventType_LeftButtonUp
        drag = #False
        DrawCanvas(#MyCanvas, Images(), $f0fff0)
      Case #PB_EventType_MouseMove
        x = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
        y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
        If drag
          *currentItem\x = x - *currentItem\drag_x
          *currentItem\y = y - *currentItem\drag_y
          DrawCanvas(#MyCanvas, Images(), $f0fff0)
        ElseIf HitTest(Images(), x, y)
          SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Hand) 
        Else
          SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Default) 
        EndIf
    EndSelect
  EndIf   
Until Event = #PB_Event_CloseWindow

Re: move/drag canvas image

Posted: Sat Dec 05, 2015 9:18 am
by coder14
Hello everyone.Thank you for the great code.

I think that NicTheQuick modified codes to add resizing.This post in the German forum:
http://www.forums.purebasic.com/german/ ... 91#p315798

After resizing larger the transparent circle I get an error when I try to click in the new larger areas of the circle:

Code: Select all

[ERROR] Point(): Point() is outside the drawing area.
Why is this happening?

Re: move/drag canvas image

Posted: Sat Dec 05, 2015 1:08 pm
by TI-994A
coder14 wrote:After resizing larger the transparent circle I get an error when I try to click in the new larger areas of the circle:

Code: Select all

[ERROR] Point(): Point() is outside the drawing area.
As the error indicates, the Point() function is trying to access a pixel outside the current drawing area. This is because the resizing code only updates the list records, but does not actually resize the image itself. Add this to the #PB_EventType_LeftButtonUp code, and it should work:

Code: Select all

Case #PB_EventType_LeftButtonUp
  drag = #False
  DrawCanvas(#MyCanvas, Images(), $f0fff0)
  ResizeImage(Images()\img, Images()\width, Images()\height)

Re: move/drag canvas image

Posted: Sat Dec 05, 2015 3:23 pm
by coder14
TI-994A wrote:
coder14 wrote:After resizing larger the transparent circle I get an error when I try to click in the new larger areas of the circle:

Code: Select all

[ERROR] Point(): Point() is outside the drawing area.
As the error indicates, the Point() function is trying to access a pixel outside the current drawing area. This is because the resizing code only updates the list records, but does not actually resize the image itself. Add this to the #PB_EventType_LeftButtonUp code, and it should work:

Code: Select all

Case #PB_EventType_LeftButtonUp
  drag = #False
  DrawCanvas(#MyCanvas, Images(), $f0fff0)
  ResizeImage(Images()\img, Images()\width, Images()\height)
Thanks for your help TI-994A! It works now. :D :D :D

Re: move/drag canvas image

Posted: Sat Dec 05, 2015 6:22 pm
by coder14
When moving the images around how to determine if the image is overlapping another image?In Windows PtInRect can tell if point is in a rectangle area, but how to do for whole image area?

Thank you.

Re: move/drag canvas image

Posted: Sat Dec 05, 2015 8:24 pm
by davido
@coder14,
Thank you for posting the code by NicTheQuick
@TI-994A,
Thank you for the correction.

Re: move/drag canvas image

Posted: Sun Dec 06, 2015 4:47 pm
by mestnyi
To the selected pattern back into place (Z-order).

Code: Select all

EnableExplicit

Structure canvasitem
  img.i
  X.w
  Y.w
  Width.w
  Height.w
  drag_x.w
  drag_y.w
  alphatest.b
  IsGrid.i
  Index.i
EndStructure

Enumeration
  #MyCanvas = 1   ; just to test whether a number different from 0 works now
EndEnumeration

Define Event.i, X.i, Y.i, Drag.i, hole.i, Grid.i

Define *currentItem.canvasitem
NewList Images.canvasitem()


Procedure AddImage (List Images.canvasitem(), X, Y, img, alphatest = #False, Grid = #False)
  If AddElement(Images())
    If alphatest And ImageDepth(img) < 32
      alphatest = #False
    EndIf
    
    Images()\IsGrid = Grid
    Images()\Index = ListIndex(Images())
    Images()\img    = img
    Images()\x      = X
    Images()\y      = Y
    Images()\width  = ImageWidth(img)
    Images()\height = ImageHeight(img)
    Images()\alphatest = alphatest
    
    If Grid
      MoveElement(Images(), #PB_List_First)
    EndIf
  EndIf
EndProcedure

Procedure DrawCanvas (canvas.i, List Images.canvasitem(), bgColor.l = $ffffff)
  Shared *currentItem, Drag
  Protected shadow, *i.canvasitem = *currentItem
  
    If Drag And *i
      shadow = CopyImage(*i\img, #PB_Any)
      StartDrawing(ImageOutput(shadow))
      Box(0, 0, *i\width, *i\height, 0)
      StopDrawing()
    EndIf
    
    If StartDrawing(CanvasOutput(canvas))
      Box(0, 0, GadgetWidth(canvas), GadgetHeight(canvas), bgColor)
      DrawingMode(#PB_2DDrawing_AlphaBlend)
      ForEach Images()
       *i = Images()
        If Drag And *i = *currentItem
          DrawAlphaImage(ImageID(shadow), *i\x + 3, *i\y + 3, 30)
        EndIf
        DrawImage(ImageID(*i\img), *i\x, *i\y) ; draw all images with z-order
      Next
      StopDrawing()
    EndIf
    
    If shadow
    FreeImage(shadow)
  EndIf
EndProcedure

Procedure.i HitTest (List Images.canvasitem(), X, Y) ; search for hit, starting from end (z-order)
  Protected hit_x.w, hit_y.w, alpha.b, *i.canvasitem 
  *i = LastElement(Images())
  PushListPosition(Images())
  
  While *i
    If *i And Not *i\IsGrid
      hit_x = X - *i\x
      hit_y = Y - *i\y
      If hit_x >= 0 And hit_y >= 0 And hit_x < *i\width And hit_y < *i\height
        alpha = 255
        If *i\alphatest And StartDrawing(ImageOutput(*i\img))
          DrawingMode(#PB_2DDrawing_AlphaChannel)
          alpha = Point(hit_x, hit_y) >> 24 ; get alpha
          StopDrawing()
        EndIf
        If alpha
          Select EventType()
            Case #PB_EventType_LeftButtonDown
              MoveElement(Images(), #PB_List_Last)
              *i\drag_x = hit_x
              *i\drag_y = hit_y          
              
            Case #PB_EventType_LeftButtonUp
              PushListPosition(Images())
              Protected *Prev = SelectElement(Images(), Images()\Index)
              PopListPosition(Images())
              MoveElement(Images(), #PB_List_After, *Prev)
              
          EndSelect
          Break
        EndIf
      EndIf
    EndIf
    *i = PreviousElement(Images())
  Wend
  
  PopListPosition(Images())
  ProcedureReturn *i
  
EndProcedure

AddImage(Images(),  10, 10, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))
AddImage(Images(), 100,100, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/GeeBee2.bmp"))
AddImage(Images(),  50,200, LoadImage(#PB_Any, #PB_Compiler_Home + "Examples/Sources/Data/AlphaChannel.bmp"))

hole = CreateImage(#PB_Any,100,100,32)
If StartDrawing(ImageOutput(hole))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,100,100,RGBA($00,$00,$00,$00))
  Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
  Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
EndIf
AddImage(Images(),170,70,hole,#True)



If OpenWindow(0, 0, 0, 420, 420, "Move/drag Canvas Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
  MessageRequester("Fatal error", "Program terminated.")
  End
EndIf

CanvasGadget(#MyCanvas, 10, 10, 400, 400)

Define Y,X, Steps = 5
Grid = CreateImage(#PB_Any, 400, 400,32,#PB_Image_Transparent)
If StartDrawing(ImageOutput(Grid))
  DrawingMode(#PB_2DDrawing_AlphaChannel|#PB_2DDrawing_Transparent)
  ;Box(0, 0, OutputWidth(),OutputHeight(), RGBA(0, 0, 0, 0))
  For X = 0 To OutputWidth()-1
    For Y = 0 To OutputHeight()-1
      Plot(X,Y,RGBA(0,0,0,255))
      Y+Steps
    Next
    X+Steps
  Next
  StopDrawing()
EndIf

AddImage(Images(),0,0,Grid,#True, #True)

DrawCanvas(#MyCanvas, Images(), $f0fff0)


Repeat
  Event = WaitWindowEvent()
  
  If Event = #PB_Event_Gadget And EventGadget() = #MyCanvas
    Select EventType()
      Case #PB_EventType_LeftButtonDown
        X = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
        Y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
        *currentItem = HitTest(Images(), X, Y)
        
        If *currentItem
          Debug *currentItem\Index
          Drag = #True
          DrawCanvas(#MyCanvas, Images(), $f0fff0)
        EndIf
        
      Case #PB_EventType_LeftButtonUp
        Drag = #False
        *currentItem = HitTest(Images(), X, Y)
        If *currentItem
          DrawCanvas(#MyCanvas, Images(), $f0fff0)
          ResizeImage(Images()\img, Images()\width, Images()\height)
        EndIf
        
      Case #PB_EventType_MouseMove
        X = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseX)
        Y = GetGadgetAttribute(#MyCanvas, #PB_Canvas_MouseY)
        If Drag
          *currentItem\x = X - *currentItem\drag_x
          *currentItem\y = Y - *currentItem\drag_y
          DrawCanvas(#MyCanvas, Images(), $f0fff0)
        ElseIf HitTest(Images(), X, Y)
          SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Hand) 
        Else
          SetGadgetAttribute(#MyCanvas, #PB_Canvas_Cursor, #PB_Cursor_Default) 
        EndIf
    EndSelect
  EndIf   
Until Event = #PB_Event_CloseWindow

Re: move/drag canvas image

Posted: Tue Dec 08, 2015 1:23 am
by gurj

Code: Select all

If OpenWindow(0, 0, 0, 420, 420, "Move/drag Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
 MessageRequester("Fatal error", "Program terminated.")
 End
EndIf

ScrollAreaGadget(0, 10, 10, 400, 400, 1400, 1200)
ImageGadget(1,  10, 10, 200,100,LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))

If  CreateImage(1,100,100,32)
 If StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,100,100,RGBA($00,$00,$00,$00))
  Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
  Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
 EndIf
ImageGadget(2,170,70,100,100,ImageID(1)):EndIf
hDC=WindowID(0)
Repeat
 Event = WaitWindowEvent()
 Select Event
  Case #WM_MOUSEMOVE
   If GetAsyncKeyState_(#VK_LBUTTON):eg=EventGadget():If eg>0
     SendMessage_(GadgetID(eg), #WM_SYSCOMMAND, #SC_MOVE + #HTCLIENT, 0)
   EndIf:EndIf
 EndSelect
Until Event = #PB_Event_CloseWindow
__________________________________________________
Code tags added
08.12.2015
RSBasic

Re: move/drag canvas image

Posted: Tue Dec 08, 2015 6:34 am
by gurj
other one:

Code: Select all

If OpenWindow(0, 0, 0, 420, 420, "Move/drag Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
 MessageRequester("Fatal error", "Program terminated.")
 End
EndIf

ScrollAreaGadget(0, 10, 10, 400, 400, 1400, 1200)
ImageGadget(1,  10, 10, 200,100,LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))

If  CreateImage(1,100,100,32)
 If StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,100,100,RGBA($00,$00,$00,$00))
  Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
  Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
 EndIf
ImageGadget(2,170,70,100,100,ImageID(1)):EndIf

Repeat
 Event = WaitWindowEvent()
 Select Event
  Case #PB_Event_Gadget:eg=EventGadget()
  Case #WM_LBUTTONUP:jx=0:eg=-1
  Case #WM_MOUSEMOVE
   If GetAsyncKeyState_(#VK_LBUTTON):If eg>0:If jx=1
     x=WindowMouseX(0)-x0:y=WindowMouseY(0)-y0
     ResizeGadget(eg,gx+x,gy+y,#PB_Ignore,#PB_Ignore):Else:jx=1:Gosub cs:EndIf
   EndIf:EndIf
 EndSelect
Until Event = #PB_Event_CloseWindow
End
cs: :x0=WindowMouseX(0):y0=WindowMouseY(0)
gx=GadgetX(eg):gy=GadgetY(eg)
Return

Re: move/drag canvas image

Posted: Tue Dec 08, 2015 9:02 am
by gurj
other 2:

Code: Select all

If OpenWindow(0, 0, 0, 420, 420, "Move/drag Image", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) = 0
 MessageRequester("Fatal error", "Program terminated.")
 End
EndIf

ScrollAreaGadget(0, 10, 10, 400, 400, 1400, 1200)
ImageGadget(1,  10, 10, 200,100,LoadImage(0, #PB_Compiler_Home + "Examples/Sources/Data/PureBasic.bmp"))

If  CreateImage(1,100,100,32)
 If StartDrawing(ImageOutput(1))
  DrawingMode(#PB_2DDrawing_AllChannels)
  Box(0,0,100,100,RGBA($00,$00,$00,$00))
  Circle(50,50,48,RGBA($00,$FF,$FF,$FF))
  Circle(50,50,30,RGBA($00,$00,$00,$00))
  StopDrawing()
 EndIf
ImageGadget(2,170,70,100,100,ImageID(1)):EndIf

Repeat
 Event = WaitWindowEvent()
 Select Event
  Case #PB_Event_Gadget:eg=EventGadget()
  Case #WM_LBUTTONUP:eg=-1
  Case #WM_MOUSEMOVE
   If GetAsyncKeyState_(#VK_LBUTTON):If eg>0
     SendMessage_(GadgetID(eg), #WM_SYSCOMMAND, #SC_MOVE + #HTCLIENT, 0)
   EndIf:EndIf
  Default:eg=-1
 EndSelect
Until Event = #PB_Event_CloseWindow


Re: move/drag canvas image

Posted: Wed Dec 09, 2015 3:26 pm
by gurj
delete:GetAsyncKeyState_(#VK_LBUTTON)

Re: move/drag canvas image

Posted: Thu Dec 10, 2015 2:32 am
by gurj
but 'other one':
move 'GetAsyncKeyState' to:

Code: Select all

  Case #PB_Event_Gadget:If GetAsyncKeyState_(#VK_LBUTTON):eg=EventGadget():EndIf