Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
help me please, i fond this code to make the form without background color and show the background image, and this will make the image look as good as the png quality but the problem is, the form will hide all the controls but the background image :(

the code is class:

VB
Imports System.Runtime.InteropServices

Public Class rtaLayeredForm

    Private mParent As Form
    Public ReadOnly Property Parent() As Form
        Get
            Return mParent
        End Get
    End Property

    Public Sub SetImage(ByVal frm As Form, ByVal btm As Bitmap)

        Try
            mParent = frm

            Dim exStyle As Int32 = APIs.GetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE)

            APIs.SetWindowLong(frm.Handle.ToInt32, APIs.GWL_EXSTYLE, exStyle Or APIs.WS_EX_LAYERED)
            If Not (Bitmap.IsCanonicalPixelFormat(btm.PixelFormat)) Or Not (Bitmap.IsAlphaPixelFormat(btm.PixelFormat)) Then
                Throw New ApplicationException("The picture must be 32bit picture with alpha channel.")
            End If

            Dim hDC As Int32 = APIs.GetDC(0)
            Dim hBtm As Int32
            Dim memDC As Int32 = APIs.CreateCompatibleDC(hDC)
            Dim Bits As Int32

            Dim dstPos As New APIs.POINTAPI(frm.Left, frm.Top)
            Dim srcPos As New APIs.POINTAPI(0, 0)
            Dim Siz As New APIs.SizeAPI(btm.Width, btm.Height)
            Dim Opt As New APIs.BLEND_OPTIONS

            Opt.SourceConstantAlpha = 255
            Opt.AlphaFormat = 1
            Opt.BlendOp = 0
            Opt.BlendFlags = 0

            Try
                hBtm = btm.GetHbitmap(Color.Black).ToInt32
                Bits = APIs.SelectObject(memDC, hBtm)

                APIs.UpdateLayeredWindow(frm.Handle, hDC, dstPos, Siz, memDC, srcPos, 0, Opt, 2)
            Catch ex As Exception

            End Try

            If hBtm <> 0 Then
                APIs.SelectObject(memDC, Bits)
                APIs.DeleteObject(hBtm)
            End If

            APIs.ReleaseDC(0, hDC)
            APIs.DeleteDC(memDC)

        Catch ex As Exception

        End Try
    End Sub

    Private Class APIs

        Public Declare Function CreateCompatibleDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
        Public Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Int32) As Int32
        Public Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Int32, ByVal hObject As Int32) As Int32
        Public Declare Function ReleaseDC Lib "user32.dll" (ByVal hwnd As Int32, ByVal hdc As Int32) As Int32
        Public Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Int32) As Int32
        Public Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Int32) As Int32
        Public Declare Function UpdateLayeredWindow Lib "user32.dll" (ByVal hWnd As Int32, ByVal hdcDst As Int32, ByRef pptDst As POINTAPI, ByRef psize As SizeAPI, ByVal hdcSrc As Int32, ByRef pptSrc As POINTAPI, ByVal crKey As Int32, ByRef pblend As BLEND_OPTIONS, ByVal dwFlags As Int32) As Int32
        Public Declare Function GetWindowLong Lib "user32.dll" Alias "GetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32) As Int32
        Public Declare Function SetWindowLong Lib "user32.dll" Alias "SetWindowLongW" (ByVal hwnd As Int32, ByVal nIndex As Int32, ByVal dwNewLong As Int32) As Int32

        Public Const GWL_EXSTYLE As Int32 = -20
        Public Const WS_EX_LAYERED As Int32 = &H80000

        <StructLayout(LayoutKind.Sequential, Pack:=1)> _
        Public Structure BLEND_OPTIONS
            Public BlendOp As Byte
            Public BlendFlags As Byte
            Public SourceConstantAlpha As Byte
            Public AlphaFormat As Byte
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure SizeAPI
            Public cx As Int32
            Public cy As Int32

            Sub New(ByVal cx As Int32, ByVal cy As Int32)
                Me.cx = cx
                Me.cy = cy
            End Sub
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure POINTAPI
            Public x As Int32
            Public y As Int32

            Sub New(ByVal x As Int32, ByVal y As Int32)
                Me.x = x
                Me.y = y
            End Sub
        End Structure


        Public Declare Function ExtCreateRegion Lib "gdi32.dll" ( _
          ByRef lpXform As XFORM, _
          ByVal nCount As Int32, _
          ByRef lpRgnData As RGNDATA) As Int32
        <StructLayout(LayoutKind.Sequential)> _
        Public Structure XFORM
            Public eM11 As Single
            Public eM12 As Single
            Public eM21 As Single
            Public eM22 As Single
            Public eDx As Single
            Public eDy As Single
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RGNDATA
            Public rdh As RGNDATAHEADER
            Public Buffer As Byte
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RGNDATAHEADER
            Public dwSize As Int32
            Public iType As Int32
            Public nCount As Int32
            Public nRgnSize As Int32
            Public rcBound As RECT
        End Structure

        <StructLayout(LayoutKind.Sequential)> _
        Public Structure RECT
            Public Left As Int32
            Public Top As Int32
            Public Right As Int32
            Public Bottom As Int32
        End Structure


    End Class

End Class


and in the form i have to write this code in the load event:

VB
Dim Layer As New rtaLayeredForm()
       Layer.SetImage(Me, Me.BackgroundImage)



can anyone show me how to show the controls ??
Posted
Updated 4-Feb-12 4:27am
v2

1 solution

Don't copy huge amounts of code off the web that you don't understand. Handle your form's paint event, and, in it, draw the background you want, then call the base method. Your controls will be visible just fine, and it's not a hack.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900