邪魔なユーザーフォームを透明に!(2/2 実装編)


Win32API関数を使います。Userform1のプロシージャーに下のコードをそのまま貼り付けてください。

64bit版Excelをお使いの場合は、こちらを参考にしてください。

'Win32APIの関数

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ (ByVal lpClassName As String, ByVal lpWindowName As String) As Long Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _ (ByVal hWnd As Long, ByVal nIndex 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 Function SetLayeredWindowAttributes Lib "user32" _ (ByVal hWnd As Long, ByVal crey As Byte, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long Private Const GWL_EXSTYLE = (-20) Private Const WS_EX_LAYERED = &H80000 Private Const LWA_ALPHA = &H2& Public hWnd As Long

'ユーザーフォームの値が変更された時に実行 Private Sub Slider1_Change() Dim sliderValue As Integer Dim transparencyRate As Double Dim transParency As Byte 'スライダーの値 sliderValue = Me.Slider1.Value

’透明度率(スライダーの値を割合に変換。スライダーの値が1の場合は90%、2なら80%という具合) transparencyRate = (10 - sliderValue) / 10

’255(透明度0の状態)に透明度率をかけて設定値を割り出す。 transParency = Int(255 * transparencyRate) hWnd = FindWindow("ThunderDFrame", Me.Caption) Call SetWindowLong(Me.hWnd, GWL_EXSTYLE, GetWindowLong(Me.hWnd, GWL_EXSTYLE) Or WS_EX_LAYERED) Call SetLayeredWindowAttributes(Me.hWnd, 0, transParency, LWA_ALPHA) End Sub

#マクロ #ユーザーフォーム #ExcelVBA #Slider