Seite 1 von 1

GotoXY zum plazieren von gadgets

Verfasst: 07.05.2020 15:21
von hjbremer
UpDate vom 17.05.2020

Einen wunderschönen Guten Tag,

nun zu Corona Zeiten muß man sich beschäftigen. Also habe ich mein altes SetXY-Modul überarbeitet.

Da ich mit dem Form-Designer nicht warm werde, erstelle ich meine GUIs auf alt hergebrachte Weise.
Nur das ändern derselben ist manchmal nervig. Aber das kennt Ihr ja.

Wer also die Tradition hochhält, für den ist GotoXY gedacht. Als GotoXY.pbi abspeichern.

Code: Alles auswählen


;GotoXY, Gadgets verschieben, ab PB 5.41 x86 Windows 10 - Apr./Mai 2020 by HJBremer

;V.2.04  2 neue Flags: #goGadgetTop + #goGadgetLeft 
;V.2.05  move ist jetzt ein flag, wert für move steht in value
;        Parameter move kann #goCenter sein, gedacht für #goLeft #goRight um Vertikal zu zentrieren
;        Parameter move kann #goTop, #goBottom, #goLeft, #goRight sein um X+Y zu verschieben


DeclareModule GotoXY   
   
   Declare.i GotoXY(pbnr, parent, flag, gap=10, moveflag=0, value=0)
   
   ; pbnr   = Gadget welches verschoben wird
   ; parent = Gadget auf das Bezug genommen wird
   ; gap    = x und/oder y Abstand zwischen pbnr + parent, kann positiv/negativ sein
   ; move   = flag für zusätzlich verschieben, #goLeft, #goRight, #goTop, #goBottom, #goCenter=vertikal
   ; value  = für move
   
   ; flags
   If Defined(goLeft, #PB_Constant) = 0
      XIncludeFile "gotoxy-const.pbi"
   EndIf
   
   Declare.i SetGadgetSize(pbnr, w=2, h=0)
   Declare.i WriteText(pbnr, text$, fcolor=#PB_Default, bcolor=#PB_Default, fontid=#PB_Default)  
   
EndDeclareModule

Module GotoXY   
   EnableExplicit
   
   Procedure.i GotoXY(pbnr, parent, flag, gap=10, moveflag=0, value=0)
      
      If IsGadget(pbnr) = 0 : Debug "Gadget gibt es nicht: " + pbnr : ProcedureReturn : EndIf
      
      Protected x = GadgetX(pbnr), y = GadgetY(pbnr)                 ;x y Gadget Werte
      Protected w = GadgetWidth(pbnr), h = GadgetHeight(pbnr)        ;w h Gadget Werte
      
      Protected px, py, pw, ph, pth, parenttyp = #PB_GadgetType_Unknown  ;Parent Werte
      
      If IsGadget(parent) : parenttyp = GadgetType(parent) : EndIf
      
      If parenttyp = #PB_GadgetType_Unknown                          ; parent = Window
         px = WindowX(parent): py = WindowY(parent)                  
         pw = WindowWidth(parent): ph = WindowHeight(parent)  
      Else                                                           ; parent = Gadget
         px = GadgetX(parent): py = GadgetY(parent)
         pw = GadgetWidth(parent): ph = GadgetHeight(parent) 
         If parenttyp = #PB_GadgetType_Panel : pth = GetGadgetAttribute(parent, #PB_Panel_TabHeight) : EndIf
      EndIf
      
      Select flag
         Case #goLeft:           x = px - w - gap:       y = py               ;links neben dem Parent oben            
         Case #goRight:          x = px + pw + gap:      y = py               ;rechts neben dem Parent oben            
         Case #goLeftBottom:     x = px - w - gap:       y = py + ph - h      ;links neben dem Parent unten
         Case #goRightBottom:    x = px + pw + gap:      y = py + ph - h      ;rechts neben dem Parent unten   
            
         Case #goTopLeft:        x = px:                 y = py - h - gap     ;über dem Parent links (oder #goTop)      
         Case #goTopRight:       x = px + pw - w:        y = py - h - gap     ;über dem Parent rechts
         Case #goTopCenter:      x = px + ((pw - w)/2):  y = py - h - gap     ;über dem Parent Mitte            
         Case #goBottomLeft:     x = px:                 y = py + ph + gap    ;unter dem Parent links (#goBottom)
         Case #goBottomRight:    x = px + pw - w:        y = py + ph + gap    ;unter dem Parent rechts
         Case #goBottomCenter:   x = px + ((pw - w)/2):  y = py + ph + gap    ;unter dem Parent Mitte            
            
         Case #goGadgetFill:     x = gap: y = gap: w=pw-(2*gap): h=ph-(2*gap) ;ganzes Fenster ausfüllen
            
         Case #goGadgetWidth:    w = pw                   ;gleiche Breite wie Parent
         Case #goGadgetHeight:   h = ph                   ;gleiche Höhe wie Parent
         Case #goGadgetTop:      h = py - y - gap         ;bis zur oberen Seite Parent (Gadgets) - Abstand   
         Case #goGadgetLeft :    w = px - x - gap         ;bis zur linken Seite Parent (Gadgets) - Abstand
         Case #goGadgetRight:    w = px + pw - x          ;bis zur rechten Seite Parent (Gadgets)
         Case #goGadgetBottom:   h = py + ph - y          ;bis zur unteren Seite Parent (Gadgets) 
            
         Case #goWindowRight:    w = pw - x - gap         ;bis zum Rand vom Parent (Window) - Abstand
         Case #goWindowBottom:   h = ph - y - gap - pth   ;bis zum Rand vom Parent (Window) - Abstand
            
            ;- Oder Kombinationen für Parent = Window, Container, Panel                (+ anstatt | geht auch)
         Case #goTopLeft|#goInside:    x = gap:             y = gap                 ;Inside oben links (oder #goTop)
         Case #goTopRight|#goInside:   x = pw - w - gap:    y = gap                 ;     oben rechts 
         Case #goTopCenter|#goInside:  x = (pw - w) / 2:    y = gap                 ;     oben mitte            
         Case #goBottomLeft|#goInside:   x = gap:           y = ph - h - gap - pth  ;     unten links (#goBottom)
         Case #goBottomRight|#goInside:  x = pw - w - gap:  y = ph - h - gap - pth  ;     unten rechts
         Case #goBottomCenter|#goInside: x = (pw - w) / 2:  y = ph - h - gap - pth  ;     unten mitte            
            
         Default: Debug "unbekanntes Flag " + flag: ProcedureReturn            
      EndSelect
      
      If parenttyp = #PB_GadgetType_Frame And flag & #goInside : x + px : y + py : EndIf  ;innerhalb eines Frame
      
      Select moveflag
         Case #goLeft:     x - value
         Case #goRight:    x + value
         Case #goTop:      y - value
         Case #goBottom:   y + value
         Case #goCenter:   y + ((ph-h) / 2)
      EndSelect
      
      ResizeGadget(pbnr, x, y, w, h)  
      
      ;Debug Str(pbnr) + ":" + Str(x) + "," + Str(y + moveY) + "," + Str(w) + "," + Str(h)
      
   EndProcedure   
   
EndModule

UseModule GotoXY

hier die Konstanten als "gotoxy-const.pbi" abspeichern, es empfiehlt sich ein Residentdatei zu erstellen.

Code: Alles auswählen

;16.05.2020 - für GotoXY.pbi 

EnumerationBinary
   #goLeft           ; oben links neben dem parent, auch für moveflag
   #goRight          ; oben rechts neben dem parent, auch für moveflag
   #goLeftBottom     ; unten links neben dem parent
   #goRightBottom    ; unten rechts neben dem parent
   
   #goTopLeft        ; oben links über dem parent
   #goTopRight       ; oben rechts über dem parent
   #goTopCenter      ; mittig über dem parent
   
   #goBottomLeft     ; unten links unter dem parent
   #goBottomRight    ; unten rechts unter dem parent
   #goBottomCenter   ; mittig unter dem parent 
   
   #goGadgetFill     ; füllt ganzes Window, Container etc aus
   #goGadgetWidth    ; gleiche Breite wie Parent
   #goGadgetHeight   ; gleiche Höhe wie Parent
   #goGadgetTop      ; Höhe bis zur oberen Seite Parent (Gadgets) - Abstand
   #goGadgetLeft     ; Breite bis zur linken Seite Parent (Gadgets) - Abstand
   #goGadgetRight    ; Breite bis zur rechten Seite Parent (Gadgets)
   #goGadgetBottom   ; Höhe bis zur unteren Seite Parent (Gadgets)
   
   #goWindowRight    ; Breite bis zum RAND vom Parent (Window)
   #goWindowBottom   ; Höhe bis zum Rand vom Parent (Window)
   
   #goInside         ; für Oder-Kombinationen, wenn Parent = Window, Container, Panel !!! (+ anstatt | geht auch)
   #goCenter         ; nur für moveflag, verschiebt vertikal in Verbindung mit #goLeft + #goRight
   
EndEnumeration

#goTop = #goTopLeft        ; auch für moveflag
#goBottom = #goBottomLeft  ; auch für moveflag

; Muster CMD Datei Windows x64 ab 'echo off' z.B. mit Notepad erstellen (Semikolon entfernen)
;
; als gotoxy.cmd speichern (ANSI File), im gleichen Verzeichnis wie gotoxy-const.pbi !
; Doppelclick auf CMD-Datei und gotoxy.res ins PB Resident-Verzeichnis kopieren.
; ev. Verzeichnisname anpassen 

; @echo off
; @prompt --$g
; 
; echo.
; echo wenn die gotoxy.res im Resident-Verzeichnis vorher nicht geloescht wird, ist das Ergebnis meistens Mist !!!
; 
; set pbvz=C:\Programme\PureBasic572x64
; 
; %pbvz%\Compilers\pbcompiler.exe gotoxy-const.pbi /resident gotoxy.res /IGNORERESIDENT /CREATERESIDENT
; 
; pause
   
   

Re: GotoXY zum plazieren von gadgets

Verfasst: 07.05.2020 15:23
von hjbremer
und hier eine Demo

Code: Alles auswählen

;GoXY Demo 

XIncludeFile "GotoXY.pbi"

Enumeration
   #window
   #list
   #cont
   #text0
   #text1
   #text2
   #frame
   #combo
   #editor
   #button1
   #button2
   #button3
   #button4
   #conttext1
   #conttext2
   #contbutton1
   #contbutton2
   
EndEnumeration

#winwidth = 1200
#winheight = 500

#listwidth = 550 ; zum Testen diese Konstanten ändern
#listheight = 250
#buttonbr = 100
#buttonhh = 25

LoadFont(1,"Arial", 16)

OpenWindow(#window, 0, 0, #winwidth, #winheight, "", #PB_Window_SystemMenu|#PB_Window_ScreenCentered|#PB_Window_Invisible)

ListIconGadget(#list, 0, 0, #listwidth, #listheight, "Euro", 0, #PB_ListIcon_GridLines) 
AddGadgetColumn(#list, 1, "Column", 0)
AddGadgetItem(#list, -1, "23.45" + #LF$ + "Bananenschale")

ComboBoxGadget(#combo, 0, 0, #buttonbr, #buttonhh)   
EditorGadget(#editor,  0, 0, 300, 100) : SetGadgetText(#editor, "EditorGadget")

ButtonGadget(#button1, 0, 0, #buttonbr, #buttonhh, "Button 1")
ButtonGadget(#button2, 0, 0, #buttonbr, #buttonhh, "Button 2")
ButtonGadget(#button3, 0, 0, #buttonbr, #buttonhh, "Button 3")
ButtonGadget(#button4, 0, 0, #buttonbr, #buttonhh, "Button 4")

TextGadget(#text0, 0, 0, 200, #buttonhh, "Dies ist eine Demo", #PB_Text_Center)
TextGadget(#text1, 0, 0, 200, #buttonhh, "Liste ")
TextGadget(#text2, 0, 0, 200, #buttonhh, "Container");, #PB_Text_Border)
FrameGadget(#frame,0, 0, 200, #buttonhh, "Framegadget")

ContainerGadget(#cont, 0, 0, 0, 150, #PB_Container_Flat)

   ButtonGadget(#contbutton1, 0, 0, #buttonbr, #buttonhh, "C.Button 1")
   ButtonGadget(#contbutton2, 0, 0, #buttonbr, #buttonhh, "C.Button 2")
   
CloseGadgetList()

GotoXY(#text0, #window, #goTopCenter|#goInside)    ;Überschrift oben mitte, da Parent ein Window mit #goInside

GotoXY(#text1, #window, #goTop|#goInside, 20, #goBottom, 10)  ;#text1 oben links Abstand 20 und zusätzlich 10 down
GotoXY(#list, #text1, #goBottom, 0)                ;Liste unter #text1
GotoXY(#combo, #list, #goRight)                    ;Combo rechts von Liste
GotoXY(#editor, #combo, #goBottom, 20)             ;#editor unter Combo, Abstand 20
GotoXY(#editor, #list, #goGadgetBottom)            ;#editor Höhe bis Liste unten
GotoXY(#button1, #editor, #goRight, 15, #goCenter) ;Button rechts vom #editor
GotoXY(#button2, #button1, #goBottom)              ;Button 2 unter Button 1

GotoXY(#button4, #window, #goBottomRight|#goInside)   ;#button4 rechts unten

GotoXY(#text2, #list, #goBottom, 10, #goRight, 5)     ;#text2 unter Liste 
GotoXY(#cont, #text2, #goBottom, -8, #goLeft, 5)      ;Container unter Text, Container verdeckt Teil vom Textgadget
GotoXY(#cont, #list, #goGadgetWidth)                  ;Container gleiche Breite wie Liste
GotoXY(#cont, #window, #goWindowBottom, 45)           ;Container Höhe bis Windowrand Abstand 45
GotoXY(#contbutton1, #cont, #goBottomLeft|#goInside)  ;links unten  im Container
GotoXY(#contbutton2, #cont, #goBottomRight|#goInside) ;rechts unten im Container

GotoXY(#frame, #cont, #goRight)                       ; rechts neben Container 
GotoXY(#frame, #cont, #goGadgetHeight)                ; gleiche Höhe wie Container
GotoXY(#frame, #window, #goWindowRight, 40)           ; Breite bis Windowrand Abstand 40

GotoXY(#button3, #frame, #goTopRight|#goInside, 10, #goBottom, 5)   ;FrameGadget wird wie ein Container behandelt


HideWindow(#window, 0)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow


Re: GotoXY zum plazieren von gadgets

Verfasst: 17.05.2020 14:51
von hjbremer
Hier ein kleines UpDate, Code oben geändert !

Es hat sich gezeigt das der Parameter Move geändert werden mußte, da ich doch häufiger X+Y Position optimieren mußte

Re: GotoXY zum plazieren von gadgets

Verfasst: 17.05.2020 21:29
von GPI
Der Code hier:

Code: Alles auswählen

   If Defined(goLeft, #PB_Constant) = 0
      XIncludeFile "gotoxy-const.pbi"
   EndIf
macht so keinen Sinn.

XINCLUDEFILE wird quasi von Compiler immer durchgeführt. Keine Ahnung, ob der Compiler so clever ist, den "if"-Teil rauszukürzen, wenn die bedinung immer nicht erfüllt ist. Zum anderen weis ich nicht, ob Konstanten sich um if/endif überhaupt scheren, da das eigentlich "Compiler-Direktiven" sind.

Sinnvoller wäre

Code: Alles auswählen

   compilerIf Defined(goLeft, #PB_Constant) = 0
      XIncludeFile "gotoxy-const.pbi"
   compilerEndIf
Dann entfernt der Compiler den Code gleich.

Re: GotoXY zum plazieren von gadgets

Verfasst: 17.05.2020 22:17
von hjbremer
wenn es diese Konstante nicht gibt wird die Include geladen, das macht der Code

dann wenn es z.B. keine Resourcendatei gibt in der die Konstanten enthalten sind

Re: GotoXY zum plazieren von gadgets

Verfasst: 18.05.2020 04:34
von GPI
Das meinte ich ja, bei deinen Code wird sie immer geladen. XINCLUDEFILE ist kein Befehl, es ist eine Compileranweisung. Die wird unabhängig von anderen Befehlen immer ausgeführt.

Re: GotoXY zum plazieren von gadgets

Verfasst: 18.05.2020 07:08
von hjbremer
Ich muß dir recht geben, das stimmt.

Habe mir mal eine alte Version angesehenund da steht noch innerhalb dieser If Anweisung als Info

Code: Alles auswählen

Debug "Info: ResidentDatei fehlt/fehlerhaft !"
Dies fehlt in dieser Version. Ich lasse es aber so, denn wenn die Residentdatei fehlt
müssen die Konstanten ja trotzdem geladen werden.
Und es ist dann egal ob XInclude innerhalb oder ausserhalb der IF Anweisung steht

Re: GotoXY zum plazieren von gadgets

Verfasst: 20.05.2020 18:50
von GPI
du könntest das ganze auch in COMPILERIF und COMPILERENDIF einschließen, dann macht es einen Unterschied.

Anderseits, wenns die Konstanten schon gibt und die identisch sind, passiert nichts :) - Struckturen sind kritisch.