Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1560
  • Last Modified:

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: http://www.shagrouni.com/english/software/transgrid.html , but that example is using Delphi, and I'm having trouble with the translation to C++.

I'm using the BCB 6.0 IDE.
0
gbwisc
Asked:
gbwisc
  • 9
  • 3
  • 2
  • +1
1 Solution
 
GurudenisCommented:
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.
0
 
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.
0
 
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.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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.
0
 
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.
0
 
GurudenisCommented:
Regarding the first comment, see this link:
http://msdn.microsoft.com/en-us/library/bb775065(VS.85).aspx

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)
 
LVBKIMAGE lvimg = { LVBKIF_SOURCE_URL };
lvimg.pszImage = path;
ListView_SetBkImage(hwnd,&lvimg);

Open in new window

0
 
George TokasCommented:
I'm REALLY getting old....
I wrote an article more than one year back on http://bcbjournal.com 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.
0
 
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.
screenshot-designtime.jpg
screenshot-runtime.jpg
0
 
George TokasCommented:
Set the parent of TSTringGrid to TImage and try again.
George Tokas.
0
 
George TokasCommented:
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);
        BitBlt(StringGrid1->Canvas->Handle,
         Rct.left,
         Rct.top,
         Rct.right - Rct.left,
         Rct.bottom - Rct.top,
         Image1->Canvas->Handle,
         Rct.left + StringGrid1->Left,// + Panel1->Left,
         Rct.Top + StringGrid1->Top,// + Panel1->Top,
         SRCCOPY);
 
        ::SetBkMode(StringGrid1->Canvas->Handle, TRANSPARENT);
        Rct.Top += 2;
        Rct.Left += 2;
        StringGrid1->Canvas->Font->Color = clBlack;
 
        DrawTextEx(StringGrid1->Canvas->Handle,
             Text.c_str(),
             Text.Length(),
             &Rct,
             DT_WORDBREAK,
             NULL);
        
 
}

Open in new window

0
 
gbwiscAuthor Commented:
This is exactly what I was looking for! Thanks for the work on it!
0
 
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.
0
 
George TokasCommented:
Versions > 5. My mistake...
0
 
George TokasCommented:
ONE MORE IDEA:
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...
0
 
prutovitzCommented:
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...
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 9
  • 3
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now