Transparency with a StringGrid

What I'm looking to do is create a transparent StringGrid on a basic form. I found an example of pretty much exactly what I'd like to do at: , but that example is using Delphi, and I'm having trouble with the translation to C++.

I'm using the BCB 6.0 IDE.
Who is Participating?
George TokasConnect With a Mentor Commented:
I just saw the example in Delphi....
Translating to BCB with TStringGrid:

void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol,
      int ARow, TRect &Rect, TGridDrawState State)
        AnsiString Text;
        TRect   Rct;
        //Text = Field.AsString;
        Rct   = Rect;
        StringGrid1->Canvas->Brush->Color = clWhite;
        StringGrid1->Canvas->FillRect (Rct);
         Rct.right - Rct.left,
         Rct.bottom -,
         Rct.left + StringGrid1->Left,// + Panel1->Left,
         Rct.Top + StringGrid1->Top,// + Panel1->Top,
        ::SetBkMode(StringGrid1->Canvas->Handle, TRANSPARENT);
        Rct.Top += 2;
        Rct.Left += 2;
        StringGrid1->Canvas->Font->Color = clBlack;

Open in new window

The example you found is that of drawing an image in the background of a list view, this isn't "true" transparency, speaking of which I'm not sure you can achieve it easily. You can set a background image for the list view, though. I'm not a Delphi expert, but you would send a LVM_SETBKIMAGE message to the list view if you were using pure Win32 API. I'm sure there is a method in TListView that wraps this message.
George TokasCommented:
BCB6 in main form properties - in Object inspector - has Transparent and Transparent Color properties.
Setting Transparent to true and Transparent color to a color different than the default (because there is a bug there) whatever control or anything else contained in that form with that color appears transparent ar runtime.

George Tokas.
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

George TokasCommented:
In example:
Start a project with just an empty form.
Set the color of the form to clBlack.
Set Transparent to true and Transparent Color to clBlack.

Run the project.
You will see an empty frame. Your window (form) IS transparent.

There are also interesting effects with the alpha property.

George Tokas.
gbwiscAuthor Commented:
That's close to what I'm looking for... but not exactly. The issue with the transparent color solution as posted above is that it IS transparent, but all the way through to the Desktop. I'm looking for something that is transparent through to the form, or to an image, or whatever is "behind" it.

I'd also be interested in the solution to the first comment, which would be to give the StringGrid a background image... either one would work.
George TokasCommented:
The quickest way I can think is instead of the grid to type your strings as labels over a TImage and add a scroll bar in case there are more to be displayed.

George Tokas.
Regarding the first comment, see this link:

Therefore your code would be:

// Assumptions:
// HWND hwnd - handle to the TListView window
// const char* imagePath - path to the background image file (JPG, BMP, GIF or whatever)
lvimg.pszImage = path;

Open in new window

George TokasCommented:
I'm REALLY getting old....
I wrote an article more than one year back on about "Shaping" applications.
In the example I explained before add a TImage and have an image of a circle of red color in example in black background...
Load the image - must be bmp - and set it's transparent property to true and the transparent color to background color...
When you run the project the frame will have a red circle only...
That means you can do what you want...

George Tokas.
gbwiscAuthor Commented:
gtokas: I understand where you're at as far as the form, but I'm failing with the integration of the StringGrid. I've attached screenshots to illustrate the designtime/runtime of what I'm seeing. To keep this as simple as possible, in BCB right now, I just have three elements:
1) TForm
2) TImage
3) TStringGrid

The elements I have changed for each form are:
1) TForm - Color to clBlack, Transparent Color = True, TransparentColorValue = clBlack
2) TImage - Picture to a BMP (red circle on a black background), Transparent = True
3) TStringGrid - Color to clBlack

As you can see though from the screenshots, the transparency is still going all the way through to the desktop (which in this case was my open browser), instead of "stopping" at the red circle. What I would like to see in the second screenshot is the rest of the red circle behind the StringGrid.
George TokasCommented:
Set the parent of TSTringGrid to TImage and try again.
George Tokas.
gbwiscAuthor Commented:
This is exactly what I was looking for! Thanks for the work on it!
George TokasCommented:
I thought and still think that BCB versions > 6 can do the same without code as I proposed before my last post...
Usually I hate coding when there is other way, in fact re-invent the wheel...
Anyway as you ca see translation from Delphi to BCB isn't THAT hard in most of the cases...
And OF COURSE you are welcome...:-)

George Tokas.
George TokasCommented:
Versions > 5. My mistake...
George TokasCommented:
2 forms.
One transparent and one with the image NOT transparent...
My oppinion is that it want less code than those few lines I translated...
By the way thanks for giving me the idea for my next article in bcbjournal...

George Tokas...
i never could get this to work. I am ignoring it for the moment as it is not absolutely critical, but I will need to come back to it...
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.