meine neue MessageBox

Windowsspezifisches Forum , API ,..
Beiträge, die plattformübergreifend sind, gehören ins 'Allgemein'-Forum.
Benutzeravatar
hjbremer
Beiträge: 822
Registriert: 27.02.2006 22:30
Computerausstattung: von gestern
Wohnort: Neumünster

meine neue MessageBox

Beitrag von hjbremer »

alt und doch neu, ein Infofenster als Modul

Code: Alles auswählen

;MessageBox, V.2.0, ab PB 5.41 x86 Windows - Jan.2022 by HJBremer

;Messagebox ist immer StickyWindow(), benutzt intern ein ListIconGadget, hat max 5 Buttons + CloseButton

DeclareModule MessageBox2   
   
   Declare.i MessageBox(pbnr, title$, header$, width=400, height=435)
   
   Declare.i MessageBoxButton(text$)
   Declare.i MessageBoxAddText(text$, color=#Black)
   
   ;MessageBox(101, "Info", "2022,140,2|Produkt,100|Preis,70,1")
   ;
   ;      nr: entspricht Gadgetnr vom ListIconGadget(), #pb_any wird unterstützt
   ;   title: WindowText oben
   ;  header: Definition der Spalten getrennt durch |
   ;          ColumnName, ColumnBreite, format: LEFT=0, RIGHT=1, CENTER=2
   
   ;MessageBoxAddText("10 kg|Äpfel|12,50")
   ;  Spalten getrennt durch | oder #LF$, entspricht AddGadgetItem()
  
   ;MessageBoxButton("Button1|Button2") usw für max 5 Buttons
   ;  für mehr Buttons #lastbutton + MessageBoxGadgetEvent() ändern
   ;
   
   ;  Rückgabe: EventGadget() = pbnr der MessageBox , siehe Demo
   ;            EventType() 101-105 für Button 1-5 und #PB_EventType_CloseItem für Close
   

EndDeclareModule

Module MessageBox2
   EnableExplicit
   
   #gap = 15            ;Abstand für Rand und zwischen Buttons
   #deli = "|"          ;delimiter Trennzeichen
   #buttonheight = 26
   #lastbutton = 6                  ;lastbutton ist immer der CloseButton
   Global Dim button(#lastbutton)   ;Button 1 bis 5 zur freien Verfügung  
   
   Global winx, winy                ;merkt sich Windowposition, wenn verschoben
   
   Global msgboxliste         ;pbnr der Liste (ListIconGadget) für Anzeige von Text 
   Global msgboxwindow = -1   ;pbnr des Windows, wird zur Sicherheit bei CloseWindow() immer auf -1 gesetzt
   
   Global msgbackcolor = $D7FFFF, msgheadcolor = #Blue      
   
   Global font_liste = LoadFont(#PB_Any, "Consolas", 11) 
   Global font_button = LoadFont(#PB_Any, "Tahoma", 10)
   
   Procedure.i MessageBoxClose()
      winx = WindowX(msgboxwindow)
      winy = WindowY(msgboxwindow) 
      CloseWindow(msgboxwindow): msgboxwindow = -1  
      PostEvent(#PB_Event_Gadget, 0, msgboxliste, #PB_EventType_CloseItem)
   EndProcedure
     
   Procedure.i MessageBoxGadgetEvent()
      ;msgboxliste wird als Gadgetnr in der Main-Repeat-EventSchleife verwendet
      ;die Zahlen 1-5 für die Buttons werden via Eventype() abgefragt vom Mainprogramm
      
      Select EventGadget()
         Case button(1): PostEvent(#PB_Event_Gadget, 0, msgboxliste, 101)         
         Case button(2): PostEvent(#PB_Event_Gadget, 0, msgboxliste, 102)         
         Case button(3): PostEvent(#PB_Event_Gadget, 0, msgboxliste, 103)  
         Case button(4): PostEvent(#PB_Event_Gadget, 0, msgboxliste, 104)  
         Case button(5): PostEvent(#PB_Event_Gadget, 0, msgboxliste, 105)         
         Case button(#lastbutton): MessageBoxClose()
         Case msgboxliste
            If EventType() = #PB_EventType_LeftClick: : EndIf
      EndSelect   
      
   EndProcedure
   
   Procedure.i MessageBoxCallback(hWnd, msg, wParam, lParam) 
      
      If msg = #WM_CLOSE            ;CloseButton oben rechts vom MsgBoxWindow gedrückt
         MessageBoxClose()
         ProcedureReturn 0          ;wm_close wird nicht zum MainWindow weitergeleitet      
      EndIf      
      ProcedureReturn #PB_ProcessPureBasicEvents 
      
   EndProcedure 
   
   Procedure.i MessageBoxButton(text$)
      
      text$ = Trim(text$): If Right(text$, 1) <> #deli: text$ + #deli: EndIf
      
      Protected anz = CountString(text$, #deli)
      Protected t$   
      Protected j, w, x = #gap, y = GadgetY(button(#lastbutton))

      For j = 1 To anz 
         If j = #lastbutton: Break: EndIf
         t$ = StringField(text$, j, #deli)
         SetGadgetText(button(j), Trim(t$))      
         w = GadgetWidth(button(j), #PB_Gadget_RequiredSize) + 10
         ResizeGadget(button(j), x, y, w, #PB_Ignore)      
         HideGadget(button(j), 0)      
         x + w + #gap      
      Next
      
      SetGadgetText(button(#lastbutton), "Ende")
      w = GadgetWidth(button(#lastbutton), #PB_Gadget_RequiredSize) + 10
      ResizeGadget(button(#lastbutton), WindowWidth(msgboxwindow) - w - #gap, y, w, #PB_Ignore)   
      
   EndProcedure
   
   Procedure.i MessageBoxAddText(text$, color=#Black)
      
      text$ = ReplaceString(text$, #deli, #LF$)    ; #deli ist Trennzeichen <> #LF$
      AddGadgetItem(msgboxliste, -1, #LF$ + text$) ; #LF$ für Spalte null, welche nicht benutzt wird      
      SetGadgetItemColor(msgboxliste, CountGadgetItems(msgboxliste) -1, #PB_Gadget_FrontColor, color, #PB_All)  
      
   EndProcedure
   
   Procedure.i MessageBox(pbnr, title$, header$, width=400, height=435)     
      
      If IsWindow(msgboxwindow): MessageBoxClose(): EndIf  ;nur eine Messagebox zur Zeit   
      
      Protected msgwindowflag = #PB_Window_MinimizeGadget|#PB_Window_NoGadgets|#PB_Window_Invisible
      
      Protected id, oldGadgetList, tnr      
      Protected j, cols, colwidth, text$, item$   
      Protected lv.LV_COLUMN\mask = #LVCF_FMT      
      
      If winx 
         msgboxwindow  = OpenWindow(#PB_Any, winx, winy, width, height, "MsgBox", msgwindowflag)
      Else         
         msgboxwindow  = OpenWindow(#PB_Any, 0, 0, width, height, title$, msgwindowflag|#PB_Window_ScreenCentered)
      EndIf   
      
      oldGadgetList = UseGadgetList(WindowID(msgboxwindow)) 
      
      StickyWindow(msgboxwindow, 1)       
      SetWindowColor(msgboxwindow, msgbackcolor) 
      SetWindowCallback(@MessageBoxCallback(), msgboxwindow) 
      
      tnr = TextGadget(#PB_Any, 0, 0, width, 30, title$, #SS_CENTERIMAGE|#SS_CENTER)
      SetGadgetFont(tnr, FontID(font_liste))
      SetGadgetColor(tnr, #PB_Gadget_BackColor, msgbackcolor) 
      
      msgboxliste = pbnr      
      id = ListIconGadget(msgboxliste, -1, #buttonheight, width+2, height-30-50, "", 0, #LVS_NOCOLUMNHEADER)      
      If msgboxliste = #PB_Any: msgboxliste = id: EndIf
      
      SetGadgetFont(msgboxliste, FontID(font_liste))
      SetGadgetColor(msgboxliste, #PB_Gadget_BackColor, msgbackcolor) 
      BindGadgetEvent(msgboxliste, @MessageBoxGadgetEvent()) 
      
      cols = CountString(header$, #deli) + 1            ;z.B. "2022,100,2|Produkt,150|Preis,145,1" 
      
      For j = 1 To cols
         text$ = StringField(header$, j, #deli)           ;      = 2022,100,2         
         item$ + Trim(StringField(text$, 1, ",")) + #LF$  ;item$ + 2022
         colwidth = Val(StringField(text$, 2, ","))       ;colwidth = 100
         AddGadgetColumn(msgboxliste, j, "", colwidth)
         lv\fmt = Val(StringField(text$, 3, ","))        ;fmt = 2
         If lv\fmt     
            SendMessage_(GadgetID(msgboxliste), #LVM_SETCOLUMN, j, lv)
         EndIf
      Next   
      
      AddGadgetItem(msgboxliste, 0, "")
      AddGadgetItem(msgboxliste, 1, #LF$ + item$) ;Spalte Null wird nicht benutzt  title$
      SetGadgetItemColor(msgboxliste, 1, #PB_Gadget_FrontColor, msgheadcolor, #PB_All)   
      
      ;Buttons + Posi      
      For j = 1 To #lastbutton
         button(j) = ButtonGadget(#PB_Any, 0, 0, 0, #buttonheight, "") 
         HideGadget(button(j), 1)
         SetGadgetFont(button(j), FontID(font_button))         
         BindGadgetEvent(button(j), @MessageBoxGadgetEvent())      
      Next      
      SetGadgetText(button(#lastbutton), "ok")  ;#lastbutton ist immer der CloseButton      
      ResizeGadget(button(#lastbutton), (width-100)/2, height-#buttonheight-11, 100, #PB_Ignore)
      HideGadget(button(#lastbutton), 0)      
      
      UseGadgetList(oldGadgetList)  ;kehrt zur vorherigen Gadgetliste zurück  
      HideWindow(msgboxwindow, 0)   ;MsgBoxWindow anzeigen

   EndProcedure
   
EndModule

UseModule MessageBox2

CompilerIf #PB_Compiler_IsMainFile
   
   ;- DEMO
   
   Enumeration 
      #button1
      #button2
      #button3
   EndEnumeration
   
   Enumeration 100
      #msgbox1
      #msgbox2
      #msgbox3
   EndEnumeration
   
   DisableExplicit
   
   OpenWindow(1, 0, 0, 900, 700, "PureBasic Window", #PB_Window_SystemMenu|#PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
   
   ButtonGadget(#button1,  30, WindowHeight(1) - 50, 100, 30, "Message 1")
   ButtonGadget(#button2, 150, WindowHeight(1) - 50, 100, 30, "Message 2")
   ButtonGadget(#button3, 280, WindowHeight(1) - 50, 100, 30, "Message 3")   
   
   Repeat
      event = WaitWindowEvent()
      
      Select event
            
         Case #PB_Event_Gadget
            
            Select EventGadget()
               Case #button1 
                  
                  MessageBox(#msgbox1, "Test", "Gewicht,100,2|Produkt,100|Preis,80,1", 300)
                  
                  MessageBoxAddText("10 kg|Kohlen|12,50", $272780)  
                  MessageBoxAddText("10 kg|Briketts|122,50", $272780)                   
                  
               Case #button2 
                  
                  MessageBox(#msgbox2, "Jahresliste" , "2022,120,1|Monat,80,2|KM,90, 1")
                  
                  summe = 0
                  For j = 1 To 12
                     km = Random(1000, 5)
                     summe + km                                ;bei AddText auch #LF$, wie AddGadgetItem()
                     MessageBoxAddText(#LF$ + j + #LF$ + km )  ;ist der 1.Wert in der Kette ein String
                  Next                                         ;braucht man bei Variablen kein Str()  
                  MessageBoxAddText("|Summe|" + summe, #Red )  ;macht aber nur bei Ganzzahlen Sinn
                  
               Case #button3 
                  
                  head$ = "2022,140,2|Produkt,100|Preis,70,1"
                  
                  MessageBox(#msgbox3, "Info", head$)              
                  
                  MessageBoxButton("Action 1|Action 2|3|4|5|6")
                  
                  For j = 1 To 5
                     MessageBoxAddText(Str(j) + " kg|Äpfel|12,50", $272780)   
                     MessageBoxAddText("" + j + " kg|Birnen|112,50", $996813)   
                  Next
                  
                  MessageBoxAddText("10 kg" + #LF$ + "Äpfel" + #LF$ + "112,50", $1515FF)  
                  
                  
                  ;Rückgabe von Messageboxen
                  
               Case #msgbox1
                  Select EventType()
                     Case #PB_EventType_CloseItem: Debug "close " + EventGadget()  
                  EndSelect
                  
               Case #msgbox3

                  Select EventType()
                     Case 101: Debug "msgboxnr " + EventGadget() + " Button 1"
                     Case 102: Debug "msgboxnr " + EventGadget() + " Button 2"
                     Case 103: Debug "msgboxnr " + EventGadget() + " Button 3"
                     Case 104: Debug "msgboxnr " + EventGadget() + " Button 4"
                     Case 105: Debug "msgboxnr " + EventGadget() + " Button 5"
                     Case #PB_EventType_CloseItem: Debug "close " + EventGadget()  
                  EndSelect
                  
                  If EventType() = 101 ;MsgBox auslesen wenn Button 1 gedrückt
                     Debug GetGadgetItemText(#msgbox3, 1, 1)
                     Debug GetGadgetItemText(#msgbox3, 1, 2)
                     Debug GetGadgetItemText(#msgbox3, 1, 3)                     
                  EndIf

            EndSelect
            
      EndSelect
   Until Event = #PB_Event_CloseWindow
   
CompilerEndIf
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
ccode_new
Beiträge: 1214
Registriert: 27.11.2016 18:13
Wohnort: Erzgebirge

Re: meine neue MessageBox

Beitrag von ccode_new »

Schick! :allright:
Betriebssysteme: div. Windows, Linux, Unix - Systeme

no Keyboard, press any key
no mouse, you need a cat
Antworten