想製作像Aero一樣,半透明的表單嗎?套用Aero API,讓你程式的每個Form都很Aero!

1.首先,將System.Runtime.InteropServices加入參考,並加入這段Structure。

Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> Public Structure MARGINS Public cxLeftWidth As Integer Public cxRightWidth As Integer Public cyTopHeight As Integer Public cyBottomHeight As Integer End Structure

2.接著在Class裡面加入這段Function。

<DllImport("dwmapi.dll")> _ Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef margins As MARGINS) As Integer End Function

3.最後在Load裡面加入這段程式碼。其中-1是指整個表單,如果你不希望整個表單都是半透明的話,你可以修改下列數值。

Dim margins As MARGINS = New MARGINS margins.cxLeftWidth = -1 margins.cxRightWidth = -1 margins.cyTopHeight = -1 margins.cyBottomHeight = -1 Dim hwnd As IntPtr = Me.Handle Dim result As Integer = DwmExtendFrameIntoClientArea(hwnd, margins) Me.BackColor = Color.Black '表單會把黑色的部分變為半透明,所以設表單為黑色。

最後,如果還是看不懂的話,附上整個源碼供參考吧。

Imports System.Runtime.InteropServices <StructLayout(LayoutKind.Sequential)> Public Structure MARGINS Public cxLeftWidth As Integer Public cxRightWidth As Integer Public cyTopHeight As Integer Public cyBottomHeight As Integer End Structure Public Class Form1 <DllImport("dwmapi.dll")> _ Private Shared Function DwmExtendFrameIntoClientArea(ByVal hwnd As IntPtr, ByRef margins As MARGINS) As Integer End Function Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim margins As MARGINS = New MARGINS margins.cxLeftWidth = -1 margins.cxRightWidth = -1 margins.cyTopHeight = -1 margins.cyBottomHeight = -1 Dim hwnd As IntPtr = Me.Handle Dim result As Integer = DwmExtendFrameIntoClientArea(hwnd, margins) Me.BackColor = Color.Black End Sub End Class ' Source by https://noob.tw