Transparent Forms


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 1 of 1

Thread: Transparent Forms

  1. #1
    Join Date
    Jul 2005
    Posts
    8

    Transparent Forms

    I want to add a picture to the actual form and then make a certain color from the picture transparent. I have got code to make this work but it will only work with .bmp added to form, but obviously makes the final program huge in size

    Please see attached picture.
    Example

    It makes color "0, 0, 255" transparent, but i need it to do the same thing with a .jpg image..

    Please help.

    Kal


    The code i have is:

    "Form code"
    Code:
    Dim g_nTransparency As Integer
        Dim color As Long
        Dim flag As Byte
    
    Private Sub Form_load()
        On Error GoTo ErrorRtn
        color = RGB(0, 0, 255)
        flag = 0
        If chkcolor.Value = 1 Then flag = flag Or LWA_COLORKEY
        If chkAlpha.Value = 1 Then flag = flag Or LWA_ALPHA
                    
        'value between 0 and 255
        g_nTransparency = txtTransparency.Text
        If g_nTransparency < 0 Then g_nTransparency = 0
        If g_nTransparency > 255 Then g_nTransparency = 255
        Me.Show
        SetTranslucent2 Me.hWnd, color, g_nTransparency, flag
        
        Exit Sub
    ErrorRtn:
        MsgBox Err.Description & " Source : " & Err.Source
        
        
    'initialize
        txtTransparency.Text = 150
        g_nTransparency = 150
        Me.Show
        
    End Sub

    "API Module"
    Code:
    Option Explicit
    Public Const LWA_COLORKEY = 1
    Public Const LWA_ALPHA = 2
    Public Const LWA_BOTH = 3
    Public Const WS_EX_LAYERED = &H80000
    Public Const GWL_EXSTYLE = -20
    Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal color As Long, ByVal X As Byte, ByVal alpha As Long) As Boolean
    Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long

    "Misc Module"
    Code:
    Option Explicit
    
    Sub SetTranslucent(ThehWnd As Long, nTrans As Integer)
        On Error GoTo ErrorRtn
    
        'SetWindowLong and SetLayeredWindowAttributes are API functions, see MSDN for details
        Dim attrib As Long
        attrib = GetWindowLong(ThehWnd, GWL_EXSTYLE)
        SetWindowLong ThehWnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED
        SetLayeredWindowAttributes ThehWnd, RGB(255, 255, 0), nTrans, LWA_ALPHA
        Exit Sub
    ErrorRtn:
        MsgBox Err.Description & " Source : " & Err.Source
        
    End Sub
    
    
    Sub SetTranslucent2(ThehWnd As Long, color As Long, nTrans As Integer, flag As Byte)
        On Error GoTo ErrorRtn
    
        'SetWindowLong and SetLayeredWindowAttributes are API functions, see MSDN for details
        Dim attrib As Long
        attrib = GetWindowLong(ThehWnd, GWL_EXSTYLE)
        SetWindowLong ThehWnd, GWL_EXSTYLE, attrib Or WS_EX_LAYERED
        'anything with color value color will completely disappear if flag = 1 or flag = 3
        SetLayeredWindowAttributes ThehWnd, color, nTrans, flag
        Exit Sub
    ErrorRtn:
        MsgBox Err.Description & " Source : " & Err.Source
        
    End Sub
    I am sure this code could be simplified also, this code requires 2 checkboxes and a textbox with the transparency value, in order to work.
    Last edited by kal2004; 07-26-2005 at 04:19 PM.

Similar Threads

  1. Replies: 2
    Last Post: 02-21-2002, 06:28 AM
  2. VB Transparent Forms
    By Stacey Hamilton in forum VB Classic
    Replies: 4
    Last Post: 11-09-2000, 01:38 AM
  3. VB Transparent Forms
    By Stacey Hamilton in forum VB Classic
    Replies: 0
    Last Post: 11-08-2000, 03:30 PM
  4. Transparent forms
    By Alf Eliassen Bergum in forum authorevents.patrick
    Replies: 3
    Last Post: 09-06-2000, 06:06 PM
  5. Problem in Access 97 Using Forms and Sub Forms
    By Barend Esterhuizen in forum VB Classic
    Replies: 1
    Last Post: 06-16-2000, 05:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center