台灣最大程式設計社群網站
線上人數
722
 
會員總數:245217
討論主題:189089
歡迎您免費加入會員
討論區列表 >> 專欄文章 >> 如何讓Form變型及漸漸淡出及透明
[]  
[我要回覆]
回應主題 加入我的關注話題 檢舉此篇討論 將提問者加入個人黑名單
如何讓Form變型及漸漸淡出及透明
價值 : 0 QP  點閱數:927 回應數:0

樓主

alumi
初學者
20 51
1341 88
發送站內信

常常看到有些設計師發問如何讓Form展現漸漸淡出或如Vista一樣的透明方式,在這裡本人舉出用程式的方式及圖片的配合做出讓Form產生變型及漸漸淡出及透明。
而本人用了vb6.0及vb.net的方式來表現此方法,讓有心設計美美Form的設計師參考。
在vb6.0裡不需準備一張透明的背景圖,可以用windows裡的API來做,程式碼如下:
Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, Y, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function CreateRoundRectRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
Private Declare Function RoundRect Lib "gdi32" (ByVal hDC As Long, ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long, ByVal X3 As Long, ByVal Y3 As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags 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 Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000
Const LWA_ALPHA = &H2
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1
Const TOPMOST_FLAGS = SWP_NOMOVE Or SWP_NOSIZE
Private Sub TOPFORM(hWnd As Long, Action As Boolean)
If Action = True Then
SetWindowPos hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
Else
SetWindowPos hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS
End If
End Sub
Private Sub Form_Load()
On Local Error Resume Next
LBLB.Left = Me.ScaleWidth / 2 - LBLB.Width / 2 - 1
LBLB.Top = 100
lonRect = CreateRoundRectRgn(0, 0, Me.ScaleWidth, Me.ScaleHeight, 20, 20)
SetWindowRgn Me.hWnd, lonRect, True
' RoundRectBorder Me, 0, 0, Me.ScaleWidth - 1, Me.ScaleHeight - 1, 20, 20, vbWhite
FormFadeIn Me, 0, 240, 4
End Sub
Private Sub RoundRectBorder(nObject As Object, X1 As Long, Y1 As Long, X2 As Long, Y2 As Long, X3 As Long, Y3 As Long, nColor As ColorConstants)
Dim A As Variant
A = nObject.ForeColor
nObject.ForeColor = nColor
RoundRect nObject.hDC, X1, Y1, X2, Y2, X3, Y3
nObject.ForeColor = A
End Sub
Private Sub FormFadeIn(ByRef nForm As Form, Optional ByVal nFadeStart As Byte = 0, Optional ByVal nFadeEnd As Byte = 255, Optional ByVal nFadeInSpeed As Byte = 5)
Dim c
Dim ne As Integer, en(32767) As Boolean
For Each c In nForm.Controls
ne = ne + 1
en(ne) = c.Enabled
c.Enabled = False
Next
If nFadeEnd = 0 Then
nFadeEnd = 255
End If
If nFadeInSpeed = 0 Then
nFadeInSpeed = 5
End If
If nFadeStart >= nFadeEnd Then
nFadeStart = 0
ElseIf nFadeEnd <= nFadeStart Then
nFadeEnd = 255
End If
TransparentsForm nForm.hWnd, 0
nForm.Show
Dim i As Long
For i = nFadeStart To nFadeEnd Step nFadeInSpeed
TransparentsForm nForm.hWnd, CByte(i)
DoEvents
Call Sleep(5)
Next
TransparentsForm nForm.hWnd, nFadeEnd
i = 0
For Each c In nForm.Controls
i = i + 1
c.Enabled = en(i)
Next
End Sub
Private Function TransparentsForm(FormhWnd As Long, Alpha As Byte) As Boolean
SetWindowLong FormhWnd, GWL_EXSTYLE, WS_EX_LAYERED
SetLayeredWindowAttributes FormhWnd, 0, Alpha, LWA_ALPHA
LastAlpha = Alpha
End Function
Private Sub FormFadeOut(ByRef nForm As Form)
On Local Error Resume Next
Dim c
Dim S As Integer
For Each c In nForm.Controls
c.Enabled = False
Next
Dim i As Long
For i = 240 To 0 Step -5
TransparentsForm nForm.hWnd, CByte(i)
DoEvents
Call Sleep(5)
Next
End Sub
'Private Sub SBtn_Exit_Click()
FormFadeOut Me
TOPFORM Me.hWnd, False
Unload Me
End Sub
在vb.net裡我們可以透過一張透明的圖片來達成使Form變行與透明,而方法很簡單,只要在load及paint裡設計程式就可以了。步驟如下:
第一、 在Form的屬性backcolor設定為黑色。
第二、 將Form的windowstate設為normal。
第三、 拉一個picturebox在Form裡。將picturebox的backgroundimage設定準備好的透明圖片。
程式碼如下:

Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.IO
Imports System.Xml.Serialization
Private Sub MainPage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Location = New System.Drawing.Point(200, 100)
Me.Opacity = 0
Me.TransparencyKey = BackColor
End Sub
Private Sub MainPage_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
For dblLoop As System.Double = 0.01 To 1 Step 0.01
Opacity = dblLoop
Application.DoEvents()
Threading.Thread.Sleep(12)
Next
End Sub
Private Sub Btn_Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Exit.Click
If MessageBox.Show("確定要離開監測系統嗎?", "離開?", _
MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button1) = DialogResult.Yes Then
For dblLoop As System.Double = 1 To 0 Step -0.01
Opacity = dblLoop
Application.DoEvents()
Threading.Thread.Sleep(12)
Next
Me.Close()
End If
End Sub


搜尋相關Tags的文章: [ From ] , [ picturebox ] ,
本篇文章發表於2008-04-13 10:37
別忘捐VP感謝幫助你的人 新手會員瞧一瞧
目前尚無任何回覆
   

回覆
如要回應,請先登入.