Resize Gadgets R1.1 (Windows)

Share your advanced PureBasic knowledge/code with the community.
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Resize Gadgets R1.1 (Windows)

Post by RichardL »

Hi,
I have used PureResize() for a long time, but as the developer seems to have gone silent I'm having to write my own version so I can upgrade to the most recent version of PB.
PureResize has some elegant features that I do not use so this simple alternative is a bare bones alternative.

Comments and reports welcomed
RichardL

Original code 10th May 2014
Revised code 15th May 2014 - No longer needs the 'Window#' to be passed to SetGadgetResize().
I had not realised that GetActiveWindow() would return the Window# immediately it was opened.
Also, added <While WindowEvent() : Wend> after the ResizeGadget() to force the gadget to be re-rendered before the next change is made. The result is much smoother operation, probably at the cost of increased processor load... but it seems to be negligible.

Save this code as 'Resize.pbi'...

Code: Select all

; A basic gadget resizer

Structure RESIZER
  WinNum.l
  OldWindowHeight.l
  OldWindowWidth.l  
  
  GadNum.l
  FlagX.l
  FlagY.l
  FlagW.l
  FlagH.l
  
EndStructure

Global NumReSizeGads = 1
Global Dim ReSize.RESIZER(1)

Procedure SetGadgetResize(GadNum,FlagX,FlagY,FlagWidth,FlagHeight)
  ; Add a gadget to the list of gadgets that are to be re-sized.
  If NumReSizeGads > 1
    Redim ReSize(NumReSizeGads)
  EndIf
  
  GadWin = GetActiveWindow()
  With ReSize(NumReSizeGads)
    \WinNum          = GetActiveWindow()
    \OldWindowHeight = WindowHeight(GadWin)
    \OldWindowWidth  = WindowWidth(GadWin)
    
    \GadNum = GadNum
    \FlagX  = FlagX & 1
    \FlagY  = FlagY & 1
    \FlagW  = FlagWidth & 1
    \FlagH  = FlagHeight & 1
    
  EndWith
  
  NumReSizeGads + 1
  
EndProcedure

 
Procedure WinCallback(Win,msg,wParam,lParam) 
  Protected dx,dy,FoundWin,n
  Protected NewX,NewY,NewW,NewH
  Protected Result =  #PB_ProcessPureBasicEvents 
  
  Select msg 
      
    ; Window is resized, so adjust gadget positions/size etc
    Case #WM_SIZE  ;{ 
      FoundWin = #False
      For n = 1 To NumReSizeGads-1 
        
        If WindowID(ReSize(n)\WinNum) = Win
          
          ; How much has window changed?
          dy = WindowHeight(ReSize(n)\WinNum) - ReSize(n)\OldWindowHeight   
          dx = WindowWidth(ReSize(n)\WinNum)  - ReSize(n)\OldWindowWidth           
          
          ; Save the new window size.
          ReSize(n)\OldWindowHeight = WindowHeight(ReSize(n)\WinNum)
          ReSize(n)\OldWindowWidth  = WindowWidth(ReSize(n)\WinNum)          
          
          FoundWin = #True
          Break
          
        EndIf
      Next
      
      ; Adjust the gadget sizes / positions
      If FoundWin 
        For n = 1 To NumReSizeGads-1 
          With ReSize(n)
            If WindowID(\WinNum) = Win
              NewX = (dx * \FlagX) + GadgetX(\GadNum)
              NewY = (dy * \FlagY) + GadgetY(\GadNum)
              NewW = (dx * \FlagW) + GadgetWidth(\GadNum)
              NewH = (dy * \FlagH) + GadgetHeight(\GadNum)
              ResizeGadget(\GadNum,NewX,NewY,NewW,NewH)
              While WindowEvent() : Wend
            EndIf
          EndWith
        Next
      EndIf
       ;}
       
      ; Example of reading a scroll bar (Vertical)
    Case #WM_VSCROLL          ;{
      If lParam = GadgetID(7)       
        T = GetGadgetState(7)
        SetGadgetText(6,Str(T))
      EndIf
      ;}
    
    ; Other callbacks here
      
      
  EndSelect 
  ProcedureReturn Result
EndProcedure 

'XInclude' the above code in your own application.... something like in this demo.

Code: Select all

XIncludeFile "Resize.pbi" ; Resize (a) Add gadget to list (b) Manage resizing

; Test code
OpenWindow(1,100,100,200,90,"Re-Size my height",#PB_Window_SystemMenu|#PB_Window_SizeGadget) 
SetWindowColor(1,RGB(255,255,0))
WindowBounds(1,200,90,200,400)

; Resize flags are ==========================>  (Gadget#,X,Y,W,H)
EditorGadget(1,5,5,190,20)               : SetGadgetResize(1,0,0,0,1) 
ButtonGadget(2,5,40,190,20,"Button 2")   : SetGadgetResize(2,0,1,0,0)

OpenWindow(2,420,100,425,90,"Re-Size Me 2 ways",#PB_Window_SystemMenu|#PB_Window_SizeGadget)
SetWindowColor(2,RGB(0,255,255))
WindowBounds(2,400,90,800,400)
EditorGadget(3,5,5,190,20)               : SetGadgetResize(3,0,0,0,1)
EditorGadget(4,200,5,195,20)             : SetGadgetResize(4,0,0,1,1)
ButtonGadget(5,5,40,190,40,"Button 4")   : SetGadgetResize(5,0,1,0,0)
StringGadget(6,340,65,50,20,"---")       : SetGadgetResize(6,1,1,0,0)
ScrollBarGadget(7,400,5,21,80,0,1023,1, #PB_ScrollBar_Vertical) :  SetGadgetResize(7,1,0,0,1)
SetGadgetState(7,512)

SetWindowCallback(@WinCallback()) 

Repeat 

  ; Your event management here
  
Until WaitWindowEvent() = #PB_Event_CloseWindow 
Last edited by RichardL on Mon May 12, 2014 2:27 pm, edited 2 times in total.
User avatar
Thorsten1867
Addict
Addict
Posts: 1372
Joined: Wed Aug 24, 2005 4:02 pm
Location: Germany

Re: AW: Resize Gadgets R1.0 (Windows)

Post by Thorsten1867 »

I had the same problem, because I used PureResize too.
If you need some additional ideas: ;-)

http://www.purebasic.fr/english/viewtop ... 87#p435994
Translated with http://www.DeepL.com/Translator

Download of PureBasic - Modules
Download of PureBasic - Programs

[Windows 11 x64] [PB V5.7x]
RichardL
Enthusiast
Enthusiast
Posts: 532
Joined: Sat Sep 11, 2004 11:54 am
Location: UK

Re: Resize Gadgets R1.1 (Windows)

Post by RichardL »

Hi,
See first posting for revised code which is slightly simpler and is smoother in operation.
Richard L
Post Reply