ResizeWindow()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

ResizeWindow()

Post by IdeasVacuum »

Expand ResizeWindow:

Code: Select all

ResizeWindow(#Window, x, y, Width, Height [, Flag [, ParentWindowID]])
Flag: #PB_Window_WindowCentered
This will allow the developer to ensure that pre-defined Windows will always be centered to the parent when displayed [HideWindow(#MyWin, #False)], no matter where the User has moved the parent (including to another monitor).

How to do it in the meantime:

Code: Select all

Procedure.i iAbs(val.i)
;----------------------
              EnableASM

                          CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
                          
                                         MOV eax, val
                                         NEG eax
                                         CMOVS eax, val
                          CompilerElse
                                         MOV rax, val
                                         NEG rax
                                         CMOVS rax, val
                          CompilerEndIf
              DisableASM

              ProcedureReturn
EndProcedure

iWinX.i = (WindowX(#WinParent) + ( iAbs(WindowWidth(#WinParent)  - WindowWidth(#WinChild)) ) / 2)
iWinY.i = (WindowY(#WinParent) + ( iAbs(WindowHeight(#WinParent) - WindowHeight(#WinChild)) ) / 2)

ResizeWindow(#WinChild, iWinX, iWinY, #PB_Ignore, #PB_Ignore)
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
mestnyi
Addict
Addict
Posts: 1098
Joined: Mon Nov 25, 2013 6:41 am

Re: ResizeWindow()

Post by mestnyi »

Code: Select all

ProcedureDLL IDGadget(GadgetID) ;Returns purebasic Gadget ID from GadgetID
  Protected Gadget  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows 
      Gadget = GetProp_(GadgetID, "PB_ID")
      If IsGadget(Gadget) And GadgetID(Gadget) = GadgetID
        ProcedureReturn Gadget 
      EndIf
    CompilerDefault
      
  CompilerEndSelect
  ProcedureReturn -1 
EndProcedure  

ProcedureDLL IDWindow(WindowID) ;Returns purebasic Window ID from WindowID
  Protected Window  
  CompilerSelect #PB_Compiler_OS
    CompilerCase #PB_OS_Windows 
      Window = GetProp_(WindowID, "PB_WindowID") -1
      If IsWindow(Window) And WindowID(Window) = WindowID
        ProcedureReturn Window 
      EndIf
    CompilerDefault
      
  CompilerEndSelect
  ProcedureReturn -1 
EndProcedure  


ProcedureDLL IsWindowID(WindowID) ;Returns TRUE if is WindowID
  If IsWindow(IDWindow(WindowID))
    ProcedureReturn #True
  EndIf  
EndProcedure  

ProcedureDLL IsGadgetID(GadgetID);Returns TRUE if is GadgetID
  If IsGadget(IDGadget(GadgetID))
    ProcedureReturn #True
  EndIf  
EndProcedure  

ProcedureDLL GetParent(ChildID) ;Returns ParentID from ChildID (Default)
  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    ProcedureReturn GetParent_(ChildID)
  CompilerElseIf #PB_Compiler_OS = #PB_OS_Linux
    ProcedureReturn gtk_widget_get_parent_(ChildID)
  CompilerEndIf
EndProcedure 

ProcedureDLL GetParentID(ChildID) ;Returns ParentID from ChildID
  While ChildID
    ChildID = GetParent(ChildID)
    If IsWindowID(ChildID)
      ProcedureReturn ChildID
    ElseIf IsGadgetID(ChildID)
      ProcedureReturn ChildID
    EndIf
  Wend  
EndProcedure

ProcedureDLL WindowParentID(Window) ;Returns ParentID from Child Window
  If IsWindow(Window) 
    Window = WindowID(Window)
  EndIf
  ProcedureReturn GetParentID(Window)
EndProcedure

ProcedureDLL InnerX(Gadget) ;Returns the Container inner X coordinate from the Gadget
  If (GadgetType(Gadget) = #PB_GadgetType_Container Or
      GadgetType(Gadget) = #PB_GadgetType_ScrollArea)
    ProcedureReturn 0 
  ElseIf GadgetType(Gadget) = #PB_GadgetType_Panel
    If GetGadgetAttribute(Gadget, #PB_Panel_ItemWidth)
      ProcedureReturn 0
    Else
      ProcedureReturn 1
    EndIf
  EndIf 
EndProcedure 

ProcedureDLL InnerY(Gadget) ;Returns the Container inner Y coordinate from the Gadget
  If (GadgetType(Gadget) = #PB_GadgetType_Container Or
      GadgetType(Gadget) = #PB_GadgetType_ScrollArea)
    ProcedureReturn 0 
  ElseIf GadgetType(Gadget) = #PB_GadgetType_Panel
    If GetGadgetAttribute(Gadget, #PB_Panel_ItemHeight)
      ProcedureReturn 0
    Else
      ProcedureReturn 3
    EndIf
  EndIf 
EndProcedure 

ProcedureDLL InnerWidth(Gadget) ;Returns the Container inner Width coordinate from the Gadget
  Protected Result,Width
  If (GadgetType(Gadget)=#PB_GadgetType_Container Or GadgetType(Gadget)=#PB_GadgetType_ScrollArea)
    OpenGadgetList(Gadget) :Result = TextGadget(#PB_Any,0,0,0,0,"") :CloseGadgetList()
    Width = (GadgetX(Result,#PB_Gadget_WindowCoordinate) - GadgetX(Gadget,#PB_Gadget_WindowCoordinate))*2
    If IsGadget(Result) :FreeGadget(Result) :EndIf
    Width=GadgetWidth(Gadget)-Width
    ProcedureReturn Width 
  ElseIf GadgetType(Gadget)=#PB_GadgetType_Panel :Width = GetGadgetAttribute(Gadget,#PB_Panel_ItemWidth)
    If Width
      ProcedureReturn Width
    Else
      ProcedureReturn GadgetWidth(Gadget)-4
    EndIf  
  Else
    ProcedureReturn GadgetWidth(Gadget) 
  EndIf 
EndProcedure 

ProcedureDLL InnerHeight(Gadget) ;Returns the Container inner Height coordinate from the Gadget
  Protected Result,Height
  If (GadgetType(Gadget)=#PB_GadgetType_Container Or GadgetType(Gadget)=#PB_GadgetType_ScrollArea)
    OpenGadgetList(Gadget) :Result = TextGadget(#PB_Any,0,0,0,0,"") :CloseGadgetList()
    Height = (GadgetY(Result,#PB_Gadget_WindowCoordinate) - GadgetY(Gadget,#PB_Gadget_WindowCoordinate))*2
    If IsGadget(Result) :FreeGadget(Result) :EndIf
    Height=GadgetHeight(Gadget)-Height
    ProcedureReturn Height 
  ElseIf GadgetType(Gadget)=#PB_GadgetType_Panel :Height = GetGadgetAttribute(Gadget,#PB_Panel_ItemHeight)
    If Height
      ProcedureReturn Height
    Else
      ProcedureReturn GadgetHeight(Gadget)-5
    EndIf  
  Else
    ProcedureReturn GadgetHeight(Gadget)
  EndIf 
EndProcedure 


ProcedureDLL ResizeWindowCentered(Window, ParentID=0)
  Protected x,y
  If ParentID =0 :ParentID = WindowParentID(Window) :EndIf  
  If IsWindowID(ParentID)
    x =WindowWidth(IDWindow(ParentID)) -(WindowX(Window,#PB_Window_FrameCoordinate)+WindowWidth(Window,#PB_Window_FrameCoordinate))
    x =WindowX(Window,#PB_Window_FrameCoordinate) +((x -WindowX(Window,#PB_Window_FrameCoordinate)) /2)
    y =WindowHeight(IDWindow(ParentID)) -(WindowY(Window,#PB_Window_FrameCoordinate)+WindowHeight(Window,#PB_Window_FrameCoordinate))
    y =WindowY(Window,#PB_Window_FrameCoordinate) + ((y -WindowY(Window,#PB_Window_FrameCoordinate)) / 2)
  ElseIf IsGadgetID(ParentID)  
    x =InnerWidth(IDGadget(ParentID)) -(WindowX(Window,#PB_Window_FrameCoordinate)+WindowWidth(Window,#PB_Window_FrameCoordinate))
    x =WindowX(Window,#PB_Window_FrameCoordinate) +((x -WindowX(Window,#PB_Window_FrameCoordinate)) /2)
    y =InnerHeight(IDGadget(ParentID)) -(WindowY(Window,#PB_Window_FrameCoordinate)+WindowHeight(Window,#PB_Window_FrameCoordinate))
    y =WindowY(Window,#PB_Window_FrameCoordinate) + ((y -WindowY(Window,#PB_Window_FrameCoordinate)) / 2)
  EndIf
  ResizeWindow(Window,x,y,#PB_Ignore,#PB_Ignore)
EndProcedure



ProcedureDLL ResizeWindowEx(Window, x, y, Width, Height) ;Resize the specified window to the given dimensions
  If IsWindow(Window) 
    Protected ParentID = WindowParentID(Window)
    Protected Parent = IDWindow(ParentID)
    If IsWindowID(ParentID) 
      If x = #PB_Ignore
        x = WindowX(Window, #PB_Window_FrameCoordinate) - WindowX(Parent, #PB_Window_InnerCoordinate)
      EndIf
      If y = #PB_Ignore
        y = WindowY(Window, #PB_Window_FrameCoordinate) - WindowY(Parent, #PB_Window_InnerCoordinate)
      EndIf
      If Width <> #PB_Ignore
        Width = Width - ((WindowX(Window,#PB_Window_InnerCoordinate) - WindowX(Window,#PB_Window_FrameCoordinate))*2)
      EndIf
      If Height <> #PB_Ignore
        Height = Height - ((WindowY(Window,#PB_Window_InnerCoordinate) - WindowY(Window,#PB_Window_FrameCoordinate)) + (WindowX(Window,#PB_Window_InnerCoordinate) - WindowX(Window,#PB_Window_FrameCoordinate)))
      EndIf
    EndIf
    ProcedureReturn ResizeWindow(Window, x, y, Width, Height)
  EndIf
EndProcedure


Macro OpenWindowEx(Window, x,y,w,h, title, flags=0, ParentID=0)
  OpenWindow(Window, x,y,w,h, title, flags, ParentID)
  
   If flags & #PB_Window_WindowCentered
    ResizeWindowCentered(Window, ParentID)
   EndIf
EndMacro

CompilerIf #PB_Compiler_IsMainFile
  flags = #PB_Window_Invisible | #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered 
  hWnd=OpenWindowEx(0, 0, 0, 800, 600, "Main Form",#WS_EX_COMPOSITED |flags )
  
  
  If GetWindowLongPtr_(WindowID(0), #GWL_STYLE) & #WS_EX_COMPOSITED = #False 
    SetWindowLongPtr_(WindowID(0),#GWL_STYLE, GetWindowLongPtr_(WindowID(0), #GWL_STYLE)|#WS_EX_COMPOSITED)
  EndIf
  
  
  flags = #PB_Window_Invisible | #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget| #PB_Window_SizeGadget 
  OpenWindowEx(1, 10, 20, 280, 60, "Child Form",#WS_CHILD|#WS_POPUP |#PB_Window_WindowCentered|flags, WindowID(0))
  
  If GetWindowLong_(WindowID(1), #GWL_STYLE) & #WS_CHILD = #False 
    ;SetWindowLongPtr_(WindowID(1),#GWL_STYLE, GetWindowLongPtr_(WindowID(1), #GWL_STYLE)|#WS_CHILD|#WS_POPUP);
  EndIf
  
  
  SetParent_(WindowID(1), WindowID(0))
  
  
  Macro ResizeWindow(Window, x, y, w, h) :ResizeWindowEx(Window, x, y, w, h) :EndMacro
  ResizeWindow(1, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)    
  
  HideWindow(0,0)
  HideWindow(1,0)
  
  
  
  Repeat
    Event=WaitWindowEvent()
  Until Event=#PB_Event_CloseWindow
CompilerEndIf
Post Reply