Snap a window to all 4 corners of a screen

Just starting out? Need help? Post your questions and find answers here.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Believe it or not, adding snapping to the sides to what's already there makes for simpler code. This version will snap to all four corners and all four sides:

Code: Select all

;====================================== 
; Program:    Window Snap Demo 
; Author:     netmaestro 
; Date:       October 23, 2006 
;====================================== 

Global screenW, screenH, SnapD = 50, AutoSnap=#True 

SystemParametersInfo_(#SPI_GETWORKAREA,0,@wa.RECT,0) ; Client area of the desktop 
ScreenW = wa\right 
screenH = wa\bottom 

Procedure Snap(hwnd, corner) 
  Protected w,h 
  GetWindowRect_(hwnd, @view.RECT) 
  w = view\right - view\left 
  h = view\bottom - view\top 
  Select corner 
    Case 1 
      MoveWindow_(hwnd, 0, 0, w, h, #True) 
    Case 2 
      MoveWindow_(hwnd, screenw-w,0,w,h,#True) 
    Case 3 
      MoveWindow_(hwnd,0, screenH-h,w,h,#True)    
    Case 4 
     MoveWindow_(hwnd, screenW-w,screenH-h,w,h,#True) 
  EndSelect 
EndProcedure 

Procedure CallBack(hwnd, msg, wParam, lParam) 

  Static snapped1,snapped2,snapped3,snapped4 
  result = #PB_ProcessPureBasicEvents 

  If msg = #WM_MOVING 
    *view.RECT = lparam 
    curwidth = *view\right - *view\left 
    curheight = *view\bottom - *view\top 
    
    ;============================== 
    ;  AutoSnap Section 
    ;============================== 
    If AutoSnap 
      If *view\left<SnapD 
        If Not Snapped1 
          *view\left=0: *view\right = curwidth 
          snapped1=#True 
          result=#True 
        EndIf 
      Else 
        snapped1=#False 
      EndIf 

      If *view\top < SnapD 
        If Not Snapped2 
          *view\top=0 : *view\bottom = curheight 
          snapped2=#True 
          result=#True 
        EndIf 
      Else 
          snapped2=#False 
      EndIf 
      
      If *view\right > screenw-SnapD 
        If Not Snapped3 
          *view\left=ScreenW-curwidth : *view\right=screenW 
          snapped3=#True 
          result=#True 
        EndIf 
      Else 
          snapped3=#False 
      EndIf      
      If *view\bottom > screenH-SnapD 
        If Not Snapped4 
           *view\top=screenH-curheight : *view\bottom = screenH 
          snapped4=#True 
          result=#True 
        EndIf 
      Else 
          snapped4=#False 
      EndIf      
    EndIf 

    ;============================== 
    ;  Inside Desktop Section 
    ;============================== 
    If *view\top < 0 : *view\top=0 : *view\bottom = curheight : EndIf 
    If *view\left < 0: *view\left=0 : *view\right = curwidth : EndIf 
    If *view\right > screenW : *view\right = screenW : *view\left = *view\right-curwidth : EndIf 
    If *view\bottom > screenH: *view\bottom = screenH : *view\top = *view\bottom-curheight : EndIf 
    MoveWindow_(hwnd,*view\left,*view\top,*view\right-*view\left,*view\bottom-*view\top,#True) 
    result = #True 

  EndIf 
  ProcedureReturn result 
EndProcedure 

OpenWindow(0,0,0,320,240,"The Dapper Snapper",$CF0001) 
CreateGadgetList(WindowID(0)) 
ButtonGadget(1,120,80,40,20,"Snap") 
ButtonGadget(2,160,80,40,20,"Snap") 
ButtonGadget(3,120,100,40,20,"Snap") 
ButtonGadget(4,160,100,40,20,"Snap") 
ButtonGadget(5,100,160,120,20,"AutoSnap On/Off") 
TextGadget(6,120,210,120,20,"AutoSnap is ON") 

SetWindowCallback(@Callback()) 
Repeat 
  EventID = WaitWindowEvent() 
  If EventID = #PB_Event_Gadget 
    Select EventGadget() 
      Case 1 
        Snap(WindowID(0), 1) 
      Case 2 
        Snap(WindowID(0), 2) 
      Case 3 
        Snap(WindowID(0), 3) 
      Case 4 
        Snap(WindowID(0), 4) 
      Case 5 
        AutoSnap = 1-AutoSnap 
        SetGadgetText(6, "AutoSnap is "+StringField("OFF|ON",AutoSnap+1,"|")) 
   EndSelect 
 EndIf 
  
Until EventID = #WM_CLOSE 
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4792
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post by Fangbeast »

When may I sell my daughter to pay you money for this???? This is a seriously great bit of code and help
Post Reply