mein farbiges ButtonGadget

Hier könnt Ihr gute, von Euch geschriebene Codes posten. Sie müssen auf jeden Fall funktionieren und sollten möglichst effizient, elegant und beispielhaft oder einfach nur cool sein.
Benutzeravatar
hjbremer
Beiträge: 822
Registriert: 27.02.2006 22:30
Computerausstattung: von gestern
Wohnort: Neumünster

mein farbiges ButtonGadget

Beitrag von hjbremer »

mein uraltes farbiges ButtonGadget habe ich nun endlich mal neu geschrieben, auf Basis des Canvasgadgets

es ist simpel und klein (200 Zeilen) und läßt sich leicht erweitern, finde ich.

die flags #PB_Button_MultiLine + _Default gibts nicht, Multiline wird aber im Text mit #LF$ oder | gemacht

das Toggleflag wird unterstützt mit minimaler Unterstützung, aber PB ist auch nicht besser.

Der wichtigste Unterschied ist das muß von Eventtype() in der Eventschleife.

Das Modul abspeichern z.B. als IButton1.pbi, anschließend folgt ein kleines Demoprog

Code: Alles auswählen

DeclareModule IButtonGadget
   ;HJBremer Windows 10 - ab PB 5.41 x86 + 5.70 x64 - 25.05.2019 V.1.15 
   
   Declare.i IButtonGadget(pbnr, x, y, br, hh, text$, flag=0)  ;flags: #PB_Button_Toggle, _Left, _Right
   
   ;die flags #PB_Button_MultiLine + _Default gibts nicht, Multiline wird aber im Text mit #LF$ oder | gemacht
   
   Declare.i IButtonSetColor(pbnr, oben, unten, text=#PB_Ignore, flag=1)  ;flag=2 für MouseHover
   
   Declare.i IButtonCopyColor(pbnr, ziel)                ;kopiert Farben 
   
   Declare.s IButtonGetText(pbnr)                        ;holt Text
   Declare.i IButtonSetText(pbnr, text$, flag=1)         ;ändert den Text mit Draw
   Declare.i IButtonSetText2(pbnr, text$, flag=0)        ;ändert Zusatztext, flag=1 dann Draw
   Declare.i IButtonSetFont(pbnr, fontid)                ;ändert den Font      
   Declare.i IButtonState(pbnr)                          ;ToggleState 0 oder 1
   
   Declare.i IButtonDraw(pbnr, colorOben, colorUnten, colortext, colorborder)  ; in der Regel nur Intern
   
   Structure IButton    ;in der Regel nur Intern
      br.i              ;Breite Canvas, für IButtonDraw()
      hh.i              ;Höhe
      toggle.i          ;erhält den Wert 4096 bei Zuweisung und wenn Button gedrückt plus 1 also 4097
      cBorderAussen.i   ;Rahmen immer schwarz (in IButtonGadget ändern)
      cBorderNormal.i   ;Rahmen innen
      cBorderHover.i    ;Rahmen innen
      cTextNormal.i     ;Text
      cTextHover.i
      gcObenNormal.i    ;GradientFarben
      gcUntenNormal.i      
      gcObenHover.i
      gcUntenHover.i      
      fontid.i
      left.i            ;Text nach links - ist flag und abstand zugleich
      right.i           ;Text nach rechts - ist flag und abstand
      text.s            ;Buttontext - mehrere Zeilen trennen mit #LF$ oder |
      text2.s           ;Zusatztext
   EndStructure  
   
   #windowbkcolor = $F0F0F0   ;Window/Container-Farbe nur zur Info (Windows 10), auch GetSysColor_(#COLOR_3DFACE)
   
EndDeclareModule

Module IButtonGadget   
   EnableExplicit
   
   Procedure.i IButtonDraw(pbnr, colorOben, colorUnten, colortext, colorborder)
      
      Protected x, y, anz, j, thh, text$, t$, *ib.IButton = GetGadgetData(pbnr)       
      
      With *ib
         
         StartDrawing(CanvasOutput(pbnr))            
            ;Border
            If \toggle & 1
               Box(0, 0, \br, \hh, #Red)              ; Box ev. durch DrawEdge_() ersetzen
               Box(1, 1, \br-2, \hh-2, #Yellow)
            Else
               Box(0, 0, \br, \hh, \cBorderAussen)    ; Fix, Vorgabe ist #Black 
               Box(1, 1, \br-2, \hh-2, colorBorder)   ; Rahmen innen, cBorderNormal/cBorderHover
            EndIf
            ;Buttoncolor
            DrawingMode(#PB_2DDrawing_Gradient)
            BackColor(colorOben)          ;colorOben
            FrontColor(colorUnten)        ;colorUnten
            LinearGradient(0, 5, 0, \hh)
            Box(2, 2, \br-4, \hh-4)   
            ;Text + Font
            DrawingMode(#PB_2DDrawing_Transparent)
            If \fontid : DrawingFont(\fontid) : EndIf            
            text$ = \text + \text2
            ReplaceString(text$, "|", #LF$, #PB_String_InPlace)
            anz = CountString(text$, #LF$)
            thh = TextHeight(text$)
            y = (\hh - (thh * (anz+1))) / 2  ;1 - mehrzeilig, entspricht y = (\hh - thh) / 2
            For j = 0 To anz
               t$ = StringField(text$, j+1, #LF$)               
               x = (\br - TextWidth(t$)) / 2       ;CenterText
               If \left : x = \left : EndIf
               If \right : x = \br - TextWidth(t$) - \right : EndIf               
               DrawText(x, y, t$, colortext) : y + thh
            Next
         StopDrawing()
      EndWith      
   EndProcedure
   
   Procedure.s IButtonGetText(pbnr)
      Protected *ib.IButton = GetGadgetData(pbnr) 
      ProcedureReturn *ib\text
   EndProcedure
   
   Procedure.i IButtonSetFont(pbnr, fontid)      
      Protected *ib.IButton = GetGadgetData(pbnr)      
      *ib\fontid = fontid
      IButtonDraw(pbnr, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal)      
   EndProcedure
   
   Procedure.i IButtonSetText(pbnr, text$, flag=1)      
      Protected *ib.IButton = GetGadgetData(pbnr)      
      *ib\text = text$      
      If flag: IButtonDraw(pbnr, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal): EndIf      
   EndProcedure
   
   Procedure.i IButtonSetText2(pbnr, text$, flag=0)      
      Protected *ib.IButton = GetGadgetData(pbnr)      
      *ib\text2 = text$ 
      If flag: IButtonDraw(pbnr, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal): EndIf
   EndProcedure
   
   Procedure.i IButtonSetColor(pbnr, oben, unten, text=#PB_Ignore, flag=1)      
      Protected *ib.IButton = GetGadgetData(pbnr) 
      
      Select flag          
         Case #COLOR_GRADIENTINACTIVECAPTION, #PB_Gadget_FrontColor, 1  ; 3 Möglichkeiten zum aussuchen
            If oben <> #PB_Ignore : *ib\gcObenNormal = oben : EndIf
            If unten <> #PB_Ignore: *ib\gcUntenNormal = unten : EndIf 
            If text <> #PB_Ignore : *ib\cTextNormal = text : EndIf
            
         Case #COLOR_GRADIENTACTIVECAPTION, #PB_Gadget_BackColor, 2
            If oben <> #PB_Ignore : *ib\gcObenHover = oben : EndIf
            If unten <> #PB_Ignore: *ib\gcUntenHover = unten : EndIf 
            If text <> #PB_Ignore : *ib\cTextHover = text : EndIf
            
         Case #COLOR_BTNFACE, #PB_Gadget_LineColor, 3
            If oben <> #PB_Ignore : *ib\cBorderNormal = oben : EndIf
            If unten <> #PB_Ignore: *ib\cBorderHover = unten : EndIf 
            If text <> #PB_Ignore : *ib\cBorderAussen = text : EndIf
            
      EndSelect
      
      IButtonDraw(pbnr, *ib\gcObenHover, *ib\gcUntenHover, *ib\cTextHover, *ib\cBorderHover)
      IButtonDraw(pbnr, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal)      
   EndProcedure   
   
   Procedure.i IButtonCopyColor(pbnr, ziel)      
      Protected *ib.IButton = GetGadgetData(pbnr)       
      IButtonSetColor(ziel, *ib\gcObenHover, *ib\gcUntenHover, *ib\cTextHover, #PB_Gadget_BackColor)
      IButtonSetColor(ziel, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, #PB_Gadget_FrontColor)
      IButtonSetColor(ziel, *ib\cBorderNormal, *ib\cBorderHover, *ib\cBorderAussen, #PB_Gadget_LineColor)      
   EndProcedure 
   
   Procedure.i IButtonState(pbnr)      
      Protected *ib.IButton = GetGadgetData(pbnr)      
      ProcedureReturn *ib\toggle & 1
   EndProcedure
   
   Procedure.i IButtonEvent()      
      Protected pbnr = EventGadget(), *ib.IButton = GetGadgetData(pbnr)      
      Select EventType()
         Case #PB_EventType_MouseEnter
            IButtonDraw(pbnr, *ib\gcObenHover, *ib\gcUntenHover, *ib\cTextHover, *ib\cBorderHover)            
            
         Case #PB_EventType_MouseLeave
            IButtonDraw(pbnr, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal)  
            
         Case #PB_EventType_LeftClick
            If *ib\toggle & 4096 : *ib\toggle ! 1 : EndIf  ;ergibt 4096 bzw 4097
            IButtonDraw(pbnr, *ib\gcObenHover, *ib\gcUntenHover, *ib\cTextHover, *ib\cBorderHover)
      EndSelect      
   EndProcedure   
   
   Procedure.i IButtonGadget(pbnr, x, y, br, hh, text$, flag=0)      
      Protected rg, *ib.IButton = AllocateMemory(SizeOf(IButton))
      
      With *ib
         \br = br
         \hh = hh
         \text = text$
         \cBorderAussen = #Black
         \cBorderNormal = $F0F0F0   ;#white oder #gray
         \cBorderHover = $FFFF00    ;#white oder #yellow
         \cTextNormal = #Black
         \cTextHover = #Blue
         \gcObenHover = #White
         \gcUntenHover = #Gray
         \gcObenNormal = #White
         \gcUntenNormal = $999999 
         
         If flag & #PB_Button_Left = #PB_Button_Left : \left = 5: EndIf             ;256
         If flag & #PB_Button_Right = #PB_Button_Right : \right = 5 : EndIf         ;512         
         If flag & #PB_Button_Toggle = #PB_Button_Toggle : \toggle = 4096 : EndIf   ;ist 4099       
         
         rg = CanvasGadget(pbnr, x, y, br, hh)
         If pbnr = #PB_Any : pbnr = rg : EndIf
         
         SetGadgetData(pbnr, *ib)         
         BindGadgetEvent(pbnr, @IButtonEvent(), #PB_EventType_MouseEnter)
         BindGadgetEvent(pbnr, @IButtonEvent(), #PB_EventType_MouseLeave)         
         BindGadgetEvent(pbnr, @IButtonEvent(), #PB_EventType_LeftClick)         
         IButtonDraw(pbnr, \gcObenNormal, \gcUntenNormal, \cTextNormal, \cBorderNormal)          
      EndWith 
      
      ProcedureReturn rg
   EndProcedure
   
EndModule

UseModule IButtonGadget
Demo

Code: Alles auswählen

XIncludeFile "IButton1.pbi"

EnableExplicit

Enumeration
   #Window : #font   
   #IButton1 : #IButton2 : #IButton3 : #IButton4
   #IButton5 : #IButton6 : #IButton7 : #IButton8
   #IButton9 : #IButton10 : 
EndEnumeration

LoadFont(#font, "Consolas", 9)

Define event, pbnr10, text$, nr, jahr = 2019

OpenWindow(#Window, 0, 0, 500, 300, "Canvas Buttons",#PB_Window_ScreenCentered | #PB_Window_SystemMenu)

IButtonGadget(#IButton1, 10, 10, 120, 28, "Button 1")

IButtonGadget(#IButton2, 10, 50, 120, 34, "Button 2", #PB_Button_Toggle|#PB_Button_Left)

IButtonSetColor(#IButton2, $86E0FF, $1379C4, #Blue, #PB_Gadget_FrontColor)
IButtonSetColor(#IButton2, #White, $D24321, #Red, #PB_Gadget_BackColor)

IButtonGadget(#IButton3, 10, 99, 120, 44, "Button 3|mit 2 Zeilen", #PB_Button_Right)

IButtonGadget(#IButton4, 10, 170, 120, 26, "Jahr ändern ->")
IButtonSetFont(#IButton4, FontID(#font))
IButtonSetColor(#IButton4, #Yellow, #Green, #Magenta)

IButtonGadget(#IButton5, 150, 10, 120, 28, "Button 5 State ?")
IButtonGadget(#IButton6, 150, 50, 120, 34, "Toggle Button", #PB_Button_Toggle) 
IButtonGadget(#IButton7, 150, 99, 150, 64, "") 
IButtonSetText(#IButton7, "Button 7|drück mich")

IButtonGadget(#IButton8, 150, 170, 150, 44, "im Jahre ")
IButtonSetText2(#IButton8, Str(jahr), 1)

IButtonSetColor(#IButton5, #PB_Ignore, #PB_Ignore, #Blue,1)
IButtonSetColor(#IButton5, #PB_Ignore, #PB_Ignore, #Red, 2)
IButtonSetColor(#IButton5, #PB_Ignore, #Red, #PB_Ignore, 3)

IButtonSetColor(#IButton7, $F0F0F0, $F0F0F0, #Blue,1)
IButtonSetColor(#IButton7, $F0F0F0, $F0F0F0, #Red,  2)
IButtonSetColor(#IButton7, $F0F0F0, $F0F0F0, $F0F0F0,3)

IButtonGadget(#IButton9, 10, 230, 320, 34, "Eventype Abfrage vergessen: im Jahre ")

pbnr10 = IButtonGadget(#PB_Any, 400, 260, 90, 24, "Ende")
IButtonCopyColor(#IButton4, pbnr10)


Repeat
   Event = WaitWindowEvent()
   
   Select Event         
      Case #PB_Event_Gadget
         
         Select EventGadget()               
            Case #IButton1 To #IButton3
               If EventType() = #PB_EventType_LeftClick     ;Abfrage nie vergessen
                  Debug "Button Leftclick " + EventGadget()
               EndIf
               
            Case #IButton4
               If EventType() = #PB_EventType_LeftClick
                  jahr + 1
                  IButtonSetText2(#IButton8, Str(jahr), 1) ;1=mit Redraw
               EndIf
               
            Case #IButton5
               If EventType() = #PB_EventType_LeftClick
                  Debug "Toggle 2 " + IButtonState(#IButton2)
                  Debug "Toggle 6 " + IButtonState(#IButton6)
               EndIf

            Case #IButton6: 
               If EventType() = #PB_EventType_LeftClick                                    
                  
               EndIf
               
            Case #IButton7
               If EventType() = #PB_EventType_LeftButtonDown
                  nr + 1
                  text$ = IButtonGetText(#IButton7)
                  text$ = StringField(text$, 1, #LF$) + #LF$ + "weiter so"
                  IButtonSetText(#IButton7, text$, 0) ;0=kein Redraw                  
                  IButtonSetText2(#IButton7, #LF$ + Str(nr) + " x gedrückt")
               EndIf
               
            Case #IButton8
               If EventType() = #PB_EventType_LeftClick
                  jahr = 1355
                  IButtonSetText(#IButton8, "wieder im Jahre"+#LF$)
                  IButtonSetText2(#IButton8, Str(jahr), 1)     ;1=mit Redraw
               EndIf
               
            Case #IButton9
                  jahr + 1
                  IButtonSetText2(#IButton9, Str(jahr))
 
            Case pbnr10
               If EventType() = #PB_EventType_LeftClick: Break: EndIf               
            
         EndSelect
   EndSelect
   
Until Event = #PB_Event_CloseWindow
Purebasic 5.70 x86 5.72 X 64 - Windows 10

Der Computer hat dem menschlichen Gehirn gegenüber nur einen Vorteil: Er wird benutzt
grüße hjbremer
Benutzeravatar
hjbremer
Beiträge: 822
Registriert: 27.02.2006 22:30
Computerausstattung: von gestern
Wohnort: Neumünster

Re: mein farbiges ButtonGadget + Farbentool

Beitrag von hjbremer »

Hier noch ein Tool um die Farben zu bestimmen bzw. anzusehen für IButtonGadget

Hat zwar 500 Zeilen ist aber nützlich finde ich

PS: benötigt obiges Modul

Code: Alles auswählen

;- IButton-Tool1 - Windows 10 PB ab 5.41 LTS x86 x64

;Hinweis: Beim CanvasGadget muß es immer eine EventType() Abfrage geben !

XIncludeFile "IButton1.pbi" 

#version = "IButton-Tool1 by HJBremer V.1.04 - 05.2019"

EnableExplicit

Enumeration
   #Window   
   #ContainerF1 : #ContainerF2 : #ContainerF3 : #ContainerF4  : #ContainerF5   
   #cNormal : #cHover : #cInfo : #colorboxen : #colorboxinfo : #gradient    
   #optNormal : #optHover : #optOben : #optUnten : #optText : #optFBox : #cInfotext   
   #IButton1  : #IButton2 : #IButton3 : #IButton12 : #IButton13 : #IButtonEnd    
   #fontDemo: #fontButton : #fontInfo : #textF5Info : #textClipinfo : #dummy   
EndEnumeration

Macro IButtonDrawInfo(color)
   IButtonSetText(#cInfo, MakeHex(color))
   IButtonDraw(#cInfo,  color, color, color ! #White, color)
EndMacro

Procedure.s MakeHex(color)
   ProcedureReturn "$" + Right("00000" + Hex(color), 6)
EndProcedure

Procedure.i CreateCanvas(pbnr, x, y, br, hh, text$)   
   ;erstellt CanvasGadget für die Gradient-Color eines Buttons
   
   Protected *ib.IButton = AllocateMemory(SizeOf(IButton))
   
   *ib\cBorderAussen = #Black
   *ib\cBorderNormal = #White
   *ib\br = br
   *ib\hh = hh
   *ib\text = text$   
   *ib\fontid = FontID(#fontDemo)
   
   CanvasGadget(pbnr, x, y, br, hh) : SetGadgetData(pbnr, *ib) 
   
EndProcedure

Procedure.i SetColorInfo(pbnr)   
   ;füllt Info-CanvasGadget 
   ;pbnr = für aktiven DemoButton (Button1 Button2 Button3)
   
   Protected *ib.IButton = GetGadgetData(pbnr)    
   
   Protected t1$, t2$, t3$, t4$, thh, x=4, y=1
   
   Protected br = GadgetWidth(#cInfotext)
   Protected hh = GadgetHeight(#cInfotext)
   
   If GetGadgetState(#optNormal)
      t1$ = "Normal"
      t2$ = "Oben.: " + MakeHex(*ib\gcObenNormal)
      t3$ = "Unten: " + MakeHex(*ib\gcUntenNormal)
      t4$ = "Text.: " + MakeHex(*ib\cTextNormal)      
   Else
      t1$ = "Hover"
      t2$ = "Oben.: " + MakeHex(*ib\gcObenHover)
      t3$ = "Unten: " + MakeHex(*ib\gcUntenHover)
      t4$ = "Text.: " + MakeHex(*ib\cTextHover)      
   EndIf
   
   StartDrawing(CanvasOutput(#cInfotext))
      Box(0, 0, br, hh, $DADADA)
      Box(1, 1, br-2, hh-2, #windowbkcolor)
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(FontID(#fontButton))
      thh = TextHeight(t1$)
      DrawText(x, y, *ib\text, #Red) : y + thh + 2
      DrawText(x, y, t1$, #Black)    : y + thh + 2
      DrawText(x, y, t2$, #Blue)     : y + thh + 2
      DrawText(x, y, t3$, #Blue)     : y + thh + 2
      DrawText(x, y, t4$, #Blue)     : y + thh + 2      
   StopDrawing()
   
EndProcedure

Procedure.i ColorIni(pbnr, flag=0)   
   ;merkt sich die Farben der 3 DemoButtons
   
   Protected ok = OpenPreferences("IButton.ini")
   If ok = 0: CreatePreferences("IButton.ini"): EndIf
   
   Protected *ib.IButton = GetGadgetData(pbnr), name$ = *ib\text
   
   With *ib
      If flag   ;schreiben         
         PreferenceGroup(name$)
         WritePreferenceInteger("TextNormal",  \cTextNormal)
         WritePreferenceInteger("BackNormal",  \gcObenNormal)
         WritePreferenceInteger("FrontNormal", \gcUntenNormal)
         WritePreferenceInteger("TextHover",   \cTextHover)
         WritePreferenceInteger("BackHover",   \gcObenHover)
         WritePreferenceInteger("FrontHover",  \gcUntenHover)         
      Else      ;lesen         
         PreferenceGroup(name$)
         \cTextNormal   = ReadPreferenceInteger("TextNormal",  #Black)
         \gcObenNormal  = ReadPreferenceInteger("BackNormal",  #White)
         \gcUntenNormal = ReadPreferenceInteger("FrontNormal", #Gray)
         \cTextHover    = ReadPreferenceInteger("TextHover",   #White)
         \gcObenHover   = ReadPreferenceInteger("BackHover",   $333333)
         \gcUntenHover  = ReadPreferenceInteger("FrontHover",  $999999)         
         IButtonSetColor(pbnr, \gcObenNormal, \gcUntenNormal, \cTextNormal, #PB_Gadget_FrontColor)
         IButtonSetColor(pbnr, \gcObenHover,  \gcUntenHover,  \cTextHover, #PB_Gadget_BackColor)         
      EndIf      
   EndWith
   
   ClosePreferences() 
   
EndProcedure

Procedure.i ColorBox(pbnr, x, y, br, hh)   
   ;erstellt Farbtafel rechts mit 16 Farben aus DataSection
   
   Protected j, color, chh
   
   CanvasGadget(pbnr, x, y, br, hh)
   SetGadgetAttribute(pbnr, #PB_Canvas_Cursor , #PB_Cursor_Hand)
   
   StartDrawing(CanvasOutput(pbnr))
      Box(0, 0, br, hh, pbnr)      
      x = 0: y = 0: chh = 24      
      For j = 0 To 15
         Read.i color         
         Box(x, y, br, chh, color) : y + chh + 1
      Next      
   StopDrawing()
   
EndProcedure

Procedure.i GradientBox(pbnr, mitte)   
   ;füllt Gradienttafel ganz rechts 
   
   Protected t$, thh, x, y, br = GadgetWidth(pbnr), hh = GadgetHeight(pbnr)
   
   StartDrawing(CanvasOutput(#gradient))
      DrawingMode(#PB_2DDrawing_Gradient)
      BackColor(#White) 
      GradientColor(0.5, mitte)
      FrontColor(#Black)
      LinearGradient(0, 0, 0, hh)
      Box(0, 0, br, hh) 
      DrawingMode(#PB_2DDrawing_Transparent)
      DrawingFont(FontID(#fontInfo))
      t$ = "Farbe oben" : x = (br - TextWidth(t$)) / 2 : y = 5 : thh = TextHeight(t$)
      DrawText(x, y, t$ , #Black) : y + thh
      t$ = "Leftclick" : x = (br - TextWidth(t$)) / 2
      DrawText(x, y, t$,#Black)
      t$ = "Rightclick" : x = (br - TextWidth(t$)) / 2 : y = hh - 5 - thh
      DrawText(x, y, t$, #White)
      t$ = "Farbe unten" : x = (br - TextWidth(t$)) / 2 : y - thh
      DrawText(x, y, t$, #White)      
      DrawRotatedText(br-5, hh/4, #version, 270, mitte ! #White)
   StopDrawing()
   
EndProcedure

Procedure GetColorUnderMouse() 
   Protected cursorPos.POINT
   Protected dc = GetDC_(0), farbe
   
   GetCursorPos_(cursorPos)    
   farbe = GetPixel_(dc, cursorPos\x, cursorPos\y) 
   ReleaseDC_(0, dc) 
   
   ProcedureReturn farbe
EndProcedure 

Procedure.i ColorToClipboard(pbnr, buttonaktiv)
   
   Protected t1$, t2$, *ib.IButton = GetGadgetData(buttonaktiv) 
   
   With *ib
      
      t1$ = "IButtonSetColor(nr, "
      t1$ + MakeHex(\gcObenNormal) + ", "
      t1$ + MakeHex(\gcUntenNormal) + ", "
      t1$ + MakeHex(\cTextNormal) + ", 1)"
      
      t2$ + "IButtonSetColor(nr, "
      t2$ + MakeHex(\gcObenHover) + ", "
      t2$ + MakeHex(\gcUntenHover) + ", "
      t2$ + MakeHex(\cTextHover) + ", 2)"
      
      Select pbnr
         Case #cNormal : SetClipboardText(t1$)
         Case #cHover  : SetClipboardText(t2$)
         Case #cInfo
            StartDrawing(CanvasOutput(#cInfo))
               SetClipboardText(MakeHex(Point(3,3)))
            StopDrawing() 
         Case #cInfotext : SetClipboardText(t1$ + #LF$ + t2$)
      EndSelect
      
   EndWith
   
   SetGadgetText(#textClipinfo, "im Clipboard" + #LF$ + GetClipboardText())
   
EndProcedure

;- Define

LoadFont(#fontInfo, "Consolas", 9)
LoadFont(#fontButton, "Consolas", 11)
LoadFont(#fontDemo,   "Consolas", 14)
SetGadgetFont(#PB_Default, FontID(#fontButton))

Define x, y, br, hh, j, t$

Define color, colorinfo
Define event, welchesGadget, flags

Define buttonaktiv, *ib.IButton

#con1br = 320 : #con2br = 150 : #conhh = 450 : 
#winbr = #con1br + #con2br + #con2br : #winhh = 80 + #conhh : #con4hh = #winhh-#conhh

;- Window

flags = #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget | #PB_Window_Invisible

OpenWindow(#Window, 0, 0, #winbr, #winhh, "Canvas Button Tool", flags)

AddKeyboardShortcut(#Window, #PB_Shortcut_F5, #PB_Shortcut_F5)

;{ ContainerF1
;--- ContainerF1
ContainerGadget(#ContainerF1, 0, 0, #con1br, #conhh, #PB_Container_Single)
   
   x = 15: y = 15: br = 140 : hh = 60
   CreateCanvas(#cNormal, x, y, br, hh, "Normal") : y + hh + 20
   CreateCanvas(#cHover,  x, y, br, hh, "Hover")  : y + hh + 20
   CreateCanvas(#cInfo,  x, y, br, hh, "")        : y + hh + 20
   CreateCanvas(#cInfotext, x, y, br, 100, "")    : y + 100 + 10
   
   SetGadgetAttribute(#cNormal, #PB_Canvas_Cursor , #PB_Cursor_Hand)
   SetGadgetAttribute(#cHover, #PB_Canvas_Cursor , #PB_Cursor_Hand)
   SetGadgetAttribute(#cInfo, #PB_Canvas_Cursor , #PB_Cursor_Hand)
   SetGadgetAttribute(#cInfotext, #PB_Canvas_Cursor , #PB_Cursor_Hand)   
   
   TextGadget(#textClipinfo, x, y, 280, 75, "")    ;, #PB_Text_Border)
   SetGadgetFont(#textClipinfo, FontID(#fontInfo))
   SetGadgetColor(#textClipinfo, #PB_Gadget_FrontColor, #Gray)
   
   x + br + 25: y = 20
   OptionGadget(#optNormal, x, y, br, 20, "Normal")      : y + hh + 20
   OptionGadget(#optHover,  x, y, br, 20, "MouseHover")  : y + hh + 20
   
   TextGadget(#PB_Any,0,0,0,0,"") ;trennt Optiongadgets
   
   y = GadgetY(#cInfo)
   OptionGadget(#optOben,  x, y, br, 20, "Farbe Oben")  : y + 40
   OptionGadget(#optUnten, x, y, br, 20, "Farbe Unten") : y + 40
   OptionGadget(#optText,  x, y, br, 20, "Farbe Text")  : y + 80
   OptionGadget(#optFBox,  x, y, br, 20, "Farbe Box ->")
   
CloseGadgetList()
;}   
;{ ContainerF2
;--- ContainerF2
ContainerGadget(#ContainerF2, #con1br, 0, #con2br, #conhh, #PB_Container_Single)
   br = #con2br - 20
   TextGadget(#colorboxinfo, 10, 10, br, 20, GetGadgetText(#optOben), #PB_Text_Center)
   ColorBox(#colorboxen, 10, 40, br, 400)
CloseGadgetList()
;}   
;{ ContainerF3
;--- ContainerF3
ContainerGadget(#ContainerF3, #con1br + #con2br, 0, #con2br, #conhh, #PB_Container_Single)
   CanvasGadget(#gradient, 10, 10, #con2br - 20, #conhh - 20)
   SetGadgetAttribute(#gradient, #PB_Canvas_Cursor , #PB_Cursor_Hand)      
   GradientBox(#gradient, $8FC08F)  ;#Gray   
CloseGadgetList()
;}
;{ ContainerF4
;--- ContainerF4
ContainerGadget(#ContainerF4, 0, #conhh, #con1br + #con2br, #con4hh, #PB_Container_Single)
   x = 10: y = 20
   IButtonGadget(#IButton1, x, y, 110, 50, "Button 1") : x + 125 : y + 10  
   IButtonGadget(#IButton2, x, y, 105, 40, "Button 2") : x + 120 : y + 8
   IButtonGadget(#IButton3, x, y, 100, 32, "Button 3") : x + 120 : y + 2  
   IButtonGadget(#IButton12, x, y, 40, 30, "1"+Chr($2B82)+"2")  : x + 45  
   IButtonGadget(#IButton13, x, y, 40, 30, "1"+Chr($2B82)+"3")
   
   IButtonSetFont(#IButton1, FontID(#fontDemo))      
   
   ColorIni(#IButton1) : ColorIni(#IButton2) : ColorIni(#IButton3)
   
   x = GadgetX(#IButton3): y = 10
   TextGadget(#textF5Info, x, y, 200, 22, "F5 = Get Color under Mouse")
   SetGadgetFont(#textF5Info, FontID(#fontInfo))
   SetGadgetColor(#textF5Info, #PB_Gadget_FrontColor, #Gray)
   
CloseGadgetList()
;}   
;{ ContainerF5
;--- ContainerF5
ContainerGadget(#ContainerF5, #con1br + #con2br, #conhh, #con2br, #con4hh, #PB_Container_Single)
   
   x = 65: y = #con4hh - 40 + 6
   IButtonGadget(#IButtonEnd, x, y, 77, 24, "Ende")
   IButtonSetColor(#IButtonEnd, $FFFFB6, $FFFFB6, #Red, #PB_Gadget_BackColor)
   
CloseGadgetList()
;}
;{ ToolTips
;--- ToolTips

   GadgetToolTip(#cNormal,    "Leftclick kopiert Normal Farben ins Clipboard")
   GadgetToolTip(#cHover,     "Leftclick kopiert Hover Farben ins Clipboard")
   GadgetToolTip(#cInfo,      "Leftclick kopiert Farbe ins Clipboard")   
   GadgetToolTip(#cInfotext,  "Leftclick kopiert Normal + Hover Farben ins Clipboard") 
   
   GadgetToolTip(#optNormal,  "Farben festlegen Normal-Zustand")      
   GadgetToolTip(#optHover,   "Farben festlegen, wenn MausHover")      

   GadgetToolTip(#optOben,    "Gradient Farbe festlegen im Button oben")      
   GadgetToolTip(#optUnten,   "Gradient Farbe festlegen im Button unten")  
   GadgetToolTip(#optText,    "TextFarbe festlegen") 
   GadgetToolTip(#optFBox,    "FarbeBox ändert Farbe von Farbfeld rechts aussen") 
   
   GadgetToolTip(#colorboxen, "ändert Farbe abhängig von Option") 
   GadgetToolTip(#gradient,   "ändert Farbe und Option mit Leftclick oder Rightclick") 
   
   GadgetToolTip(#IButton1,   "Farben dieses Buttons oben anzeigen und ändern") 
   GadgetToolTip(#IButton2,   "Farben dieses Buttons oben anzeigen und ändern") 
   GadgetToolTip(#IButton3,   "Farben dieses Buttons oben anzeigen und ändern") 
   
   GadgetToolTip(#IButton12,  "Farben von Button 1 nach 2 mit Leftclick kopieren und mit Rightclick zurück") 
   GadgetToolTip(#IButton13,  "Farben von Button 1 nach 3 mit Leftclick kopieren und mit Rightclick zurück") 
   
   GadgetToolTip(#IButtonEnd, "Progende + Farben in IButton.ini abspeichern") 
   
;}

;- Start
  
SetGadgetState(#optOben, 1)
SetGadgetState(#optNormal, 1)

buttonaktiv = #IButton1 : *ib = GetGadgetData(buttonaktiv)
SetColorInfo(buttonaktiv)                     
IButtonDraw(#cHover, *ib\gcObenHover, *ib\gcUntenHover, *ib\cTextHover, *ib\cBorderHover) 
IButtonDraw(#cNormal, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, *ib\cBorderNormal)
IButtonDrawInfo(*ib\gcObenNormal)

HideWindow(#Window, 0)

;- Repeat

With *ib   
   Repeat
      Event = WaitWindowEvent()
      welchesGadget = EventGadget()
      
      Select Event
            
         Case #PB_Event_Menu
            If EventMenu() = #PB_Shortcut_F5
               color = GetColorUnderMouse()
               SetClipboardText(Makehex(color))
               SetGadgetText(#textClipinfo, "im Clipboard" + #LF$ + Makehex(color))
               PostEvent(#PB_Event_Gadget, #Window, #dummy)  
            EndIf
            
         Case #PB_Event_Gadget            
            Select welchesGadget
                  
               Case #gradient 
                  Select EventType() 
                     Case #PB_EventType_LeftClick, #PB_EventType_RightClick
                        y = GetGadgetAttribute(#gradient, #PB_Canvas_MouseY)
                        StartDrawing(CanvasOutput(#gradient))  
                           If y > 0 And y < GadgetHeight(#gradient) 
                              color = Point(1, y)
                           EndIf
                        StopDrawing()  
                        If GetGadgetState(#optText) = 0
                           If EventType() = #PB_EventType_LeftClick
                              SetGadgetState(#optOben, 1)
                              SetGadgetText(#colorboxinfo, GetGadgetText(#optOben))
                           Else
                              SetGadgetState(#optUnten, 1) 
                              SetGadgetText(#colorboxinfo, GetGadgetText(#optUnten))
                           EndIf
                        EndIf
                        IButtonDrawInfo(color)
                        PostEvent(#PB_Event_Gadget, #Window, #dummy) 
                  EndSelect
                  
               Case #colorboxen                  
                  Select EventType() 
                     Case #PB_EventType_LeftClick, #PB_EventType_RightClick
                        y = GetGadgetAttribute(#colorboxen, #PB_Canvas_MouseY)
                        StartDrawing(CanvasOutput(#colorboxen))  
                           If y > 0 And y < GadgetHeight(#colorboxen) 
                              color = Point(1, y)
                           EndIf
                        StopDrawing()                        
                        If GetGadgetState(#optFBox)
                           GradientBox(#gradient, color)
                           IButtonDrawInfo(color)
                        Else
                           If EventType() = #PB_EventType_LeftClick
                              PostEvent(#PB_Event_Gadget, #Window, #dummy)  
                           EndIf
                        EndIf
                  EndSelect                  
                  
               ;---  Buttons                  
               Case #IButtonEnd: If EventType() = #PB_EventType_LeftClick: Break : EndIf
                  
               Case #dummy ;Farbe zuweisen + anzeigen ( das Gadget gibt es nicht )
                  
                  If GetGadgetState(#optText) And GetGadgetState(#optNormal)
                     
                     \cTextNormal = color             
                     
                  ElseIf GetGadgetState(#optText) And GetGadgetState(#optHover)
                     
                     \cTextHover = color             
                     
                  ElseIf GetGadgetState(#optOben) And GetGadgetState(#optNormal)
                     
                     \gcObenNormal = color
                     
                  ElseIf GetGadgetState(#optUnten) And GetGadgetState(#optNormal)
                     
                     \gcUntenNormal = color                   
                     
                  ElseIf GetGadgetState(#optOben) And GetGadgetState(#optHover)
                     
                     \gcObenHover = color
                     
                  ElseIf GetGadgetState(#optUnten) And GetGadgetState(#optHover)
                     
                     \gcUntenHover = color
                     
                  EndIf
                  
                  SetColorInfo(buttonaktiv)                      
                  IButtonDrawInfo(color)                     
                  IButtonDraw(#cHover, \gcObenHover,  \gcUntenHover,  \cTextHover, \cBorderHover)
                  IButtonDraw(#cNormal, \gcObenNormal, \gcUntenNormal, \cTextNormal, \cBorderNormal)                     
                  PostEvent(#PB_Event_Gadget, #Window, buttonaktiv, #PB_EventType_MouseLeave)
                  
               Case #IButton1, #IButton2, #IButton3
                  If EventType() = #PB_EventType_LeftClick 
                     buttonaktiv = welchesGadget : *ib = GetGadgetData(buttonaktiv)
                     SetGadgetState(#optOben, 1)
                     SetGadgetState(#optNormal, 1)
                     SetColorInfo(buttonaktiv)                     
                     IButtonDrawInfo(*ib\gcObenNormal)                     
                     IButtonDraw(#cNormal, *ib\gcObenNormal, *ib\gcUntenNormal, *ib\cTextNormal, \cBorderNormal)
                     IButtonDraw(#cHover, *ib\gcObenHover,  *ib\gcUntenHover,  *ib\cTextHover, \cBorderHover) 
                  EndIf
                  
               Case #IButton12 
                  If EventType() = #PB_EventType_LeftClick
                     IButtonCopyColor(#IButton1, #IButton2)
                  EndIf
                  If EventType() = #PB_EventType_RightClick
                     IButtonCopyColor(#IButton2, #IButton1)
                     PostEvent(#PB_Event_Gadget, #Window, #IButton1, #PB_EventType_LeftClick)
                     PostEvent(#PB_Event_Gadget, #Window, #IButton1, #PB_EventType_MouseLeave)
                  EndIf
                  
               Case #IButton13
                  If EventType() = #PB_EventType_LeftClick
                     IButtonCopyColor(#IButton1, #IButton3)
                  EndIf
                  If EventType() = #PB_EventType_RightClick
                     IButtonCopyColor(#IButton3, #IButton1)
                     PostEvent(#PB_Event_Gadget, #Window, #IButton1, #PB_EventType_LeftClick)
                     PostEvent(#PB_Event_Gadget, #Window, #IButton1, #PB_EventType_MouseLeave)
                  EndIf
                  
               Case #cNormal, #cHover, #cInfo, #cInfotext
                  If EventType() = #PB_EventType_LeftClick 
                     ColorToClipboard(EventGadget(), buttonaktiv)
                  EndIf        
                  
               ;---  Options                  
               Case #optNormal, #optHover, #optOben, #optUnten, #optText, #optFBox 
                  SetGadgetText(#colorboxinfo, GetGadgetText(welchesGadget))                  
                  SetColorInfo(buttonaktiv)                  
                  If GetGadgetState(#optNormal)
                     If GetGadgetState(#optText) : colorinfo = *ib\cTextNormal : EndIf
                     If GetGadgetState(#optOben) : colorinfo = *ib\gcObenNormal : EndIf
                     If GetGadgetState(#optUnten): colorinfo = *ib\gcUntenNormal : EndIf                     
                  ElseIf GetGadgetState(#optHover)
                     If GetGadgetState(#optText) : colorinfo = *ib\cTextHover : EndIf
                     If GetGadgetState(#optOben) : colorinfo = *ib\gcObenHover : EndIf
                     If GetGadgetState(#optUnten): colorinfo = *ib\gcUntenHover : EndIf                     
                  EndIf                  
                  IButtonDrawInfo(colorinfo)
                  
            EndSelect
            
      EndSelect
      
   Until Event = #PB_Event_CloseWindow   
EndWith

ColorIni(#IButton1,1) : ColorIni(#IButton2,1) : ColorIni(#IButton3,1) :

DataSection
   Data.i #White, #Yellow, #Green, #Blue, #Magenta, #Red, $CCFFFF, $3399FF 
   Data.i $FFFF00, $99CC00, $CC6633, $660000, $008800, $8080FF, #Gray, #Black, 0, 0, 0
EndDataSection
Purebasic 5.70 x86 5.72 X 64 - Windows 10

Der Computer hat dem menschlichen Gehirn gegenüber nur einen Vorteil: Er wird benutzt
grüße hjbremer
Antworten