Solved

transparent text

Posted on 2000-04-13
32
329 Views
Last Modified: 2012-05-04
I want a control that
1) can show text on a form
2) have transparent background, so video, pictures or labels can be seen through the text
3) one or more words can have different color, which can chahge dynamically (e.g. using Seltext, Selcolor)
4) enable selecting a word with dlClick

If I use textbox or richtextbox, then the bachstyle can not be transparent
If I use label, then words can not have different color, and it cannot change dynamicaly

Do you have an idea ?
I dont mind using a custom control. thanks

0
Comment
Question by:gamao
  • 9
  • 8
  • 6
  • +5
32 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2713806
You can use ForeColor of label to make it a different font color .
0
 
LVL 32

Expert Comment

by:bhess1
ID: 2713870
You might try this - I have not had a change to play with it, but it should set the backgroud of a rich text box to transparent:


Dim LVal as Long
Dim r as Long

' Get information about the specified window and the value at the specified
' offset into the extra window memory of a window.
' hWnd   - handle of window
' nIndex - offset of value to retrieve
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long) As Long
'
' Changes an attribute of the specified window and sets a value
' at the specified offset into the extra window memory of a window.
' hWnd      - handle of window
' nIndex    - offset of value to set
' dwNewLong - new value
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

LVal = GetWindowLong(MyRichText1.hwnd, GWL_EXSTYLE)
r = SetWindowLong(MyRichText1.hwnd, GWL_EXSTYLE , LVal Or WS_EX_TRANSPARENT )
0
 

Author Comment

by:gamao
ID: 2713907
Adjusted points from 50 to 100
0
 

Author Comment

by:gamao
ID: 2713908
The problem is that many words must be green and many others must be red, and that condition changes while running.
I mean that, for example, after five minutes the text will change, and in the new text many words must be green and many others must be red.
Do you think that I can manage this using labels?
If yes, then the question is finished.
If no, do you have another idea.
0
 

Author Comment

by:gamao
ID: 2714098
hello bhess1,
I tried the code that you send, but the richtextbox keeps having the white NOT TRANSPARENT bachground.
The variable GWL_EXSTYLE was empty (did not take a value)
Could you give me some more information about it?
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2714847
You can accomplish transparent text with colors by building an ActiveX control...

Here a quick 100 words or less walkthrough:

Start a new project and select "ActiveX Control" as a project type.

Set the BackStyle to Transparent.

Add a label to the control and set the BackStyle property to Transparent.

Put in public subroutines in the control that will change the caption of the label and it's color...

Make the OCX and then open a new project and load the control as you would any other ocx.

You can then bring your *new* control ontop of other controls, and only the text will show...  Get the idea??


Hope this helps!


Cheers!®©

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2714848
a label will do the trick

Private Sub Form_Load()
   Label1.BackStyle = 0 'Transparent
   Label1.ForeColor = vbRed
End Sub

Private Sub Label1_DblClick()
   Label1.ForeColor = vbYellow
End Sub
0
 
LVL 27

Expert Comment

by:Ark
ID: 2714901
Hi
Whats about using FORM? Don't thing about this? Take a look:
' For this sample you need form with border style None

Option Explicit

Private Declare Function SelectClipPath Lib "gdi32" (ByVal hdc As Long, ByVal iMode As Long) As Long
Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Private Const RGN_COPY = 5

Private Sub Form_Load()
  'Don't forget set BorderStyle property to None at design time!
  Const TXT = " Cool programm" & vbCrLf & "           from" & vbCrLf & "  Cool Company" & vbCrLf & "CopyLeft by Ark"
  Dim hRgn As Long
  Font.Name = "Times New Roman"
  Font.Bold = True
  Font.Size = 60
  Width = TextWidth(TXT)
  Height = TextHeight(TXT)
  BeginPath hdc
    CurrentX = 0
    CurrentY = 0
    Print TXT
  EndPath hdc
  hRgn = PathToRegion(hdc)
  SetWindowRgn hWnd, hRgn, False
  AutoRedraw = True
' You can even make words gradient or with a picture for example:
'  Picture = LoadPicture("c:\windows\clouds.bmp")
' Here is how to change text color
Line (1, 1)-(Width, Height / 4), vbRed, BF
Line (1, Height / 4)-(Width, Height / 2), vbBlue, BF
Line (1, Height / 2)-(Width, 3 * Height / 4), vbGreen, BF
Line (1, 3 * Height / 4)-(Width, Height), vbYellow, BF
' You can move this form at any place
  Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2
End Sub

Cheers
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2715124
Public Const GWL_EXSTYLE = (-20)
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2715198
In Projects | Components choose the Microsoft Forms 2.0 Object Library, the textbox included there supports transparent background.  The properties take some getting used to as they are a bit different from the regular textbox but it may be a possible solution.
0
 
LVL 27

