[Solved] Moving borderless window

Just starting out? Need help? Post your questions and find answers here.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

[Solved] Moving borderless window

Post by SkyManager »

Hi,

I want to move my gadget but it is not working.
Can anybody give me some clue about the problem?
Is there any fastest solution?

Code: Select all

Global StartMoving.l		= #False
Global MovingMouseOffset.l	= -1
Global LastMouseLocation.l	= -1
Enumeration
	#MainWindow
	#MainContainer
	#MoveButton
EndEnumeration
If OpenWindow(#MainWindow, 800, 0, 155, 25, "", #PB_Window_BorderLess)
	ButtonGadget(  #MoveButton,   1, 1,  30, 20, "Test")
  	Repeat
	  	Event = WaitWindowEvent()
	  	If StartMoving
	  		xx.l = WindowMouseX(#MainWindow)
			If Not LastMouseLocation = xx
				LastMouseLocation = xx
				xx = xx - MovingMouseOffset
				Debug Str(xx)
				ResizeWindow(#MainWindow, xx, 0, #PB_Ignore, #PB_Ignore)
			EndIf
	  	EndIf
	  	If Event = #PB_Event_Gadget
	  		SelectedGadget.l = EventGadget()
		  	If SelectedGadget = #MoveButton
		  		If StartMoving
		  			StartMoving = #False
		  			Debug "Stop Moving"
		  		Else
		  			StartMoving = #True
		  			Debug "Start Moving"
		  			MovingMouseOffset = WindowMouseX(#MainWindow) - WindowX(#MainWindow, #PB_Window_FrameCoordinate)
		  		EndIf
		    EndIf
	    EndIf
  	Until Event = #PB_Event_CloseWindow
EndIf
Last edited by SkyManager on Wed Aug 14, 2013 6:03 am, edited 2 times in total.
User avatar
roslansalleh
User
User
Posts: 19
Joined: Fri Dec 21, 2012 3:45 am

Re: Moving gadget

Post by roslansalleh »

I think you should use resizegadget command to move the gadget.
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Moving gadget

Post by TI-994A »

SkyManager wrote:I want to move my gadget but it is not working..
..Is there any fastest solution?
Hello SkyManager. I believe that if a callback were implemented to capture the mouse messages, it might be faster, and perhaps more efficient too. However, it would result in an OS-dependent solution. This example, on the other hand, which is an adaptation of an example that I posted in an earlier thread, is cross-platform:

Code: Select all

EnableExplicit
InitNetwork()

Enumeration
  #MainWindow
  #Button
  #Label
  #Image
EndEnumeration

Define wFlags, appQuit, dragOn, buttonMouseXOffset, buttonMouseYOffset

If ReceiveHTTPFile("https://dl.dropboxusercontent.com/u/38177172/PBHome.bmp",
                   GetTemporaryDirectory() + "PBHome.bmp")
  LoadImage(#Image, GetTemporaryDirectory() + "PBHome.bmp")

  wFlags = #PB_Window_SystemMenu |#PB_Window_ScreenCentered
  OpenWindow(#MainWindow, #PB_Any, #PB_Any, 640, 480, "Drag&Drop Gadgets", wFlags)
  TextGadget(#Label, 10, 10, 620, 30, "...click on the top-left corner of the " +
             "button to move it, then click again to drop it at the new position.")
  ButtonImageGadget(#Button, 20, 40, 200, 200, ImageID(#Image))

  Repeat
    If dragOn
      ResizeGadget(#Button, WindowMouseX(#MainWindow) - buttonMouseXOffset,
                   WindowMouseY(#MainWindow) - buttonMouseYOffset, #PB_Ignore, #PB_Ignore)
    EndIf
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Button
            Select EventType()
              Case #PB_EventType_LeftClick
                If dragOn
                  dragon = 0
                Else
                  buttonMouseXOffset = WindowMouseX(#MainWindow) +
                  -(GadgetX(#Button, #PB_Gadget_WindowCoordinate))
                  buttonMouseYOffset = WindowMouseY(#MainWindow) + 
                  -(GadgetY(#button, #PB_Gadget_WindowCoordinate))
                  If buttonMouseXOffset < 20 And 
                     buttonMouseYOffset < 20
                    dragOn = 1
                  EndIf
                EndIf
            EndSelect
        EndSelect
    EndSelect
  Until appQuit = 1
EndIf
Unlike the CanvasGadget() in my earlier example, some gadgets don't process mouse events like button up, button down, move, etc. As a workaround, this example activates the move event when the gadget is clicked on its extreme top-left corner, and dropped in its new location when clicked again, and uses the window's mouse coordinates to position the move. Easily adaptable to personal requirements. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Moving gadget

Post by BasicallyPure »

roslansalleh is correct. If you want to move a gadget you should use ResizeGadget(), not ResizeWindow().
IF you describe in detail what it is you want then someone can produce a working example.
As is, there is not enough information to determine exactly what you want.
Looking at your code makes me think you may be trying to move your entire window along the top of the desktop.

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: Moving gadget

Post by SkyManager »

I am trying to move (relocate) the window
not one gadget
because inside the window, there are a dozen of gadgets
User avatar
TI-994A
Addict
Addict
Posts: 2700
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Moving gadget

Post by TI-994A »

SkyManager wrote:I am trying to move (relocate) the window not one gadget
because inside the window, there are a dozen of gadgets
Hello again SkyManager. The thread title, Moving Gadget, led me to assume that your requirement was to actually move gadgets. In any case, here's the modified code to move the window (programmatically at least):

Code: Select all

EnableExplicit
InitNetwork()

Enumeration
  #MainWindow
  #Button
  #Label
  #Image1
  #Image2
EndEnumeration

Define wFlags, appQuit, dragOn, windowMouseXOffset, windowMouseYOffset

If ReceiveHTTPFile("https://dl.dropboxusercontent.com/u/38177172/bt1.bmp",
                   GetTemporaryDirectory() + "bt1.bmp") And
   ReceiveHTTPFile("https://dl.dropboxusercontent.com/u/38177172/bt2.bmp",
                   GetTemporaryDirectory() + "bt2.bmp") And
  LoadImage(#Image1, GetTemporaryDirectory() + "bt1.bmp") 
  LoadImage(#Image2, GetTemporaryDirectory() + "bt2.bmp")

  wFlags = #PB_Window_SystemMenu |#PB_Window_ScreenCentered
  OpenWindow(#MainWindow, #PB_Any, #PB_Any, 220, 200, "Move Window", wFlags)
  TextGadget(#Label, 0, 10, 220, 30, "click on the button to move the window...", #PB_Text_Center)
  ButtonImageGadget(#Button, 40, 40, 140, 125, ImageID(#Image1))

  Repeat
    If dragOn
      ResizeWindow(#MainWindow, DesktopMouseX() - windowMouseXOffset,
                   DesktopMouseY() - windowMouseYOffset, #PB_Ignore, #PB_Ignore)
    EndIf
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Button
            Select EventType()
              Case #PB_EventType_LeftClick
                If dragOn
                  dragon = 0
                  SetGadgetAttribute(#Button, #PB_Button_Image, ImageID(#Image1))
                Else
                  windowMouseXOffset = DesktopMouseX() - WindowX(#MainWindow)
                  windowMouseYOffset = DesktopMouseY() - WindowY(#MainWindow)
                  SetGadgetAttribute(#Button, #PB_Button_Image, ImageID(#Image2))
                  dragOn = 1
                EndIf
            EndSelect
        EndSelect
    EndSelect
  Until appQuit = 1
EndIf
Hope this is what you're looking for. :?
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: Moving gadget

Post by SkyManager »

Sorry for my title which misleading you.
I will try your suggestion.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Moving gadget

Post by BasicallyPure »

@TI-994A
Thank you for the example.
Moving a borderless window is something I had never been able to do until I saw your example above.
I arranged it so now you can drag the window around while holding the mouse button down.
This binary counter has no practical use as far as I know, it's just for demonstration.
I have added a popup menu to use as a way to exit the program.

BP

Edit: fixed bug

Code: Select all

; BinaryCounter.pb
; Demonstrate how to drag a borderless window
; By BasicallyPure
; 8/4/2013

EnableExplicit

#MainWin   = 0
#Canvas1   = 0
#PopUpMenu = 0
#MenuChoice_OnTop = 0
#MenuChoice_Quit  = 1

Global Dim c(7)
   c(0) = $FFFF00 : c(1) = $00FFFF : c(2) = $00FF00 : c(3) = $0000FF
   c(4) = $FF00FF : c(5) = $FF0000 : c(6) = $808080 : c(7) = $FFFFFF

Declare INIT_GUI()
Declare UPDATE_COUNTER(k.i)
Declare EVENT_LOOP(count.i)

If INIT_GUI()
   EVENT_LOOP(Date() << 3)
EndIf

; procedures
Procedure EVENT_LOOP(count.i)
   Protected.i OffsetX, OffsetY, drag, TopState, Quit
   
   Repeat
      If drag ; move the window
         ResizeWindow(#MainWin,DesktopMouseX()-OffsetX,DesktopMouseY()-OffsetY,#PB_Ignore,#PB_Ignore)
      EndIf
      Select WaitWindowEvent()
         Case #PB_Event_CloseWindow : Quit = #True
         Case #PB_Event_RightClick : DisplayPopupMenu(#PopUpMenu, WindowID(#MainWin))
         Case #PB_Event_Timer : count + 1 : UPDATE_COUNTER(count)
         Case #PB_Event_Menu
            Select EventMenu()
               Case #MenuChoice_OnTop : TopState ! 1
                  StickyWindow(#MainWin, TopState)
                  SetMenuItemState(#PopUpMenu, #MenuChoice_OnTop, TopState)
               Case #MenuChoice_Quit : Quit = #True
            EndSelect
         Case #PB_Event_Gadget
            If EventGadget() = #Canvas1
               Select EventType()
                  Case #PB_EventType_LeftButtonDown
                     drag = #True
                     OffsetX = DesktopMouseX() - WindowX(#MainWin)
                     OffsetY = DesktopMouseY() - WindowY(#MainWin)
                  Case  #PB_EventType_LeftButtonUp
                     drag = #False
                  Case #PB_EventType_RightButtonDown
                     DisplayPopupMenu(#PopUpMenu, WindowID(#MainWin))
               EndSelect
            EndIf
         EndSelect
      Until Quit
   EndProcedure
   
Procedure UPDATE_COUNTER(k.i)
   Protected.i x, c
   
   StartDrawing(CanvasOutput(#Canvas1))
      For x = 320 To 20 Step -20
         If k & 1
            Circle(x, 25, 6, c(c & %111))
         Else
            Circle(x, 25, 6, $106218)
         EndIf
         c + 1
         k >> 1
      Next
   StopDrawing()
EndProcedure

Procedure INIT_GUI()
   Protected flags.i  = #PB_Window_ScreenCentered | #PB_Window_BorderLess
   Protected Result.i = OpenWindow(#MainWin, 0, 0, 344, 54, "Clock", flags)
   
   If Result
      If CreatePopupMenu(#PopUpMenu)
         MenuItem(#MenuChoice_OnTop, "Always on top")
         MenuItem(#MenuChoice_Quit,"Quit")
      Else : Result = 0
      EndIf
      
      If CanvasGadget(#Canvas1,2,2,340,50)
         StartDrawing(CanvasOutput(#Canvas1))
            Box(0,0,340,50,$350015)
         StopDrawing()
      Else : Result = 0
      EndIf
      
      If AddWindowTimer(#MainWin, 0, 125) = 0
         Result = 0
      EndIf
      
   Else : Result = 0
   EndIf
   
   ProcedureReturn Result
EndProcedure
Last edited by BasicallyPure on Wed Aug 14, 2013 7:21 am, edited 3 times in total.
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Moving gadget

Post by Demivec »

@BasicallyPure: I run into problems using your code if I click the top left border of the window and try to move the window. It would seem I am technically outside of the window then and can never come back inside to drop the window. My mouse pointer then gains a permanent friend to follow it around the screen. :)

Here's a link to some older code that is Windows specific and uses a windowed screen instead of a canvas gadget. It still functions without any modifications. I made some attempts at converting it to use a canvas gadget also but erred in the code to move the window. If I work it out later I may post that version also.
User avatar
BasicallyPure
Enthusiast
Enthusiast
Posts: 539
Joined: Thu Mar 24, 2011 12:40 am
Location: Iowa, USA

Re: Moving gadget

Post by BasicallyPure »

@Demivec
Thanks for pointing out the bug.
It should be working ok now.

The problem was if you click on the border then drag the mouse anywhere over the canvas and release.
This gives a LeftButtonUp event without a LeftButtonDown event.
The way the code was written it would toggle drag on or off with either event and would get stuck when
it got an odd number of events.

BP
BasicallyPure
Until you know everything you know nothing, all you have is what you believe.
User avatar
JHPJHP
Addict
Addict
Posts: 2251
Joined: Sat Oct 09, 2010 3:47 am

Re: Moving gadget

Post by JHPJHP »

Just another option (Windows Only)

Code: Select all

Global OffsetInfo.POINT
#WM_LBUTTONDOUBLECLICK = $203

Procedure WindowCallBack(hWnd, Msg, wParam, lParam)
  Select Msg
    Case #WM_LBUTTONDOWN
      CursorInfo.POINT

      If wParam = 1
        SetCapture_(WindowID(0))
        GetCursorPos_(CursorInfo)
        OffsetInfo\x = CursorInfo\x - WindowX(0)
        OffsetInfo\Y = CursorInfo\Y - WindowY(0)
      EndIf
    Case #WM_LBUTTONUP
      ReleaseCapture_()
    Case #WM_MOUSEMOVE
      CursorInfo.POINT

      If wParam = 1
        GetCursorPos_(CursorInfo)
        newX = CursorInfo\x - OffsetInfo\x
        newY = CursorInfo\Y - OffsetInfo\Y
        ResizeWindow(0, newX, newY, #PB_Ignore, #PB_Ignore)
      EndIf
    Case #WM_LBUTTONDOUBLECLICK
      SendMessage_(WindowID(0), #PB_Event_CloseWindow, 0, 0)
  EndSelect
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure

If OpenWindow(0, 0, 0, 200, 200, "", #PB_Window_BorderLess | #PB_Window_ScreenCentered)
  SetWindowCallback(@WindowCallBack())
  TextGadget(0, 25, 70, 150, 20, "Double-Click to Close", #PB_Text_Center)
  DisableGadget(0, 1)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
End

If you're not investing in yourself, you're falling behind.

My PureBasic StuffFREE STUFF, Scripts & Programs.
My PureBasic Forum ➤ Questions, Requests & Comments.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: Moving gadget

Post by RASHAD »

Moving gadget or moving border less window ?
Code for moving gadget

Code: Select all


If OpenWindow(0, 0, 0, 700, 500, "Test", #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_SizeGadget)
  SetWindowColor(0,$BDBDBF)
  StringGadget(1, 10, 10, 100, 60,"")
  ButtonGadget(3,10,110,60,22,"TEST")  

  Repeat
    Select  WaitWindowEvent()
    
      Case #PB_Event_Gadget
          Select EventGadget()
           Case 3 
              If GetWindowLongPtr_(GadgetID(1), #GWL_STYLE) & #WS_THICKFRAME
                  SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE)&~ #WS_THICKFRAME)
                  ResizeGadget(1,GadgetX(1), GadgetY(1),GadgetWidth(1)-2, GadgetHeight(1)-2)
              Else
                  SetWindowLongPtr_(GadgetID(1), #GWL_STYLE, GetWindowLongPtr_(GadgetID(1), #GWL_STYLE)|#WS_THICKFRAME)
                  ResizeGadget(1,GadgetX(1), GadgetY(1),GadgetWidth(1)-2, GadgetHeight(1)-2)
              EndIf 
          EndSelect
    
      Case #WM_CLOSE
        Quit = 1        
           
            
      Case #WM_MOUSEMOVE      
            Gad = GetActiveGadget()
            If Gad = 1 Or Gad = 3
              ReleaseCapture_()
              SendMessage_(GadgetID(Gad), #WM_NCLBUTTONDOWN, #HTCAPTION, 0)
              MoveWindow_(GadgetID(Gad), GadgetX(Gad), GadgetY(Gad), GadgetWidth(Gad), GadgetHeight(Gad), 1)
            EndIf
        
    EndSelect
  Until Quit = 1
EndIf
End

Egypt my love
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: Moving gadget

Post by SkyManager »

Sorry for the incorrect captioned "Moving gadget" which misleads you
Actually, I want to move my borderless window.

Thanks all of you,
My window can be moved now.
SkyManager
Enthusiast
Enthusiast
Posts: 339
Joined: Tue Jan 30, 2007 5:47 am
Location: Hong Kong

Re: [Solved] Moving gadget

Post by SkyManager »

Hi everybody,

Thanks for all the expert advices

Finally, I have successfully created a test for moving my borderless window

My codes is attached herebelow, I hope it can help others.

Code: Select all

EnableExplicit
Global drag.l = #False
Global Quit.l = #False
Global OffsetX.l = 0
Global OffsetY.l = 0
Enumeration
	#MainWin
	#Label
EndEnumeration
If Not OpenWindow(#MainWin, 0, 0, 80, 25, "Clock", #PB_Window_ScreenCentered | #PB_Window_BorderLess)
	End
EndIf
ButtonGadget(#Label,0,0,80,25,"Test")
SetGadgetColor(#Label,#PB_Gadget_BackColor,$00FFFF)
Repeat
	If drag ; move the window
		ResizeWindow(#MainWin, DesktopMouseX()-OffsetX, DesktopMouseY()-OffsetY, #PB_Ignore, #PB_Ignore)
	EndIf
	Select WaitWindowEvent()
		Case #PB_Event_CloseWindow : Quit = #True
		Case #PB_Event_Gadget
			If EventGadget() = #Label
				Select EventType()
					Case #PB_EventType_LeftClick
						If (drag)
							drag = #False
						Else
							drag = #True
							OffsetX = DesktopMouseX() - WindowX(#MainWin)
							OffsetY = DesktopMouseY() - WindowY(#MainWin)
						EndIf
				EndSelect
			EndIf
	EndSelect
Until Quit
Post Reply