[Sloved] Help with getting a image to fit onto a button

Post your Gambas programming questions here.
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post 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?
User avatar
BruceSteers
Posts: 1586
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

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

Post 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
If at first you don't succeed , try doing something differently.
BruceS
AndyGable
Posts: 363
Joined: Wednesday 2nd December 2020 12:11am
Location: Northampton, England
Contact:

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

Post 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
Post Reply