mercoledì 28 aprile 2010

Creare form trasparenti, semitrasparenti e sempre in primo piano

Per chi ancora usa, come me, il buon vecchio Visual Basic 6, in questo breve tutorial voglio illustrare alcuni metodi per rendere i nostri piccoli progetti più carini con l'ausilio delle API di Windows.
Renderemo il form trasparente, semitrasparente o sempre in primo piano.
Ovviamente i metodi si possono convertire per i vb.net (visual basic 2005 / 2008), basta aprire il progetto realizzato inVisualbasic6 con l'editor dotnet e convertirlo.

Form trasparente.

Creare un nuovo progetto con un form ed un module.

Nel module inserire il seguente codice:

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
Public Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Long, ByVal dwFlags _
As Long) As Long
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Const LWA_COLORKEY = &H1&

Public Sub Trasparent_Window(hwnd As Long, BackColor As Integer)
'Imposta il Form transparente.
Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong(hwnd, _
GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(hwnd, BackColor, 0, LWA_COLORKEY)
End Sub

Nel Form_load inserire il seguente codice:

BackColor = RGB(127, 127, 0)
Trasparent_Window Me.hwnd, BackColor

Form semitrasparente.

Creare un nuovo progetto con un form ed un module.

Nel module inserire il seguente codice:

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
Public Declare Function SetLayeredWindowAttributes Lib "user32" _
(ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Long, ByVal dwFlags _
As Long) As Long
Public Const LWA_ALPHA = &H2&
Public Const GWL_EXSTYLE = (-20)
Public Const WS_EX_LAYERED = &H80000
Public Sub Fantasmizza_Window(hwnd As Long, bytOpacity)
'Form Semitrasparente
Dim buf As String * 1024
Call SetWindowLong(hwnd, GWL_EXSTYLE, GetWindowLong _
(hwnd, GWL_EXSTYLE) Or WS_EX_LAYERED)
Call SetLayeredWindowAttributes(hwnd, 0, bytOpacity, LWA_ALPHA)
End Sub

Nel Form_load inserire il seguente codice:

Fantasmizza_Window Me.hwnd, 200

Variando il valore 200 si varia l’opacità del form.

Form sempre in primo piano.

Nelle dichiarazioni generale del Form inserire il seguente codice:

Private Declare Function SetWindowPos Lib "user32.dll" (ByVal hWnd As Long, _
ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, _
ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Const HWND_TOPMOST As Long = -1
Private Const SWP_NOMOVE As Long = &H2
Private Const SWP_NOSIZE As Long = &H1

Nel Form_load inserire il seguente codice:

SetWindowPos Form1.hWnd, HWND_TOPMOST, 0, 0, 0, 0, _
SWP_NOMOVE + SWP_NOSIZE

‘e se vogliamo metterlo al centro dello schermo
Dim X As Single
Dim Y As Single
X = (Screen.Width - Me.Width) / 2
Y = (Screen.Height - Me.Height) / 2
Me.Move X, Y

Nessun commento:

Posta un commento