📄 form1.vb
字号:
Public Class Form1
Protected backBuffer As Bitmap
Protected displayImage As Image
Private transparencyValue As Byte = 0
Private WithEvents blendTimer As Timer
Public Sub New()
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
' Load the image to use with the AlphaBlend API.
Dim path As String = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
displayImage = New Bitmap(path + "\Microsoft_Windows_Mobile_logo.bmp")
blendTimer = New Timer()
blendTimer.Interval = 50
blendTimer.Enabled = True
End Sub
Public Sub blendTimer_Tick(ByVal sender As Object, ByVal e As EventArgs) Handles blendTimer.Tick
transparencyValue += 10
If transparencyValue = 250 Then
transparencyValue = 255 ' opaque
blendTimer.Enabled = False ' stop the timer
End If
' Force a repaint
Me.Refresh()
End Sub
Protected Overrides Sub OnPaintBackground(ByVal e As System.Windows.Forms.PaintEventArgs)
' Make this a no-op, to avoid flicker
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
If Not backBuffer Is Nothing Then
' We need a Graphics object on the buffer to get an HDC
Using gxBuffer As Graphics = Graphics.FromImage(backBuffer)
' Since we nop'd OnPaintBackground, take care of it here
gxBuffer.Clear(Me.BackColor)
' AlphaBlend takes two HDC's - one source and one destination. Here's the source.
Using gxSrc As Graphics = Graphics.FromImage(displayImage)
Dim hdcDst As IntPtr = gxBuffer.GetHdc()
Dim hdcSrc As IntPtr = gxSrc.GetHdc()
Dim blendFunction As BlendFunction = New BlendFunction()
blendFunction.BlendOp = BlendOperation.AC_SRC_OVER ' Only supported blend operation
blendFunction.BlendFlags = BlendFlags.Zero ' Documentation says put 0 here
blendFunction.SourceConstantAlpha = transparencyValue ' Constant alpha factor
blendFunction.AlphaFormat = 0 ' Don't look for per pixel alpha
Dim left As Int32 = Me.Width / 2 - (displayImage.Width / 2) ' Get x co-or based on bitmap width
Dim top As Int32 = 100 ' y co-or
PlatformAPIs.AlphaBlend(hdcDst, left, top, _
displayImage.Width, displayImage.Height, _
hdcSrc, 0, 0, _
displayImage.Width, displayImage.Height, _
blendFunction)
gxBuffer.ReleaseHdc(hdcDst) ' Required cleanup to GetHdc()
gxSrc.ReleaseHdc(hdcSrc) ' Required cleanup to GetHdc()
End Using
End Using
' Put the final composed image on screen.
e.Graphics.DrawImage(backBuffer, 0, 0)
Else
e.Graphics.Clear(Me.BackColor)
End If
End Sub
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
If Not backBuffer Is Nothing Then
' dispose of the original one
backBuffer.Dispose()
End If
' Create a new backbuffer of the correct size
backBuffer = New Bitmap(Me.ClientSize.Width, Me.ClientSize.Height, _
System.Drawing.Imaging.PixelFormat.Format32bppRgb)
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -