Page 2 of 2

Re: Help with getting a image to fit onto a button

Posted: Thursday 4th April 2024 11:43pm
by AndyGable
BruceSteers wrote: Saturday 27th January 2024 1:12am
Public Sub Form_Open()
  ' make Button1.Picture a picture with text below it.
  MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")
End

'' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  
  Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.

  Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  Obj.Text = ""  ' make sure Obj has not got text

  ' use Paint.class to draw the icon and text to the picture
  Paint.Begin(p)
  Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  Paint.End

  Obj.Picture = p  ' set the object picture to new pic

End

Hi @BruceSteers
Quick question about your excellent code that works a treat what do I change to make the text a size smaller?

Re: Help with getting a image to fit onto a button

Posted: Friday 5th April 2024 7:37am
by BruceSteers
AndyGable wrote: Thursday 4th April 2024 11:43pm
BruceSteers wrote: Saturday 27th January 2024 1:12am
Public Sub Form_Open()
  ' make Button1.Picture a picture with text below it.
  MakePictureText(Button1, Picture["icon:/32/alarm"], "Alarm call")
End

'' Make a picture with text below it. can be used on any object that has a .Text and a .Picture property.
Public Sub MakePictureText(Obj As Object, Pic As Picture, Text As String)
  
  Dim hIcon As Picture = Pic.Stretch(Obj.Height - Obj.Font.Height - 6)  ' stretch the icon to button height lss font height less margin.

  Dim p As Picture = New Picture(Obj.W, Obj.H, True) ' make a new picture (use transparent arg to fix qt bug)
  Obj.Text = ""  ' make sure Obj has not got text

  ' use Paint.class to draw the icon and text to the picture
  Paint.Begin(p)
  Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  Paint.End

  Obj.Picture = p  ' set the object picture to new pic

End

Hi @BruceSteers
Quick question about your excellent code that works a treat what do I change to make the text a size smaller?
You're welcome
Use Paint.Font to change the text attributes :)

  Paint.Begin(p)
  Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  Paint.Font.Size = 8
  Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  Paint.End

Re: Help with getting a image to fit onto a button

Posted: Friday 5th April 2024 12:31pm
by AndyGable
BruceSteers wrote: Friday 5th April 2024 7:37am You're welcome
Use Paint.Font to change the text attributes :)

  Paint.Begin(p)
  Paint.DrawPicture(hIcon, (Obj.W - hIcon.W) / 2, 4, hIcon.Width, hIcon.Height)
  Paint.Font.Size = 8
  Paint.DrawText(Text, 0, hIcon.Height + 3, Obj.Width, Obj.Font.Height, Align.Center)
  Paint.End
So simple I though it was going to be something super complicated :D thank you