Jalaj

February 5, 2007

Form With a Hole

Filed under: Application, Visual Basic, WinAPI — Jalaj @ 6:58 am

In the previous post WinAPI : Making Forms Translucent we took one way of using the SetLayeredWindowAttributes function. Let’s take here the other one.

Remember the Ad “Mint with a Hole”? Let’s code a “Form with a hole”. Create a new project and on the form place a circle filled with a particular color that we would need to use later, say Red. Put the declarations and the load event of form as given below

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" _
(ByVal hWnd As Long, ByVal crKey As Byte, ByVal bAlpha As Byte, _
    ByVal dwFlags As Long) As Long

Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_COLORKEY = &H1

Private Sub Form_Load()

    Dim lngCurStyle As Long
    Dim lngNewStyle As Long

    lngCurStyle = GetWindowLong(Form1.hWnd, GWL_EXSTYLE)
    lngNewStyle = lngCurStyle Or WS_EX_LAYERED

    SetWindowLong Form1.hWnd, GWL_EXSTYLE, lngNewStyle

    SetLayeredWindowAttributes Form1.hWnd, RGB(255, 0, 0), 0, LWA_COLORKEY

End Sub

The SetLayeredWindowAttributes function takes the second parameter a color key [ given here as RGB(255,0,0) i.e. Red ] and on passing LWA_COLORKEY as the fourth parameter the function all area on the form painted with Red colour to be removed from the form creating a hole through which the background is visible. It’s a true hole in the sense that when you click in between the hole the window which is behind it gets the focus.

formwithhole.jpg

That’s not all. Using this method you can create forms which are not usual rectangles but can be of any shape limited by your imagination.

The code below can be pasted to a text file and named with extension frm to directly inlucde in a project.

VERSION 5.00
Begin VB.Form Form1
   Caption         =   "Form1"
   ClientHeight    =   3195
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3195
   ScaleWidth      =   4680
   StartUpPosition =   3  'Windows Default
   Begin VB.Shape Shape1
      BorderStyle     =   0  'Transparent
      FillColor       =   &H000000FF&
      FillStyle       =   0  'Solid
      Height          =   1575
      Left            =   1433
      Shape           =   3  'Circle
      Top             =   810
      Width           =   1815
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False

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" _
(ByVal hWnd As Long, ByVal crKey As Byte, ByVal bAlpha As Byte, _
    ByVal dwFlags As Long) As Long

Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_COLORKEY = &H1

Private Sub Form_Load()

    Dim lngCurStyle As Long
    Dim lngNewStyle As Long

    lngCurStyle = GetWindowLong(Form1.hWnd, GWL_EXSTYLE)
    lngNewStyle = lngCurStyle Or WS_EX_LAYERED

    SetWindowLong Form1.hWnd, GWL_EXSTYLE, lngNewStyle

    SetLayeredWindowAttributes Form1.hWnd, RGB(255, 0, 0), 0, LWA_COLORKEY

End Sub

1 Comment »

  1. [...] Form With a Hole [...]

    Pingback by Just Looking Back « Jalaj — May 28, 2007 @ 6:24 am

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.