Just use TStringGrid.OnDrawCell event and draw a filled rectangle
That is the way I prefer to do it. Though, I find an owner-drawn
TListView in
vsReport mode tends to look better (ie, more modern) than an owner-drawn
TStringGrid, but that is just my opinion.
So I tried to implement this last night. And I ran into some issues. It was actually drawing over every item in the Stringgrid... And turning the top row green. Lol... So while I thought your solution was perfect it needs some work. I'm gonna get back on later and put together a quick demo app showing what I did and maybe you can take a look and see where I went wrong. Or maybe I will figure it out putting a demo app together.
You probably didn't take the
aCol/
aRow parameters into account correctly so you would draw only in specific cells that need a progress bar drawn in them, while skipping cells that don't.
I am just wondering why to put it in the ondraw event.
Doing so uses fewer system resources, and keeps the progress bar tied to the cell it belongs to. Using a
TProgressBar like wp described requires a dedicated
HWND per
TProgressBar, and if the
TStringGrid is scrolled then the
TProgressBar has to be moved accordingly, etc.
Wouldn't I need to have a procedure of my own telling it to draw in cell xy...
You could, if you want to. For instance, if you have a 2D array of progress values, you can use the cell indexes provided by the
OnDrawCell event to index into the array, and then draw progress only for those cells that have progress to display. Such a function can update the appropriate array element, then
InvalidateCell() the corresponding cell, so the next
OnDrawCell event for that cell will draw/skip an updated progress bar.
I was not able.to grasp where the trect properties are coming from...
The provided
TRect is the rectangle of the cell that is currently being drawn in the grid's client area. In the
OnDrawCell event, you can do whatever drawing you want within the bounds of the provided rectangle.
I tried a few different things before calling the ondraw event
You are not supposed to cell the event yourself. It is called automatically while the grid is being painted onscreen.