Transparency of Picture

Hi,

I’m trying to draw a rectangle and put a string there in the Cell of the ListBox.
With below code, I can draw it.

Open event

  Listbox1.AddRow str(0)
  
  dim p as new Picture(200,50,32)
  
  p.Graphics.ForeColor = &C92B92C
  p.Graphics.FillRect(0,0,200,50)
  p.Transparent=1
  
  Listbox1.CellTag(0,0) = p
  Listbox1.Cell(0,0) = "Service Normal"

CellTextPaint

  dim p as Picture = me.CellTag( row,column )
  g.DrawPicture p,0,0
  g.DrawString me.cell(row,column),x,y
  
  Return true

Question:

  1. In fact, there is already a row data in the Cell. However, even though I tried to use ’ p.Transparent=1’, I don’t see the string in the rectangle so that’s why I should use ‘g.DrawString’.
    Is it expected?
  2. With g.DrawString, the String starts from the first pixel of the Cell. I want to put the String in the center of the Cell like AlignCenter. How can I do that?

When you ‘return true’ in the celltextpaint, you tell Xojo that you have handled the cell text.
So the normal cell text is not added after the rectangle image is drawn for you.

To set the text in the middle of the cell, you need to know how wide the cell is, and how wide the string is.

The string width is obtained like this:

[code]dim s as string
dim gw as integer
s = me.cell(row,column)

gw = g.stringwidth(s)[/code]

And to centre it, you need to start at HALF the difference between the widths

g.drawstring s, (g.width - gw) /2, 4 //a guess at the top position

Thanks for the clear explanation and code.

It works great!