Expert Comment

by:Ark
ID: 2715219
>Public Const GWL_EXSTYLE = (-20)
Don't work with TextBox/RTB
Cheers
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2717786
AzraSound,

A label will *not* do the trick...  A label is a light-weight control and can not be brought on top of a heavy control such as a picturebox or the MediaPlayer...  Try it...

The Microsoft Forms 2.0 Object Library is also not a solution... Same problem...


Ark,

Using a form will cause problems if the user moves the main form around...


I still belive the best solution is to create a transparent ActiveX control...



Cheers!®©

0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2717793
what version of vb are you using? i put a label and the Forms 2.0 textbox on top of a picture box with no problems.
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2717836
VB5 SP3 and Forms 2.0... The other problem with Forms 2.0 is that is it *HUGE* and is *NOT* redistributable.  The user must already have installed WORD, Office 97, or "Microsoft ActiveX Control Pad"...

WORD and Office 97 cost big bucks, however, "Microsoft ActiveX Control Pad" is free...

See the following Microsoft KB article:

INFO: Usage and Redistribution of FM20.DLL
http://support.microsoft.com/support/kb/articles/Q224/3/05.ASP

I still belive the best solution is to create a transparent ActiveX control...


Cheers!®©

0
 
LVL 27

Expert Comment

by:Ark
ID: 2718073
mcirider,
>Using a form will cause problems if the user moves the main form around...

No problem -
SetParent frmTransparent.hWnd, frmMain.hWnd
Cheers
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2718113
Ark,

Adding SetParent to your code will let it work, however, taking the few minutes to create an ActiveX control will make the code much more portable and allow it to be dropped into other programs easily. Also, using forms can be really sloppy... A form for each label if you want to use more than one label???

I'd still recommend building a transparent ActiveX control...


Cheers!®©




0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 27

Expert Comment

by:Ark
ID: 2718158
mcrider,
agreed, for many time using it's better to create ActiveX control. It can be not form but picturebox to start ActiveX. I only show Clipping technique for transparent text. IMHO it's a best way instead of playing with TextBox/RTB/Label transparency.
Cheers
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2718190
Meanwhile, back on the farm... gamao, you still there?? ;-)


Cheers!®©
0
 

Author Comment

by:gamao
ID: 2721275
I hope that the best solution is AzraSound's idea with the textbox included at the Microsoft Forms 2.0 Object Library. I have not tried it yet because I have a problem with the Microsoft Forms 2.0 Object Library (I can not insert an object into my form, because of a system error).
I hope that I will solve it as soon as possible and verify the transparency of that control).
I am using vb5.

If I used a label, I would have the following problems:
1)Many words must have different color from the other words.
2)I want to selecd a word by dlclick on it.

If someone have the anser to the following two questions, then the question is finished.

-----------
Ark
you suggest to use each form like a textbox, and place forms with transparent backfround over the main form? I think that it migt be a solution. I will try it.
 
Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2721363
You can use same technique ("Clipping text") for pictureboxes. Using this technique, you can limit region of the window (form or picturebox) only to text (as if cutting the text by scissors). This is "true" transparency - you can click any visible object behind text, and only when click on letters, Form_Click (or Picture_Click) event occure. BTW, lables are not transparent, they get background from parent form or control.
Cheers
0
 

Author Comment

by:gamao
ID: 2721393
mcrider
If I build an ActiveX control, how can I achieve transparency and multiple font colors at the same time?
It is simple to create a transparent form, but it will contain a richtextbox. Will the richtextbox be transparent?
Will it be transparent when I insert the ActiveX control into my project?If there was a way to achieve this, if would be the best solution.
0
 

Author Comment

by:gamao
ID: 2721400
Ark
Is this technique applicable to Richtextboxes?
Could you give me some directions on this?
0
 
LVL 27

Expert Comment

by:Ark
ID: 2721430
I'm afraid, no.
Let me explain you this technique.
First, you make a chain (path) of painting operation:
  BeginPath hdc
    CurrentX = 0
    CurrentY = 0
    Print TXT
  EndPath hdc
You can add another painting operations:
VB - Line, Circle, Pset
API - Arc, Polygon, FloodFill etc.
Print statement for form and picturebox are also painting operation.
Then using
  hRgn = PathToRegion(hdc)
you convert this path to region and assign window to this region:
  SetWindowRgn hWnd, hRgn, False
Now your window is not a rectangle, but consist from "chain" blocks. And you can work with this "non-standard" form/picturebox like with standard, for example, paint any part of this form using Line,,clr,BF with any color, make gradient background and even load picture. All changes will be visible only at clipped region.
But RichTextBox use another technique to show text, so I'm not sure you can easy make this with RTB
Cheers
 
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2721831
<<If I used a label, I would have the following problems:
1)Many words must have different color from the other words.>>
Seperate labels for each "color section"
Problem: Constant rearrangement of labels to keep up with changing colors/words.

