Solved

Transparency with a StringGrid

Posted on 2008-06-13
15
1,485 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

617 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