Seite 1 von 2

Imagegadget als Buttonleiste nur mit einer Struktur

Verfasst: 19.03.2008 18:56
von hjbremer
Da ich globale Variablen nicht mag und shared Variablen auch nicht besonders, aber man zwischen Prozeduren trotzdem Werte austauschen muß, hier mal ein Beispiel mit einer Struktur.

In den Prozeduren gibt es keine normalen Variablen mehr. Nur noch eine einzige Struktur. Zugegeben teilweise etwas übertrieben, aber es ist möglich wie man sieht.

Dieses Beispiel zeigt, wie man mit einer Struktur arbeiten kann. Werte austauschen, Übergabe und Gebrauch von With EndWith.

Besonders einfach ist es dadurch, Prozeduren in Include Dateien zu schieben.

Code: Alles auswählen

;Imagegadget als Buttonleiste, gesteuert über eine Struktur
;16.03.2008 hjbremer Windows XP mit oder ohne XP-SKIN PB 4.1

Declare myImageButton(*i)
Declare myImageButtonSection(*i)
Declare myImageButtonState(*i)
Declare myImageButtonFarbverlaufV(*i)

Structure myimgbuttondata
window.l    ;wenn OpenWindow <> 0 dann muß definiert werden !!!
pbnr.l      ;ButtonNr des Imagegadget
anz.l       ;Anzahl Button pro Imagegadget
sp.l        ;Startspalte der Buttonleiste
ze.l        ;Startzeile 
br.l        ;Gesamtbreite der Buttonleiste
hh.l        ;Höhe
text.s[11]  ;die Buttonleiste mit den meisten Buttons/Sectionen, deren Anzahl muß hier stehen + 1
            ;also z.B. 2 Leisten, eine mit 3, eine mit 5 Buttons, dann muß hier mindestens eine 6 stehen
;optional
start.l     ;welcher Button/Section am Anfang gedrückt
icon1.l     ;Icon normal 
icon2.l     ;Icon gedrückt 
fontid.l    ;TextFont = FontID() 
frame1.l    ;EDGE_BUMP	EDGE_ETCHED	EDGE_RAISED	EDGE_SUNKEN
frame2.l    ;EDGE_BUMP	EDGE_ETCHED	EDGE_RAISED	EDGE_SUNKEN
farbeN1.l   ;HintergrundFarbe normal
farbeN2.l   ;HintergrundFarbe normal 
farbeNT.l   ;Textfarbe normal
farbeH1.l   ;HintergrundFarbe gedrückt
farbeH2.l   ;HintergrundFarbe gedrückt 
farbeHT.l   ;Textfarbe gedrückt
noframe.l   ;1 für keine Rahmen
noverlauf.l ;1 für keinen Farbverlauf

;interne Variablen
dc.l
id.l
iconsize.l  ;intern auf 16 festgelegt
iconzeile.l
iconspalte.l
iconpixel.l
section_sp.l
section_br.l
section_hh.l
section_nr.l
section_alt.l
section_icon.l
section_frame.l
section_farbe1.l
section_farbe2.l
section_farbeT.l
rect.rect
vertex0.TRIVERTEX       ;Reihenfolge nicht ändern !
vertex1.TRIVERTEX
gradient.GRADIENT_RECT
EndStructure

Procedure myImageButton(*i.myimgbuttondata)