<<2)I want to selecd a word by dlclick on it.>>
Sub Label1_DoubleClick()
Label1.ForeColor = vbWhite
Label1.BackColor = vbBlue
End Sub
Problem: Managing the "focus" to know when to set the label's colors back.

Problem: STILL can't bring a label on top of other controls.
0
 

Author Comment

by:gamao
ID: 2722081
AzraSound
I tried your idea with the textbox included at the Microsoft Forms 2.0 Object Library.
It is the best approach to what I asked.
Can I do something similar with an RTB?
(either at the Microsoft Forms 2.0 Object Library, or anywhere else, perhaps using the visual studio),
because many words must have different color from the other words.
I think that I will manage it soon.
Any suggestions?
0
 
LVL 28

Expert Comment

by:AzraSound
ID: 2723412
i'm afraid the textbox is your only option using the forms 2.0 library, or any other control for that matter, unless you created your own.
0
 

Accepted Solution

by:
abautistat earned 100 total points
ID: 2816726
You can add the Microsoft Forms 2.0 Object Library to your project. The textbox object has a backstyle property you can set to transparent.
0
 

Author Comment

by:gamao
ID: 2823844
abautistat ,
I have rejected that choice because I can not use different colors for different words
0
 
LVL 14

Expert Comment

by:mcrider
ID: 2828349
Here's something you can play with... Using API's to write directly to the form, you can make them transparent and use any color you want...

Add the following to the GENERAL DECLARATIONS section:

    Private Const OPAQUE = 2
    Private Const TRANSPARENT = 1
    Private Declare Function TextOut Lib "gdi32" Alias "TextOutA" _
        (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, _
        ByVal lpString As String, ByVal nCount As Long) As Long
    Private Declare Function SetTextColor Lib "gdi32" _
        (ByVal hdc As Long, ByVal crColor As Long) As Long
    Private Declare Function SetBkMode Lib "gdi32" _
        (ByVal hdc As Long, ByVal nBkMode As Long) As Long





Then you can call the routines like this:

    Dim lText As String
    lText = "Hello"
    SetBkMode Form1.hdc, TRANSPARENT
    SetTextColor Form1.hdc, vbBlue
    TextOut Form1.hdc, 0, 0, lText, Len(lText)



Cheers!®©
0
 

Author Comment

by:gamao
ID: 2828555
hello mcrider

It is a vey good idea.

But there are two problems with the labels:

1. Different words in the same label can not have different colors.
2. I can not select a word with dlClick

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2829685
Hi
gamao, did you try "clipping" technique with picturebox?
Take a look:
'Start new project and add one picturebox on form. Don't forgret set Picture1.index=0 at design time. Add code to form:

Option Explicit

Private Declare Function SelectClipPath Lib "gdi32" (ByVal hdc As Long, ByVal iMode As Long) As Long
Private Declare Function BeginPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function EndPath Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function PathToRegion Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long

Private Const RGN_COPY = 5


Private Sub Form_Load()
  Dim TXT(5) As String
  Dim hRgn As Long, i As Integer
  TXT(0) = "Cool programm"
  TXT(1) = "from"
  TXT(2) = "Cool company"
  TXT(3) = "Copyleft by Ark"
  Picture = LoadPicture("c:\windows\îáëàêà.bmp")
  With Picture1(0)
      .AutoRedraw = True
      .BorderStyle = 0
      .Font.Name = "Times New Roman"
      .Font.Bold = True
      .Font.Size = 30
  End With
  For i = 1 To 3
     Load Picture1(i)
  Next i
  For i = 0 To 3
    With Picture1(i)
       .Visible = True
       .Width = .TextWidth(TXT(i))
       .Height = .TextHeight(TXT(i))
       .BackColor = QBColor(i * 3 + 1)
       BeginPath .hdc
          .CurrentX = 0
          .CurrentY = 0
          Picture1(i).Print TXT(i)
       EndPath .hdc
       hRgn = PathToRegion(.hdc)
       SetWindowRgn .hWnd, hRgn, False
       .Move (Me.Width - .Width) / 2, .Height * i
       Picture1(i).Line (1, 1)-(.Width / 2, .Height), vbRed, BF
    End With
  Next i
End Sub

Private Sub Picture1_Click(Index As Integer)
   ' mentioned, that you need to click on a letter, NOT between them
   MsgBox "You've clicked a picture number " & CStr(Index + 1)
End Sub

Cheers
0
 
LVL 27

Expert Comment

by:Ark
ID: 2829688
Ooops... Sorry
Posted Picture name from local (Russian) windows. You can use any pictures or video for background. In my sample correct name should be
Picture = LoadPicture("c:\windows\clouds.bmp")
Cheers
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

708 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

15 Experts available now in Live!

Get 1:1 Help Now