Basic Univers
; Affiche un rectangle de sélection (celui qu'on obtient)
;   quand on maintient la touche gauche de la souris enfoncée
;   et qu'on déplace le curseur


Procedure Set_Rect(x1, y1, x2, y2, *FRect.Rect)
  If x2EndIf
  If y2EndIf
  If x1<0 : x1 = 0 : EndIf
  If y1<0 : y1 = 0 : EndIf
  *FRect\left = x1
  *FRect\Top = y1
  *FRect\Right = x2
  *FRect\Bottom = y2
EndProcedure

If OpenWindow(0, 0, 0, 745, 425, "Démo Rectangle de sélection", #PB_Window_SystemMenu|#PB_Window_ScreenCentered| #PB_Window_MinimizeGadget)
  hdc = GetDC_(WindowID(0))
  DraggingOn = 0
  Repeat
    EventID = WaitWindowEvent()
    If EventID.l = #WM_LBUTTONDOWN
      GetCursorPos_(@StartPos.Point) ; On mémorise la position de départ du curseur
      GetWindowRect_(WindowID(0) , @SDRect.RECT)
      If PtInRect_(SDRect, StartPos\x, StartPos\y) ; On vérifie que le curseur est dans la fenêtre
        StartPos\x - SDRect\Left - 2 ; On récupère les coordonnées relatives
        StartPos\y - SDRect\Top - 27
        MPos.Point\x = StartPos\x
        MPos\y = StartPos\y
        DraggingOn = 1
        Set_Rect(WindowX(0)+ 3, WindowY(0)+ 29, WindowX(0)+ 3 + WindowWidth(0), WindowY(0)+ 29 + WindowHeight(0), WRect.Rect)
        ClipCursor_(WRect) ; on capture le curseur souris dans notre fenêtre pour être sûr de récupérer le #WM_LBUTTONUP
      EndIf
    EndIf

    If GetAsyncKeyState_(#VK_LBUTTON) And DraggingOn ; Tant que le bouton est maintenu cliqué
      GetCursorPos_(@OverPos.Point)
      GetWindowRect_(WindowID(0) , @SDRect.RECT)
      If PtInRect_(SDRect, OverPos\x, OverPos\y) ; On vérifie que le curseur est dans la fenêtre
        OverPos\x - SDRect\Left - 2 ; On récupère les coordonnées relatives
        OverPos\y - SDRect\Top - 27
        If MPos.Point\x<>StartPos.Point\x Or MPos\y<>StartPos\y  ; Si on s'est déplacé depuis le départ
          Set_Rect(StartPos\x, StartPos\y, MPos.Point\x, MPos\y, FRect.Rect)
          DrawFocusRect_(hdc, FRect) ; On efface la version précédente
        EndIf
        If MPos.Point\x<>OverPos\x Or MPos\y<>OverPos\y
          Set_Rect(StartPos\x, StartPos\y, OverPos\x, OverPos\y, FRect.Rect)
          MPos\x = OverPos\x ; On mémorise les coordonnées du nouveau tracé
          MPos\y = OverPos\y
          DrawFocusRect_(hdc, FRect) ; Et on le traçe
        EndIf
      EndIf
    EndIf
   
    If EventID.l = #WM_LBUTTONUP And DraggingOn
      Set_Rect(StartPos\x, StartPos\y, MPos.Point\x, MPos\y, FRect.Rect)
      DrawFocusRect_(hdc, FRect) ; On efface le dernier tracé
      DraggingOn = 0
      ClipCursor_(0) ; on libère le curseur
    EndIf
  Until  EventID = #PB_Event_CloseWindow

  ReleaseDC_(WindowID(0), hdc)
EndIf