Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Just For Fun: Transparent Notepad ;-)

Status
Not open for further replies.

CubeE101

Programmer
Nov 19, 2002
1,492
US
Have you ever wanted to make an application (such as NotePad) transparent? ;-)

I was working on a couple of projects and stumbled across this...

In one project, I was interfacing with NotePad, Grabing and setting text...

In the other I was making my application simi-transparent so you could see the application behind it that it was interfacing with...

I noticed both of the sets of API calls were dealing with hWnd's...
So I decided to see what would happen if you put the functions together ;-)

(In case you haven't noticed in any of my other post... NotePad is my usual Guinea Pig in any given general API Test)

Here is the Code Required to Make Notepad transparent...
Code:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal wClass As String, ByVal wName As String) As Long

Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long

Const LWA_ALPHA = &H2&
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000

Sub SetTransparent(TargetClass As String, Opacity As Byte)
  Dim TargetApp As Long, dwExStyle As Long
  TargetApp = FindWindow(TargetClass, vbNullString)
  dwExStyle = GetWindowLong(TargetApp, GWL_EXSTYLE) Or WS_EX_LAYERED
  SetWindowLong TargetApp, GWL_EXSTYLE, dwExStyle
  SetLayeredWindowAttributes TargetApp, 0, Opacity, LWA_ALPHA
End Sub

Sub SetNormal(TargetClass As String)
  Dim TargetApp As Long, dwExStyle As Long
  TargetApp = FindWindow(TargetClass, vbNullString)
  SetLayeredWindowAttributes TargetApp, 0, 255, LWA_ALPHA
  dwExStyle = GetWindowLong(TargetApp, GWL_EXSTYLE) Xor WS_EX_LAYERED
  SetWindowLong TargetApp, GWL_EXSTYLE, dwExStyle
End Sub

And this is an example of how to use it...

Code:
[b]  Private Sub Command1_Click()
    SetTransparent "NotePad", 192
  End Sub

  Private Sub Command2_Click()
    SetNormal "NotePad"
  End Sub[/b]

This will set the transparency of the last active window in the specified class that you clicked on...
So if you have 5 NotePad windows open, Click one and press the button to make it transparent...

Code:
Note: Opacity is a byte value of 0 to 255...
      0 is invisible, 255 is 100% opaque
      63 is 25% visible
      127 is 50% visible
      191 is 75% visible

This should work for any application, if you have the class name of the app...

For Example:

Use these for the Windows Calculator
SetTransparent "SciCalc", 192
SetNormal "SciCalc"


Use these for the Visual Basic Editor ;-)
SetTransparent "wndclass_desked_gsk", 192
SetNormal "wndclass_desked_gsk"


If you have VC++ and/or Visual Studio (ver 5 or 6) you can use Spy++ to get the names of other Window classes to use this on...

Have Fun ;-)
-and-
M E R R Y C H R I S T M A S

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Any minute now you're going to remember thread222-444672, and start producing transparent Notepad windows with holes in them... ;-)
 
The thought did cross my mind ;-)

MERRY CHRISTMAS

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Pretty cool stuff....

***You can't change your past, but you can change your future***
 
dear cubeE101,

woh! that is cool man! do you have any code that could make a window inactive all the time, even though it is clicked by the user? something similar to the on screen keyboard? thanks
 
C4037,
I dunno...
I was looking for the same thing rescently...

I assume you are referring to something like XP's on screen keyboard... where when you press a key, the focus stays on the target app... (and Not the On Screen KB)

If you find anything, please post it hear (or at least a link) and I will do the same.

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
CubeE101, You code was great to work with and found that it worked as stated. A Question(S) and comment if you will be so kind.
I am a rank amerture and having used your code sucessfully I must ask: Would you either explain in 'basic' (English) what your functions do, where one (me) can learn learn about these 'things'.
The comment(s): After trying your code I modified it by:
1. Dim Opacity as byte
2. Adding slider1 to control Opacity
3. in both the click and change events for Slider1 added this code
Opacity = Slider1.Value
SetTransparent "NotePad", Opacity
4. deleted Cmd1 and Cmd2
5. in Form1 unload I added
SetNormal "NotePad"
6. Form1 load added
shell "notepad.exe"
Thanks for the tip and in anticipation of the answer to my questions

 
NotSoVisual

Start here for API calls. There is a comprehensive listing, a tutorial and several VB examples.


Also try:

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
This only works on xp right? The SetLayeredWindowAttributes API does not exist on earlier O.S's ?
 
LPates,
This should work on W2K as well as XP

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
Thanks John :)
 
Should work on W2K as well
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top