Noob's Space

[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 &#39;表單會把黑色的部分變為半透明,所以設表單為黑色。

 

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

Imports System.Runtime.InteropServices
&lt;StructLayout(LayoutKind.Sequential)&gt; Public Structure MARGINS
    Public cxLeftWidth As Integer
    Public cxRightWidth As Integer
    Public cyTopHeight As Integer
    Public cyBottomHeight As Integer
End Structure
Public Class Form1
    &lt;DllImport(&quot;dwmapi.dll&quot;)&gt; _
    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
&#39; Source by https://noob.tw

你可能會有興趣......?

  • 請問「Me.BackColor」是不是必定是「Color.Black」,因為我的程式變成這樣了!
    http://i.imgur.com/UdMYy0O.png

    • 他會把黑色的部分當透明

  • 有一個神奇的現象 可以點到半透明Form 後面另一個form的按鈕 orz

廣告