With *i

  ;Vorgaben
  If \fontid = 0
     \fontid = GetStockObject_(#ANSI_VAR_FONT)
  EndIf
  
  If \farbeN1 = 0: \farbeN1 = #White : EndIf
  If \farbeN2 = 0: \farbeN2 = #Gray  : EndIf
  If \farbeNT = 0: \farbeNT = #Black : EndIf
  If \farbeH1 = 0: \farbeH1 = #Yellow: EndIf
  If \farbeH2 = 0: \farbeH2 = #Black : EndIf
  If \farbeHT = 0: \farbeHT = #White : EndIf

  If \frame1 = 0: \frame1 = #EDGE_RAISED: EndIf
  If \frame2 = 0: \frame2 = #EDGE_SUNKEN: EndIf
    
  ;Breite anpassen per Modulo Operator
  If \br % \anz
     \br - (\br % \anz)
  EndIf
  ;interne Variablen Werte
  \section_br = \br / \anz
  \section_hh = \hh   
  If \icon1
    \iconsize   = 16                          ;Icongröße   
    \iconspalte = 4                           ;Abstand Icon links
    \iconzeile  = (\hh - \iconsize) / 2       ;
    \iconpixel = \iconspalte + \iconsize + 5  ;um diesen Wert verringert sich der Platz für den Text
    If \icon2 = 0: \icon2 = \icon1: EndIf 
  EndIf
  
  ;Definition
  \id = CreateImage(\pbnr,\br,\hh)        
  \dc = StartDrawing(ImageOutput(\pbnr))
                                
          \section_icon   = \icon1
          \section_frame  = \frame1
          \section_farbe1 = \farbeN1
          \section_farbe2 = \farbeN2
          \section_farbeT = \farbeNT
          
          \section_nr = 0 ;section beginnt bei null
          
          Repeat           
                                      
              myImageButtonSection(*i)
              \section_nr + 1
              
          Until \section_nr = \anz
          
          If \start
              \section_nr = \start-1     ;section beginnt bei null; StartVorgabe ab 1, darum 1 abziehen
              \section_alt = \start-1    
              \section_icon = \icon2
              \section_frame = \frame2
              \section_farbe1 = \farbeH1
              \section_farbe2 = \farbeH2
              \section_farbeT = \farbeHT
              myImageButtonSection(*i)
          EndIf

       StopDrawing()
        
  ImageGadget(\pbnr,\sp,\ze,0,0,\id,#PB_Image_Border)

EndWith

EndProcedure  

Procedure myImageButtonSection(*i.myimgbuttondata)

;Hinweis: 1.Section hat hier die Nummer 0
;
;Hinweis: das DC kommt von myImageButton, wie auch fast alle anderen Werte

With *i
 
  ;jeweilige Startspalte einer Section ist
  \section_sp = \section_nr * \section_br
      
  ;Hintergrund
  If \noverlauf
    Box(\section_sp, 0, \section_br, \section_hh, \section_farbe1)
  Else
    myImageButtonFarbverlaufV(*i) 
  EndIf       
     
  ;Rahmen
  If \noframe 
  Else
      ;sp,ze,br,hh
      SetRect_(\rect, \section_sp, 0, \section_sp + \section_br, \section_hh)  
      DrawEdge_(\dc, \rect, \section_frame, #BF_RECT)        
  EndIf
  
  ;Icon
  If \section_icon    
      DrawImage(\section_icon, \section_sp + \iconspalte, \iconzeile, \iconsize, \iconsize)      
  EndIf

  ;Textfont
  SelectObject_(\dc,\fontid)       
  
  ;Textmode + Farbe
  SetBkMode_(\dc, #TRANSPARENT)     
  SetTextColor_(\dc, \section_farbeT)
  
  ;Vorgaben für gedachten TextRahmen r.rect
  SetTextAlign_(\dc, #TA_TOP)                           ;ohne geht es nicht
  SetRect_(\rect, 0, 0, \section_br-\iconpixel, \hh)    ;left,top,right,bottom = sp,ze,br,hh
      
  ;berechne via Api die Größe von r.rect für den Text !!!
  DrawText_(\dc, \text[\section_nr], -1, \rect, #DT_CALCRECT|#DT_WORDBREAK)  
      
  ;zeichne Text
  OffsetRect_(\rect, \section_sp + ((\section_br - \rect\right)/2), (\hh - \rect\bottom)/2)

  DrawText_(\dc, \text[\section_nr], -1, \rect, #DT_CENTER|#DT_WORDBREAK) 
  
EndWith  
          
EndProcedure

Procedure myImageButtonState(*i.myimgbuttondata)

With *i
  
  ;es folgt Farbe wechseln
  
  \dc = StartDrawing(ImageOutput(\pbnr))
      
          ;1. vorherige Section zurücksetzen
          \section_nr = \section_alt
          \section_icon = \icon1
          \section_frame = \frame1
          \section_farbe1 = \farbeN1
          \section_farbe2 = \farbeN2
          \section_farbeT = \farbeNT
          myImageButtonSection(*i)
                  
          ;2. wo wurde im ImageGadget gedrückt
          \section_nr = 0 
           Repeat                                      
             If WindowMouseX(\window) < (\section_br * \section_nr) + \sp 
                Break                                                    
             EndIf
             \section_nr + 1
           Until \section_nr = \anz

          ;3. Section hervorheben
          \section_nr - 1                ;section in myImageButtonSection(*i) beginnt bei null
          \section_icon = \icon2
          \section_frame = \frame2
          \section_farbe1 = \farbeH1
          \section_farbe2 = \farbeH2
          \section_farbeT = \farbeHT
          myImageButtonSection(*i)
          
          \section_alt = \section_nr
       
       StopDrawing()
       
       SetGadgetState(\pbnr,\id)
              
ProcedureReturn \section_nr + 1      ;oben abgezogene 1 wieder addieren, 
                                       ;damit man ein Ergebnis zwischen 1 und Anzahl Buttons erhält
EndWith

EndProcedure

Procedure myImageButtonFarbverlaufV(*i.myimgbuttondata)

 ;http://msdn2.microsoft.com/en-us/library/ms532338(VS.85).aspx
 
 ;vertex0 + vertex1 = 2 Blöcke hintereinander und diese 2 steht bei Gradientfill_

With *i
     
  ;UpperLeft = oben links vom Viereck
  \vertex0\x     = \section_sp 
  \vertex0\y     = 0
  \vertex0\Red   = Red(\section_farbe1)  <<8
  \vertex0\Green = Green(\section_farbe1)<<8
  \vertex0\Blue  = Blue(\section_farbe1) <<8 
  
  ;LowerRight = unten rechts
  \vertex1\x     = \section_sp + \section_br
  \vertex1\y     = \section_hh
  \vertex1\Red   = Red(\section_farbe2)  <<8 
  \vertex1\Green = Green(\section_farbe2)<<8 
  \vertex1\Blue  = Blue(\section_farbe2) <<8 
   
  \gradient\UpperLeft  = 0  ;an diesen Werten nicht rumspielen
  \gradient\LowerRight = 1  ;sonst Memory Fehler
  
  Gradientfill_(\dc, \vertex0, 2, \gradient, 1, #GRADIENT_FILL_RECT_V) 

EndWith

EndProcedure

;DEMO==========================================================

Enumeration
#button1
#button2
EndEnumeration

#winbreite = 700
#winhoehe  = 300

flag = #PB_Window_SystemMenu|1|#PB_Window_Invisible
hwnd = OpenWindow(0,0,0,#winbreite,#winhoehe,"Test",flag) 
       CreateGadgetList(hwnd)
              
       With mybuttondata1.myimgbuttondata
         \pbnr=#button1                 
         \anz=5
         \sp=10      
         \ze=10      
         \br=#winbreite-20      
         \hh=40      
         \start=2                
         \text[0]="Hallo Basic Programmierer"
         \text[1]="Hallo2"
         \text[2]="Hallo3"
         \text[3]="Hallo4"
         \text[4]="Hallo5"
       EndWith
       myImageButton(mybuttondata1)
             
       With mybuttondata2.myimgbuttondata
         \pbnr=#button2                 
         \anz=3
         \sp=40      
         \ze=90      
         \br=300      
         \hh=32    
         \farbeN1=#White        
         \farbeN2=#Green        
         \farbeH1=#White        
         \farbeH2=#Yellow       
         \farbeNT=$BB1122        
         \farbeHT=$105DF6 
         \icon1=ExtractIcon_(0,"shell32.dll",140)                 
         \icon2=ExtractIcon_(0,"shell32.dll",141)        
         \text[0]="Hallo du Progger du"
         \text[1]="mehr Hallo"
         \text[2]="nur Hallo"
       EndWith
       myImageButton(mybuttondata2)
             
       HideWindow(0,0)
 
Repeat: event = WaitWindowEvent() 
   
  If Event = #PB_Event_Gadget Or Event = #PB_Event_Menu 
          
      welcherButton=EventGadget()  
      
      Select welcherButton
      
            Case #button1
                  
                  Select myImageButtonState(mybuttondata1)
                      Case 1: Debug 1
                      Case 2: Debug 2 
                      Case 3: Debug 3
                      Case 4: Debug 4 
                      Case 5: Debug 5 
                  EndSelect
                  
            Case #button2
                  wb = myImageButtonState(mybuttondata2)
                  Select wb
                      Case 1: Debug wb
                      Case 2: Debug wb
                      Case 3: Debug wb
                  EndSelect
                  
      EndSelect
            
  EndIf

Until event = #PB_Event_CloseWindow 

End


Verfasst: 20.03.2008 15:40
von dige
Stark. :allright:
Sieht man den paar Zeilen Quellcode gar nicht an was da so alles drin
steckt. Gut gemacht!

Verfasst: 20.03.2008 16:08
von Rings
sehr schön.
und jetzt an alle:

geht das auch ohne Api so das es crossplattform wäre ?

Verfasst: 22.03.2008 13:59
von hjbremer
Crossplattform ist nicht mein Ding. Gibt aber eigentlich nur zwei kleine Hürden. Gradientfill_ und DrawText_ mit #DT_CALCRECT|#DT_WORDBREAK. Für Gradientfill_ gibt es eine Routine im Codearchiv, die mit Line arbeitet. Nur für #DT_CALCRECT|#DT_WORDBREAK muß man wohl etwas mehr Aufwand treiben.

Aber hier eine kleine Erweiterung zu obigem Code. Wenn man mit der Maus einen Button berührt, wird der Teil heller.

Die einzigen Änderungen sind: in der Struktur eine Variable mehr, beim Erstellen der Buttons am Ende der Prozedur myImageButton ein Aufruf dazu und 2 zusätzliche Prozeduren.
Das ganze kommt wieder ohne shared und globale Variablen aus. Das Ganze ähnelt einem früheren Code von mir, ist aber besser finde ich.

Hier der komplette Code:

Code: Alles auswählen

;Imagegadget als Buttonleiste, gesteuert über eine Struktur
;mit Highlight wenn Maus Button berührt
;21.03.2008 hjbremer Windows XP mit oder ohne XP-SKIN PB 4.1

Declare myImageButton(*i)
Declare myImageButtonSection(*i)
Declare myImageButtonState(*i)
Declare myImageButtonFarbverlaufV(*i)

Declare myImageButtonHighlight(*i)
Declare my_callback(hWnd, Msg, wParam, lParam) 

Structure myimgbuttondata
window.l    ;wenn OpenWindow <> 0 dann muß definiert werden !!!
pbnr.l      ;ButtonNr des Imagegadget
anz.l       ;Anzahl Button pro Imagegadget
sp.l        ;Startspalte der Buttonleiste
ze.l        ;Startzeile 
br.l        ;Gesamtbreite der Buttonleiste
hh.l        ;Höhe
text.s[11]  ;die Buttonleiste mit den meisten Buttons/Sectionen, deren Anzahl muß hier stehen + 1
            ;also z.B. 2 Leisten, eine mit 3, eine mit 5 Buttons, dann muß hier mindestens eine 6 stehen
;optional
start.l     ;welcher Button/Section am Anfang gedrückt
icon1.l     ;Icon normal 
icon2.l     ;Icon gedrückt 
fontid.l    ;TextFont = FontID() 
frame1.l    ;EDGE_BUMP	EDGE_ETCHED	EDGE_RAISED	EDGE_SUNKEN
frame2.l    ;EDGE_BUMP	EDGE_ETCHED	EDGE_RAISED	EDGE_SUNKEN
farbeN1.l   ;HintergrundFarbe normal
farbeN2.l   ;HintergrundFarbe normal 
farbeNT.l   ;Textfarbe normal
farbeH1.l   ;HintergrundFarbe gedrückt
farbeH2.l   ;HintergrundFarbe gedrückt 
farbeHT.l   ;Textfarbe gedrückt
noframe.l   ;1 für keine Rahmen
noverlauf.l ;1 für keinen Farbverlauf

;interne Variablen
dc.l
id.l
iconsize.l  ;intern auf 16 festgelegt
iconzeile.l
iconspalte.l
iconpixel.l
section_sp.l
section_br.l
section_hh.l
section_nr.l
section_alt.l
section_icon.l
section_frame.l
section_farbe1.l
section_farbe2.l
section_farbeT.l
rect.rect
vertex0.TRIVERTEX       
vertex1.TRIVERTEX
gradient.GRADIENT_RECT
callbackflag.l
EndStructure

Procedure myImageButton(*i.myimgbuttondata)

With *i

  ;Vorgabe Font
  If \fontid = 0
     \fontid = GetStockObject_(#ANSI_VAR_FONT)
  EndIf
  
  ;Vorgabe Farben
  If \farbeN1 = 0: \farbeN1 = #White : EndIf
  If \farbeN2 = 0: \farbeN2 = #Gray  : EndIf
  If \farbeNT = 0: \farbeNT = #Black : EndIf
  If \farbeH1 = 0: \farbeH1 = #Yellow: EndIf
  If \farbeH2 = 0: \farbeH2 = #Gray  : EndIf
  If \farbeHT = 0: \farbeHT = $000FFF: EndIf

  If \frame1 = 0: \frame1 = #EDGE_RAISED: EndIf
  If \frame2 = 0: \frame2 = #EDGE_SUNKEN: EndIf
    
  ;Breite anpassen per Modulo Operator
  If \br % \anz
     \br - (\br % \anz)
  EndIf
  ;interne Variablen Werte
  \section_br = \br / \anz
  \section_hh = \hh   
  If \icon1
    \iconsize   = 16                          ;Icongröße   
    \iconspalte = 4                           ;Abstand Icon links
    \iconzeile  = (\hh - \iconsize) / 2       ;
    \iconpixel = \iconspalte + \iconsize + 5  ;um diesen Wert verringert sich der Platz für den Text
    If \icon2 = 0: \icon2 = \icon1: EndIf 
  EndIf
  
  ;Definition
  \id = CreateImage(\pbnr,\br,\hh)        
  \dc = StartDrawing(ImageOutput(\pbnr))
                                
          \section_icon   = \icon1
          \section_frame  = \frame1
          \section_farbe1 = \farbeN1
          \section_farbe2 = \farbeN2
          \section_farbeT = \farbeNT
          
          \section_nr = 0 ;section beginnt intern bei null
          
          Repeat           
                                      
              myImageButtonSection(*i)
              \section_nr + 1
              
          Until \section_nr = \anz
          
          If \start
              \section_nr = \start-1     ;section beginnt in myImageButtonSection bei null
              \section_alt = \start-1    ; StartVorgabe ab 1, darum 1 abziehen
              \section_icon = \icon2
              \section_frame = \frame2
              \section_farbe1 = \farbeH1
              \section_farbe2 = \farbeH2
              \section_farbeT = \farbeHT
              myImageButtonSection(*i)
          EndIf

       StopDrawing()
        
  ImageGadget(\pbnr,\sp,\ze,0,0,\id,#PB_Image_Border)
  

EndWith

;aufrufen um einige Werte zu speichern
;nur nötig wegen Callback, um globale oder shared Variablen zu vermeiden
myImageButtonHighlight(*i)

EndProcedure  

Procedure myImageButtonSection(*i.myimgbuttondata)

;Hinweis: 1.Section hat hier die Nummer 0
;
;Hinweis: das DC kommt von myImageButton, wie auch fast alle anderen Werte

With *i
 
  ;jeweilige Startspalte einer Section ist
  \section_sp = \section_nr * \section_br
      
  ;Hintergrund
  If \noverlauf
    Box(\section_sp, 0, \section_br, \section_hh, \section_farbe1)
  Else
    myImageButtonFarbverlaufV(*i) 
  EndIf       
     
  ;Rahmen
  If \noframe 
  Else
      ;sp,ze,br,hh
      SetRect_(\rect, \section_sp, 0, \section_sp + \section_br, \section_hh)  
      DrawEdge_(\dc, \rect, \section_frame, #BF_RECT)        
  EndIf
  
  ;Icon
  If \section_icon    
      DrawImage(\section_icon, \section_sp + \iconspalte, \iconzeile, \iconsize, \iconsize)      
  EndIf

  ;Textfont
  SelectObject_(\dc,\fontid)       
  
  ;Textmode + Farbe
  SetBkMode_(\dc, #TRANSPARENT)     
  SetTextColor_(\dc, \section_farbeT)
  
  ;Vorgaben für gedachten TextRahmen r.rect
  SetTextAlign_(\dc, #TA_TOP)                           ;ohne geht es nicht
  SetRect_(\rect, 0, 0, \section_br-\iconpixel, \hh)    ;left,top,right,bottom = sp,ze,br,hh
      
  ;berechne via Api die Größe von r.rect für den Text !!!
  DrawText_(\dc, \text[\section_nr], -1, \rect, #DT_CALCRECT|#DT_WORDBREAK)  
      
  ;zeichne Text
  OffsetRect_(\rect, \section_sp + ((\section_br - \rect\right)/2), (\hh - \rect\bottom)/2)

  DrawText_(\dc, \text[\section_nr], -1, \rect, #DT_CENTER|#DT_WORDBREAK) 
  
EndWith  
          
EndProcedure

Procedure myImageButtonState(*i.myimgbuttondata)

With *i
  
  ;es folgt Farbe wechseln
  
  \dc = StartDrawing(ImageOutput(\pbnr))
      
          ;1. vorherige Section zurücksetzen
          \section_nr = \section_alt
          \section_icon = \icon1
          \section_frame = \frame1
          \section_farbe1 = \farbeN1
          \section_farbe2 = \farbeN2
          \section_farbeT = \farbeNT
          myImageButtonSection(*i)
                  
          ;2. wo wurde im ImageGadget gedrückt
          \section_nr = 0 
           Repeat                                      
             If WindowMouseX(\window) < (\section_br * \section_nr) + \sp 
                Break                                                    
             EndIf
             \section_nr + 1
           Until \section_nr = \anz

          ;3. Section hervorheben
          \section_nr - 1                ;section in myImageButtonSection(*i) beginnt bei null
          \section_icon = \icon2
          \section_frame = \frame2
          \section_farbe1 = \farbeH1
          \section_farbe2 = \farbeH2
          \section_farbeT = \farbeHT
          myImageButtonSection(*i)
          
          \section_alt = \section_nr
       
       StopDrawing()
       
       SetGadgetState(\pbnr,\id)
              
ProcedureReturn \section_nr + 1      ;oben abgezogene 1 wieder addieren, 
                                       ;damit man ein Ergebnis zwischen 1 und Anzahl Buttons erhält
EndWith

EndProcedure

Procedure myImageButtonFarbverlaufV(*i.myimgbuttondata)

 ;http://msdn2.microsoft.com/en-us/library/ms532338(VS.85).aspx
 
 ;vertex0 + vertex1 = 2 Blöcke hintereinander und diese 2 steht bei Gradientfill_

With *i
     
  ;UpperLeft = oben links vom Viereck
  \vertex0\x     = \section_sp 
  \vertex0\y     = 0
  \vertex0\Red   = Red(\section_farbe1)  <<8
  \vertex0\Green = Green(\section_farbe1)<<8
  \vertex0\Blue  = Blue(\section_farbe1) <<8 
  
  ;LowerRight = unten rechts
  \vertex1\x     = \section_sp + \section_br
  \vertex1\y     = \section_hh
  \vertex1\Red   = Red(\section_farbe2)  <<8 
  \vertex1\Green = Green(\section_farbe2)<<8 
  \vertex1\Blue  = Blue(\section_farbe2) <<8 
   
  \gradient\UpperLeft  = 0  ;an diesen Werten nicht rumspielen
  \gradient\LowerRight = 1  ;sonst Memory Fehler
  
  Gradientfill_(\dc, \vertex0, 2, \gradient, 1, #GRADIENT_FILL_RECT_V) 

EndWith

EndProcedure


;DEMO==========================================================

Enumeration
#window = 0
#button1
#button2
EndEnumeration

#winbreite = 700
#winhoehe  = 300

flag = #PB_Window_SystemMenu|1|#PB_Window_Invisible
hwnd = OpenWindow(0,0,0,#winbreite,#winhoehe,"myImageButtonHighlight",flag) 
       CreateGadgetList(hwnd)
              
       With mybuttondata1.myimgbuttondata
         \pbnr=#button1                 
         \anz=5
         \sp=10      
         \ze=10      
         \br=#winbreite-20      
         \hh=40      
         \start=2                
         \text[0]="Hallo Basic Programmierer"
         \text[1]="Hallo2"
         \text[2]="Hallo3"
         \text[3]="Hallo4"
         \text[4]="Hallo5"
       EndWith
       myImageButton(mybuttondata1)
             
       With mybuttondata2.myimgbuttondata
         \pbnr=#button2                 
         \anz=3
         \sp=40      
         \ze=90      
         \br=300      
         \hh=32    
         \farbeN1=$EEEEEE  
         \farbeN2=#Red          
         \farbeH1=#White        
         \farbeH2=#Yellow       
         \farbeNT=$BB1122        
         \farbeHT=$105DF6 
         \icon1=ExtractIcon_(0,"shell32.dll",140)                 
         \icon2=ExtractIcon_(0,"shell32.dll",141)        
         \text[0]="Hallo du Progger du"
         \text[1]="mehr Hallo"
         \text[2]="nur Hallo"
       EndWith
       myImageButton(mybuttondata2)
             
       HideWindow(0,0)
 
SetWindowCallback(@my_Callback())

Repeat: event = WaitWindowEvent() 
   
  If Event = #PB_Event_Gadget Or Event = #PB_Event_Menu 
          
      welcherButton=EventGadget()  
      
      Select welcherButton
      
            Case #button1
                  
                  Select myImageButtonState(mybuttondata1)
                      Case 1: Debug 1
                      Case 2: Debug 2 
                      Case 3: Debug 3
                      Case 4: Debug 4 
                      Case 5: Debug 5 
                  EndSelect
                  
            Case #button2
                  wb = myImageButtonState(mybuttondata2)
                  Select wb
                      Case 1: Debug wb
                      Case 2: Debug wb
                      Case 3: Debug wb
                  EndSelect
                  
      EndSelect
            
  EndIf

Until event = #PB_Event_CloseWindow 

End

Procedure myImageButtonHighlight(*i.myimgbuttondata)

;diese Routine dient dazu einen Teil der ImageGadgetButtonleiste heller und wieder dunkler zu machen
;der Callback ruft diese Routine auf  

Structure highlightimgdata
  sp.l    ;Spalte des Imagegadget im Window
  ze.l    ;momentan nicht benötigt
  br.l    ;Breite einer Section
  hh.l    ;Höhe
  id.l    ;ID des Image
  anz.l   ;Anzahl der Sectionen
EndStructure

Static maxindex = 0
Static Dim d.highlightimgdata(0)

Static lastbutton=0
Static lastsection=0
 
If *i\callbackflag = 0
    
     ;*i kommt von myImageButton 
     ;hier werden beim Programmstart wichtige Werte ins statische Feld geschrieben 
     ;dadurch erspart man sich die ButtonleisteStruktur der einzelnen Leisten shared zu machen

     index = *i\pbnr
     
     ;Feld bei Bedarf vergrößern
     If index > maxindex
        maxindex=index
        ReDim d.highlightimgdata(maxindex)
     EndIf
     
     d(index)\sp = *i\sp
     d(index)\ze = *i\ze
     d(index)\br = *i\section_br
     d(index)\hh = *i\section_hh
     d(index)\id = *i\id
     d(index)\anz= *i\anz
             
Else

     ;*i kommt vom Callback    
     ;dieser Teil wird vom Callback aufgerufen
      
     button = *i\pbnr
     heller = 22
       
     If button  ;wenn wahr, Maus befindet sich im Gadget
       
           ;in welcher Section ist die Maus im ImageGadget beginnend bei 1
           section = 1 
           Repeat 
             If *i\sp < (d(button)\br * section) + d(button)\sp 
                Break                                                    
             EndIf
             section + 1
           Until section = d(button)\anz
           
           ;wenn anderer Button als vorher
           If button <> lastbutton  
              lastbutton=button
              lastsection=0
           EndIf
             
           ;wenn Maus in andere Section eines Button kommt
           If section <> lastsection 
                                    
               ze = 0
               br = d(button)\br
               hh = d(button)\hh        
          
               dc = StartDrawing(ImageOutput(button))
          
                     ;Startspalte LastSection ist
                     sp = (lastsection-1) * d(button)\br 
                
                     ;in LastSection die Brightness wiederherstellen
                     ca.COLORADJUSTMENT                    
                     SetStretchBltMode_(dc,#HALFTONE)
                     GetColorAdjustment_(dc,ca.COLORADJUSTMENT)
                     ca\caBrightness=-heller 
                     SetColorAdjustment_(dc,ca.COLORADJUSTMENT)
                     StretchBlt_(dc, sp,0,br,hh,dc, sp,0,br,hh, #SRCCOPY)
               
                     ;Startspalte neue Section ist
                     sp = (section-1) * d(button)\br 
               
                     ;in neuer Section Brightness höher
                     ca\caBrightness=heller
                     SetColorAdjustment_(dc,ca.COLORADJUSTMENT)
                     StretchBlt_(dc, sp,0,br,hh,dc, sp,0,br,hh, #SRCCOPY)
                     
                     lastsection=section

                    StopDrawing()
                    
               SetGadgetState(button, d(button)\id)   
             
           EndIf
           
     Else
           ;Mouse hat Gadget verlassen, LastSection wiederherstellen
           
           ;Startspalte LastSection ist
           sp = (lastsection-1) * d(lastbutton)\br 
      
           ze = 0
           br = d(lastbutton)\br
           hh = d(lastbutton)\hh        
           
           dc = StartDrawing(ImageOutput(lastbutton))
      
                 ca.COLORADJUSTMENT                    
                 SetStretchBltMode_(dc,#HALFTONE)
                 GetColorAdjustment_(dc,ca.COLORADJUSTMENT)
                 ca\caBrightness=-heller 
                 SetColorAdjustment_(dc,ca.COLORADJUSTMENT)
                 StretchBlt_(dc, sp,0,br,hh,dc, sp,0,br,hh, #SRCCOPY)
      
                StopDrawing()
                
           SetGadgetState(lastbutton, d(lastbutton)\id)   
            
           lastbutton=0
           lastsection=0 
      
     EndIf

EndIf
    
EndProcedure

Procedure my_callback(hWnd, Msg, wParam, lParam) 

result = #PB_ProcessPureBasicEvents 

Static imgflag

Select msg 
        
    Case #WM_SETCURSOR
    
        buttonid = wParam
        
        button = GetDlgCtrlID_(buttonid): ;Debug button
               
              Select button
              
                  ;setzt voraus das Konstanten bei Enumeration hintereinander liegen
                  ;sonst halt einzeln aufführen
                  Case #button1 To #button2 
                     
                     ;Maus im ImageButton
                     GetCursorPos_(p.POINT) 
                     MapWindowPoints_(0, hwnd, p, 1) 
                     
                        imgflag=1
                        img.myimgbuttondata
                        img\sp=p\x
                        img\pbnr=button
                        img\callbackflag=1
                        myImageButtonHighlight(img)                     
                        
                  Default
                     
                     ;Maus nicht im ImageButton
                     If imgflag
                        imgflag=0
                        img.myimgbuttondata
                        img\sp=0
                        img\pbnr=0
                        img\callbackflag=1
                        myImageButtonHighlight(img)
                     EndIf
                  
              EndSelect ;von button 
        
EndSelect ;von msg

ProcedureReturn result 
EndProcedure

Verfasst: 03.12.2008 16:18
von kwai chang caine
Suchen, wie ich diesen Code verändern, um sicherzustellen, dass die Tasten gedrückt bleiben nicht, und ich habe nicht gefunden.
Jemand kann mir helfen?

Verfasst: 03.12.2008 21:16
von hjbremer
i dont understand you, but i think you mean the edge/frame of the buttons.

without frame, you must write and set the \noframe Flag to 1 in the section mybuttondata.myimgbuttondata

Code: Alles auswählen

       With mybuttondata2.myimgbuttondata 
         \pbnr=#button2                  
         \anz=3 
         \sp=40      
         \ze=90      
         \br=300      
         \hh=32    
         \farbeN1=$EEEEEE  
         \farbeN2=#Red          
         \farbeH1=#White        
         \farbeH2=#Yellow        
         \farbeNT=$BB1122        
         \farbeHT=$105DF6 
         \icon1=ExtractIcon_(0,"shell32.dll",140)                  
         \icon2=ExtractIcon_(0,"shell32.dll",141)        
         \text[0]="Hallo du Progger du" 
         \text[1]="mehr Hallo" 
         \text[2]="nur Hallo"
         
         \noframe=1 

       EndWith 
       myImageButton(mybuttondata2)

please excuse me, my bad english

Verfasst: 03.12.2008 21:37
von KatSeiko
Problems with german? Try this forum!

Verfasst: 03.12.2008 21:39
von X360 Andy
KatSeiko hat geschrieben:Problems with german? Try this forum!
http://www.purebasic.fr/english/profile ... ile&u=2947

:lol:

Verfasst: 03.12.2008 22:02
von KatSeiko
Ich weiß nicht, aus welcher Sprache heraus das übersetzt wurde, aber ich finde, dass eine schlechte übersetzung schlechter ist als gar keine.

Verfasst: 03.12.2008 22:13
von Kiffi
KatSeiko hat geschrieben:Ich weiß nicht, aus welcher Sprache heraus das übersetzt wurde, aber ich finde, dass eine schlechte übersetzung schlechter ist als gar keine.
prinzipiell stimme ich Dir zu.

kwai chang caine ist Franzose, liest und schreibt im englischen Forum
und sucht auch hier ab und zu Hilfe. Da er höflich ist, versucht er seine
Frage in deutscher Sprache zu stellen (es käme von uns auch keiner auf die
Idee, im englischen Forum auf Deutsch zu fragen). Dass die Ergebnisse
seiner Übersetzungssoftware eher suboptimal sind, kann er ja nicht wissen.

Grüße ... Kiffi