Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

transparent text

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
gamao
Asked:
gamao
  • 9
  • 8
  • 6
  • +5
1 Solution
 
MarineCommented:
You can use ForeColor of label to make it a different font color .
0
 
Brendt HessSenior DBACommented:
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
 
gamaoAuthor Commented:
Adjusted points from 50 to 100
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
gamaoAuthor Commented:
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
 
gamaoAuthor Commented:
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
 
mcriderCommented:
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
 
AzraSoundCommented:
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
 
ArkCommented:
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
 
KDivadCommented:
Public Const GWL_EXSTYLE = (-20)
0
 
AzraSoundCommented:
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
 
ArkCommented:
>Public Const GWL_EXSTYLE = (-20)
Don't work with TextBox/RTB
Cheers
0
 
mcriderCommented:
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
 
AzraSoundCommented:
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
 
mcriderCommented:
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
 
ArkCommented:
mcirider,
>Using a form will cause problems if the user moves the main form around...

No problem -
SetParent frmTransparent.hWnd, frmMain.hWnd
Cheers
0
 
mcriderCommented:
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
 
ArkCommented:
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
 
mcriderCommented:
Meanwhile, back on the farm... gamao, you still there?? ;-)


Cheers!®©
0
 
gamaoAuthor Commented:
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
 
ArkCommented:
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
 
gamaoAuthor Commented:
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
 
gamaoAuthor Commented:
Ark
Is this technique applicable to Richtextboxes?
Could you give me some directions on this?
0
 
ArkCommented:
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
 
KDivadCommented:
<<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
 
gamaoAuthor Commented:
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
 
AzraSoundCommented:
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
 
abautistatCommented:
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
 
gamaoAuthor Commented:
abautistat ,
I have rejected that choice because I can not use different colors for different words
0
 
mcriderCommented:
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
 
gamaoAuthor Commented:
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
 
ArkCommented:
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
 
ArkCommented:
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

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!

  • 9
  • 8
  • 6
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now