Background changer for VM's

Share your advanced PureBasic knowledge/code with the community.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Background changer for VM's

Post by blueznl »

Here's a little bit of (ugly :-)) code that changes your wallpaper temporary. I wrote it whilst playing with VM's. Added to the startup sequence it helps you identify different VM's at a glance.

Run it from the prompt and it will give you all options.

Code: Select all

; wallx v0.03x
; pb4.40b1
;
#program_name = "WallX"
#program_version = "v0.04x"
#program_copyright = "c2009 EJN"
;
InitNetwork()
UseJPEGImageDecoder()
wall_filename.s = "c:\wallx.bmp"
fontname.s = "Arial"
fontsize.i = 64
;
If CountProgramParameters() = 0
  ;
  OpenConsole()
  PrintN("")
  PrintN("wallx "+#program_version+" "+#program_copyright+":")
  PrintN("  replace wallpaper with a generated image / text")
  PrintN("")
  PrintN("command line parameters (interpreted from left to right):")
  PrintN("  red                  - red background")
  PrintN("  blue                 - blue background")
  PrintN("  green                - green background")
  PrintN("  black                - black background")
  PrintN("  white                - white background")
  PrintN("  grey                 - grey background")
  PrintN("  base                 - diagonal yellow / black stripes")
  PrintN("  traffic              - diagonal red / white stripes")
  PrintN("  fade                 - fade to black, top to bottom")
  PrintN("  dots                 - superimpose black dots (25%)")
  PrintN("  font <name> <size>   - specify font name And size")
  PrintN("  big <text>           - large white text")
  PrintN("  text <text|text>     - draw one or more text lines")
  PrintN("")
  PrintN("embedded text elements:")
  PrintN("  ~ip~                 - ip number")
  PrintN("  ~host~               - host name")
  PrintN("")
  PrintN("all commands in lowercase, enclose spaces in doublequotes")
  PrintN("")
  PrintN("examples:")
  PrintN("  wallx red big 1")
  PrintN("  wallx base text "+#DQUOTE$+"WINDOWS XP UK SP3 | BASE IMAGE - DO NOT MODIFY"+#DQUOTE$)
  PrintN("  wallx blue big "+#DQUOTE$+".21"+#DQUOTE$+" dots text "+#DQUOTE$+"~host~ ~ip~"+#DQUOTE$+" fade")
  PrintN("  wallx traffic fade big 1 dots text "+#DQUOTE$+"MYSQL SERVER | 192.168.0.1"+#DQUOTE$)
  PrintN("  wallx blue fade font Arial 100 text "+#DQUOTE$+"CLIENT ONE | ~host~ - ~ip~"+#DQUOTE$)
  PrintN("")
  CloseConsole()
  ;
Else
  ;
  ExamineDesktops()
  width = DesktopWidth(0)
  height = DesktopHeight(0)
  ;
  CreateImage(1,width,height,32)
  Repeat
    command.s = ProgramParameter()
    Select command
    Case "red"
      ;
      ; 50% red
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,RGB($7F,$00,$00))
      StopDrawing()
      ;
    Case "green"
      ;
      ; 50% green
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,RGB($00,$7F,$00))
      StopDrawing()
      ;
    Case "blue"
      ;
      ; 50% blue
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,RGB($00,$00,$7F))
      StopDrawing()
      ;
    Case "black"
      ;
      ; full black
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,0)
      StopDrawing()
      ;
    Case "white"
      ;
      ; 100% white
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,$FFFFFF)
      StopDrawing()
      ;
    Case "grey"
      ;
      ; 25% grey
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,$3F3F3F)
      StopDrawing()
      ;
    Case "base"
      ;
      ; yellow diagonals on black background
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,0)
        x = 0-width
        Repeat
          x = x+width/10
          For xx=x To x+width/21
            LineXY(xx,0,xx+width,width,RGB($FF,$FF,$00))
          Next xx
        Until x > width
      StopDrawing()
      ;
    Case "traffic"
      ;
      ; red diagonals on a white background
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,$FFFFFF)
        x = 0-width
        Repeat
          x = x+width/10
          For xx=x To x+width/20
            LineXY(xx,0,xx+width,width,$0000FF)
          Next xx
        Until x > width
      StopDrawing()
      ;
    Case "dots"
      ;
      ; small pattern of black dots (25%)
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        x = 0
        While x < width
          x = x+4
          y = 0
          While y < height
            Box(x,y,2,2,0)
            y = y+4
          Wend
        Wend
      StopDrawing()
      ;
    Case "fade"
      ;
      ; fade to black top to bottom
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        For y = 0 To height-1
          LineXY(0,y,width,y,y*255/height*$1000000)
        Next y
      StopDrawing()
      ;
    Case "font"
      ;
      ; select font to use
      ;
      fontname = ProgramParameter()
      fontsize = Val(ProgramParameter())
      ;
    Case "big"
      ;
      ; big background text in white
      ; font name as specified by 'font <fontname> <fontsize>'
      ; font size hardcoded
      ; use for other fontsizes the option 'text'
      ;
      t.s = ProgramParameter()
      ExamineIPAddresses()
      t = ReplaceString(t,"~ip~",StringField(IPString(NextIPAddress()),4,"."))
      ;
      LoadFont(1,fontname,height-384,#PB_Font_Bold|#PB_Font_HighQuality)
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(1))
        DrawText(width/2-TextWidth(t)/2,height/2-TextHeight(t)/2,t,$FFFFFF)
      StopDrawing()
      FreeFont(1)
      ;
    Case "text"
      ;
      ; foreground text in 100% with black shadow
      ; font name and size specified by 'font <fontname> <fontsize>'
      ;
      ; add embedded elements
      ; 
      t.s = ProgramParameter()
      ExamineIPAddresses()
      t = ReplaceString(t,"~ip~",IPString(NextIPAddress()))
      t = ReplaceString(t,"~host~",Hostname())
      ;
      ; display
      ;
      LoadFont(2,fontname,fontsize,#PB_Font_Bold|#PB_Font_HighQuality)
      LoadFont(3,fontname,fontsize/2,#PB_Font_Bold|#PB_Font_HighQuality)
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Transparent)
        n = 1
        Repeat
          tt.s = Trim(StringField(t,n,"|"))
          If tt.s = ""
          ElseIf n = 1
            DrawingFont(FontID(2))
            DrawText(width/2-TextWidth(tt)/2+2,height/2-fontsize/2+2,tt,$000000)
            DrawText(width/2-TextWidth(tt)/2,height/2-fontsize/2,tt,$FFFFFF)
          Else
            DrawingFont(FontID(3))
            DrawText(width/2-TextWidth(tt)/2+2,height/2-fontsize*0.4+fontsize*0.8*n+2,tt,$000000)
            DrawText(width/2-TextWidth(tt)/2,height/2-fontsize*0.4+fontsize*0.8*n,tt,$FFFFFF)
          EndIf
          n = n+1
        Until tt.s = ""
      StopDrawing()
      FreeFont(2)
      FreeFont(3)
      ;
    EndSelect
  Until command = ""
  ;
  SaveImage(1,wall_filename)
  SystemParametersInfo_(#SPI_SETDESKWALLPAPER,0,@wall_filename,#SPIF_SENDWININICHANGE)
  ;
  FreeImage(1)
EndIf
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
thyphoon
Enthusiast
Enthusiast
Posts: 356
Joined: Sat Dec 25, 2004 2:37 pm

Re: Background changer for VM's

Post by thyphoon »

blueznl wrote:Added to the startup sequence
Lolllll do you come from Amiga ?
Thanks for the Code ! :)
Fred
Administrator
Administrator
Posts: 18249
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

:lol:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

Well, actually, yes :-)

