Solved

Transparency with a StringGrid

Posted on 2008-06-13
15
1,377 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
  • 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now