Solved

Transparency with a StringGrid

Posted on 2008-06-13
15
1,471 Views
Last Modified: 2013-11-17
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
Comment
Question by:gbwisc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 3
  • 2
  • +1
15 Comments
 
LVL 9

Expert Comment

by:Gurudenis
ID: 21786288
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21789093
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21789102
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gbwisc
ID: 21789172
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21789244
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
 
LVL 9

Expert Comment

by:Gurudenis
ID: 21791762
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21792458
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
 

Author Comment

by:gbwisc
ID: 21795178
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21795308
Set the parent of TSTringGrid to TImage and try again.
George Tokas.
0
 
LVL 16

Accepted Solution

by:
George Tokas earned 500 total points
ID: 21795618
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
 

Author Closing Comment

by:gbwisc
ID: 31467056
This is exactly what I was looking for! Thanks for the work on it!
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21801950
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
 
LVL 16

Expert Comment

by:George Tokas
ID: 21801961
Versions > 5. My mistake...
0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21801984
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
 

Expert Comment

by:prutovitz
ID: 23217594
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

729 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question