But with 'startup sequence' I was refering to the whole process of startup, ie. either place it in the 'startup' folder, or in a batchfile that executes at startup (which is what I do, so I have a little control over sequence of events etc.).

Dunno' if my old Amiga days were behind the choice of words, if so, good stuff :-)

I wonder who is worse though, those that use it, or those that recognize it :-)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

And my final iteration :-)

I've modified it a bit for the lazy amongst us... read: me :-) To get a nice effect the next simple line will do...
wallx mark

Code: Select all

; wallx v0.08x
; pb4.40b1
;
#program_name = "WallX"
#program_version = "v0.08x"
#program_copyright = "c2009 EJN"
;
InitNetwork()
UseJPEGImageDecoder()
wall_filename.s = GetTemporaryDirectory()+"wallx.bmp"
fontname.s = "Arial"
fontsize.i = 64
;
If CountProgramParameters() = 0
  ;
  OpenConsole()
  PrintN("")
  PrintN("wallx "+#program_version+" "+#program_copyright+":")
  PrintN("  replace wallpaper with a generated image / text")
  PrintN("")
  PrintN("command line parameters (interpreted from left to right):")
  PrintN("  red                  - red background")
  PrintN("  blue                 - blue background")
  PrintN("  green                - green background")
  PrintN("  black                - black background")
  PrintN("  white                - white background")
  PrintN("  grey                 - grey background")
  PrintN("  base                 - diagonal yellow / black stripes")
  PrintN("  traffic              - diagonal red / white stripes")
  PrintN("  fade                 - fade to black, top to bottom")
  PrintN("  dots                 - superimpose black dots (25%)")
  PrintN("  font <name> <size>   - specify font name And size")
  PrintN("  big <text>           - large white text")
  PrintN("  text <text|text>     - draw one or more text lines")
  PrintN("  mark <text>           - a combination of commands")
  PrintN("")
  PrintN("")
  PrintN("embedded text elements:")
  PrintN("  ~ip~                 - ip number")
  PrintN("  ~host~               - host name")
  PrintN("  ~timestamp~          - day and time at moment of execution")
  PrintN("")
  PrintN("all commands in lowercase, enclose spaces in doublequotes")
  PrintN("")
  PrintN("examples:")
  PrintN("  wallx red big 1")
  PrintN("  wallx big ~ip~ text ~timestamp~")
  PrintN("  wallx mark")
  PrintN("  wallx pink mark "+#DQUOTE$+"MYSQL SERVER"+#DQUOTE$)
  PrintN("  wallx base text "+#DQUOTE$+"WINDOWS XP UK SP3 | BASE IMAGE - DO NOT MODIFY"+#DQUOTE$)
  PrintN("  wallx blue big "+#DQUOTE$+"~ip~"+#DQUOTE$+" dots text "+#DQUOTE$+"~host~ ~ip~"+#DQUOTE$+" fade")
  PrintN("  wallx traffic fade big 1 dots text "+#DQUOTE$+"MYSQL SERVER | 192.168.0.1"+#DQUOTE$)
  PrintN("  wallx blue fade font Arial 100 text "+#DQUOTE$+"CLIENT ONE | ~host~ - ~ip~"+#DQUOTE$)
  PrintN("")
  CloseConsole()
  ;
Else
  ;
  ; store all command line parameters in a list so we can create combo's etc.
  ;
  NewList command.s()
  Repeat
    AddElement(command())
    command.s() = ProgramParameter()
    ;
    ; preprocess any combo instructions
    ;
    Select command.s()
    Case "mark"
      command.s() = "big"
      AddElement(command.s())
      command.s() = "~ip~"
      AddElement(command.s())
      command.s() = "dots"
      AddElement(command.s())
      command.s() = "text"
      AddElement(command.s())
      command.s() =  ProgramParameter()
      If command.s() <> ""
        command.s() = command.s() + "|"
      EndIf
      command.s() = command.s() + "~host~ ~ip~ "
      AddElement(command.s())
      command.s() = "fade"
    EndSelect
  Until command.s() = ""
  AddElement(command())
  command.s() = ""
  ;
  ; get desktop info and create a random background (may be overwritten later)
  ;
  ExamineDesktops()
  width = DesktopWidth(0)
  height = DesktopHeight(0)
  CreateImage(1,width,height,32)
  StartDrawing(ImageOutput(1))
    Box(0,0,width,height,RGB(Random($E0),Random($E0),Random($E0)))
  StopDrawing()
  ;
  ; replace eventual placeholders such as ~ip~
  ;
  ResetList(command.s())
  While NextElement(command.s())
    ExamineIPAddresses()
    command.s() = ReplaceString(command.s(),"~ip~",IPString(NextIPAddress()))
    command.s() = ReplaceString(command.s(),"~host~",Hostname())
    command.s() = ReplaceString(command.s(),"~timestamp~",FormatDate("%dd/%mm/%yy %hh:%ii:%ss",Date()))
  Wend
  ;
  ; go through all commands in the list
  ;
  ResetList(command.s())
  Repeat
    NextElement(command.s())
    ;
    Select command.s()
    Case "red" , "green" , "blue" , "pink" , "lime" , "black" , "white" , "grey"
      ;
      ; one colour full-screen
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Select command.s()
        Case "red"
          FrontColor(RGB($7F,$00,$00))         ; 50% red
        Case "green"
          FrontColor(RGB($00,$7F,$00))         ; 50% green
        Case "blue"
          FrontColor(RGB($00,$00,$7F))         ; 50% blue
        Case "black"
          FrontColor(0)                        ; full black
        Case "white"
          FrontColor($FFFFFF)                  ; full white
        Case "grey"
          FrontColor($3F3F3F)                  ; 25% grey
        Case "pink"
          FrontColor(RGB($E0,$20,$60))         ; an awful pink :-)
        Case "lime"
          FrontColor(RGB($A0,$F0,$00))         ; another awful colour :-)
        EndSelect
        Box(0,0,width,height)
      StopDrawing()
      ;
    Case "base"
      ;
      ; yellow diagonals on black background
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,0)
        x = 0-width
        Repeat
          x = x+width/6
          For xx=x To x+width/13
            LineXY(xx,0,xx+width,width,RGB($FF,$FF,$00))
          Next xx
        Until x > width
      StopDrawing()
      ;
    Case "traffic"
      ;
      ; red diagonals on a white background
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0,0,width,height,$FFFFFF)
        x = 0-width
        Repeat
          x = x+width/10
          For xx=x To x+width/20
            LineXY(xx,0,xx+width,width,$0000FF)
          Next xx
        Until x > width
      StopDrawing()
      ;
    Case "dots"
      ;
      ; small pattern of black dots (25%)
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Default)
        x = 0
        While x < width
          x = x+4
          y = 0
          While y < height
            Box(x,y,2,2,0)
            y = y+4
          Wend
        Wend
      StopDrawing()
      ;
    Case "fade"
      ;
      ; fade to black top to bottom
      ;
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_AlphaBlend)
        For y = 0 To height-1
          LineXY(0,y,width,y,y*255/height*$1000000)
        Next y
      StopDrawing()
      ;
    Case "font"
      ;
      ; select font to use
      ;
      NextElement(command.s())
      fontname = command.s()
      NextElement(command.s())
      fontsize = Val(command.s())
      ;
    Case "big"
      ;
      ; big background text in white
      ; font name as specified by 'font <fontname> <fontsize>'
      ; font size hardcoded
      ; use for other fontsizes the option 'text'
      ;
      NextElement(command.s())
      t.s = StringField(command.s(),4,".")
      If t.s = ""
        t.s = command.s()
      EndIf
      ;
      LoadFont(1,fontname,height-384,#PB_Font_Bold|#PB_Font_HighQuality)
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Transparent)
        DrawingFont(FontID(1))
        DrawText(width/2-TextWidth(t)/2,height/2-TextHeight(t)/2,t,$FFFFFF)
      StopDrawing()
      FreeFont(1)
      ;
    Case "text"
      ;
      ; foreground text in 100% with black shadow
      ; font name and size specified by 'font <fontname> <fontsize>'
      ;
      ; add embedded elements
      ;
      NextElement(command.s())
      t.s = command.s()
      ;
      ; display
      ;
      LoadFont(2,fontname,fontsize,#PB_Font_Bold|#PB_Font_HighQuality)
      LoadFont(3,fontname,fontsize/2,#PB_Font_Bold|#PB_Font_HighQuality)
      StartDrawing(ImageOutput(1))
        DrawingMode(#PB_2DDrawing_Transparent)
        n = 1
        Repeat
          tt.s = Trim(StringField(t,n,"|"))
          If tt.s = ""
          ElseIf n = 1
            DrawingFont(FontID(2))
            DrawText(width/2-TextWidth(tt)/2+2,height/2-fontsize/2+2,tt,$000000)
            DrawText(width/2-TextWidth(tt)/2,height/2-fontsize/2,tt,$FFFFFF)
          Else
            DrawingFont(FontID(3))
            DrawText(width/2-TextWidth(tt)/2+2,height/2-fontsize*0.4+fontsize*0.8*n+2,tt,$000000)
            DrawText(width/2-TextWidth(tt)/2,height/2-fontsize*0.4+fontsize*0.8*n,tt,$FFFFFF)
          EndIf
          n = n+1
        Until tt.s = ""
      StopDrawing()
      FreeFont(2)
      FreeFont(3)
      ;
    EndSelect
  Until command.s() = ""
  ;
  SaveImage(1,wall_filename)
  SystemParametersInfo_(#SPI_SETDESKWALLPAPER,0,@wall_filename,#SPIF_SENDWININICHANGE)
  DeleteFile(wall_filename)
  ;
  FreeImage(1)
EndIf
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply