Microsoft Most Valuable Professional

Chris Pietschmann

An MVP From Wisconsin



VB.NET: Make a Form semi-transparent when moving and resizing it

Here's a little VB.NET code snippet that I wrote to make a Form semi-transparent when moving and resizing it. I think this is a neat little effect to add to an application.

 

Public Class frmTransparentMoveResize

Inherits System.Windows.Forms.Form

Private _OpacityResize As Double = 0.5

Private _OpacityMove As Double = 0.5

Private _OpacityOriginal As Double

Private Const WM_NCLBUTTONDOWN As Long = &HA1

Private Const WM_NCLBUTTONUP As Long = &HA0

Private Const WM_MOVING As Long = &H216

Private Const WM_SIZE As Long = &H5

Protected Overrides Sub DefWndProc(ByRef m As System.Windows.Forms.Message)

Static LButtonDown As Boolean

'Check the state of the Left Mouse Button

If CLng(m.Msg) = WM_NCLBUTTONDOWN Then

'set LButtonDown to True is Left Mouse Button is down

LButtonDown = True

ElseIf CLng(m.Msg) = WM_NCLBUTTONUP Then

'set LButtonDown to False is Left Mouse Button is not down

LButtonDown = False

End If

If LButtonDown Then

If CLng(m.Msg) = WM_MOVING Then

'Set the forms opacity to 50% if user is draging the window

If Me.Opacity <> _OpacityMove Then

_OpacityOriginal = Me.Opacity

Me.Opacity = _OpacityMove

End If

ElseIf CLng(m.Msg) = WM_SIZE Then

'Set the forms opacity to 50% if user is resizing the window

If Me.Opacity <> _OpacityResize Then

_OpacityOriginal = Me.Opacity

Me.Opacity = _OpacityResize

End If

End If

ElseIf Not LButtonDown Then

If Me.Opacity <> _OpacityOriginal Then Me.Opacity = _OpacityOriginal

End If

MyBase.DefWndProc(m)

End Sub

Public Property OpacityMove() As Double

Get

Return _OpacityMove

End Get

Set(ByVal Value As Double)

_OpacityMove = Value

End Set

End Property

Public Property OpacityResize() As Double

Get

Return _OpacityResize

End Get

Set(ByVal Value As Double)

_OpacityResize = Value

End Set

End Property

End Class

 

Currently rated 4.0 by 2 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: General
Posted by crpietschmann on Thursday, February 17, 2005 12:10 AM
Permalink | Comments (2) | Post RSSRSS comment feed

Related posts

Comments

paul

Tuesday, October 10, 2006 3:20 PM

paul

how to use this snippet you posted here, i need to put the said effect in my form do i need to inherit the class? help pls, thanks...

Chris Pietschmann

Tuesday, October 10, 2006 3:37 PM

Chris Pietschmann

You have two options: 1) You can put this code in your form. 2) or, you can inherit this class. The second option is the preferred option since you can then reuse it across multiple forms across your app.

This was also written in .NET 1.x, and as I remember it might not work perfectly in .NET 2.0+.

Comments are closed

About the author

I'm Chris Pietschmann, go to the About Me page to learn more about me.

Search

Sponsors

Web.Maps.VE - ASP.NET AJAX Virtual Earth Mapping Server Control

Recent comments

Disclaimer


This work is licensed under a Creative Commons Attribution 3.0 United States License, unless explicitly stated otherwise within the posted content.
© Copyright 2004 - 2008 Chris Pietschmann