[VB.Net]套用Aero,實現半透明表單

[VB.Net]套用Aero,實現半透明表單

想製作像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 GHOST_URL/

我們正降低廣告比例以提升閱讀體驗。如果你喜歡這篇文章,不妨按下 Like 按鈕分享到社群,以行動支持我寫更多文章。 當然,你也可以 點此用新臺幣支持我,或 點此透過 BTC、ETH、USDC 等加密貨幣支持我
分享到: