2009-07-09 18 views
2

コード行は、私はそれがなるであろう0.4〜0.6に調整した場合形シースルー "User32Wrappers.SetLayeredWindowAttributes(Me.Handle、0、_ 255 * 0.4、User32Wrappers.LWA.Alpha)"フォームの不透明度をトラックバーで制御できますか?

を行いますシースルーが少ない。

私はこの変数をトラックバーで制御できるかどうかを知りたいと思います。

パブリッククラスForm1の

Private _InitialStyle As Integer 


    Private Sub Form_Load(ByVal sender As System.Object, _ 
     ByVal e As System.EventArgs) _ 
     Handles MyBase.Load 

     Form2.Show() 



     ' Grab the Extended Style information 

     ' for this window and store it. 

     _InitialStyle = User32Wrappers.GetWindowLong(Me.Handle, User32Wrappers.GWL.ExStyle) 

     ' Set this window to Transparent 

     ' (to the mouse that is!) 

     SetFormToTransparent() 

     ' Just for giggles, set this window 

     ' to stay on top of all others so we 

     ' can see what's happening. 

     Me.TopMost = True 
    End Sub 

    Private Sub SetFormToTransparent() 
     ' This creates a new Extended Style 

     ' for our window, which takes effect 

     ' immediately upon being set, that 

     ' combines the initial style of our window 

     ' (saved in Form.Load) and adds the ability 

     ' to be Transparent to the mouse. 

     ' Both Layered and Transparent must be 

     ' turned on for this to work AND have 

     ' the window render properly! 

     User32Wrappers.SetWindowLong(Me.Handle, User32Wrappers.GWL.ExStyle, _ 
      _InitialStyle Or User32Wrappers.WS_EX.Layered Or User32Wrappers.WS_EX.Transparent) 

     ' Don't forget to set the Alpha 

     ' for the window or else you won't be able 

     ' to see the window! Possible values 

     ' are 0 (visibly transparent) 

     ' to 255 (visibly opaque). I'll set 

     ' it to 20% visible here for show. 

     ' The second parameter is 0, because 

     ' we're not using a ColorKey! 

     User32Wrappers.SetLayeredWindowAttributes(Me.Handle, 0, _ 
          255 * 0.4, User32Wrappers.LWA.Alpha) 
    End Sub 

    Private Sub SetFormToOpaque() 
     ' Turn off the Transparent Extended Style. 

     User32Wrappers.SetWindowLong(Me.Handle, User32Wrappers.GWL.ExStyle, _ 
       _InitialStyle Or User32Wrappers.WS_EX.Layered) 

     ' Set the Alpha back to 100% opaque. 

     User32Wrappers.SetLayeredWindowAttributes(Me.Handle, _ 
          0, 255, User32Wrappers.LWA.Alpha) 
    End Sub 

    Public Class User32Wrappers 

     Public Enum GWL As Integer 
      ExStyle = -20 
     End Enum 

     Public Enum WS_EX As Integer 
      Transparent = &H20 
      Layered = &H80000 
     End Enum 

     Public Enum LWA As Integer 
      ColorKey = &H1 
      Alpha = &H2 
     End Enum 

     <DllImport("user32.dll", EntryPoint:="GetWindowLong")> _ 
     Public Shared Function GetWindowLong(_ 
      ByVal hWnd As IntPtr, _ 
      ByVal nIndex As GWL _ 
       ) As Integer 
     End Function 

     <DllImport("user32.dll", EntryPoint:="SetWindowLong")> _ 
     Public Shared Function SetWindowLong(_ 
      ByVal hWnd As IntPtr, _ 
      ByVal nIndex As GWL, _ 
      ByVal dwNewLong As WS_EX _ 
       ) As Integer 
     End Function 

     <DllImport("user32.dll", _ 
      EntryPoint:="SetLayeredWindowAttributes")> _ 
     Public Shared Function SetLayeredWindowAttributes(_ 
      ByVal hWnd As IntPtr, _ 
      ByVal crKey As Integer, _ 
      ByVal alpha As Byte, _ 
      ByVal dwFlags As LWA _ 
       ) As Boolean 
     End Function 
    End Class 

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
     End 
    End Sub 
End Class 

答えて

1

あなたはここでほとんどの作業を行ってきました!ただ、自分のフォームにトラックバーを追加し1に最小を設定し、100に最大、そして自分のフォームに次のコードを追加します。

Private Sub TrackBar1_Scroll(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TrackBar1.Scroll 
    SetOpacity(TrackBar1.Value/100) 
End Sub 

Private Sub SetOpacity(ByVal value As Single) 

    ' Turn off the Transparent Extended Style. 
    User32Wrappers.SetWindowLong(Me.Handle, User32Wrappers.GWL.ExStyle, _ 
      _InitialStyle Or User32Wrappers.WS_EX.Layered) 

    ' Set the Alpha. 
    User32Wrappers.SetLayeredWindowAttributes(Me.Handle, _ 
         0, 255 * value, User32Wrappers.LWA.Alpha) 
End Sub 
+0

あなたはおそらく設定したくない人のおかげでそんなに –

+1

です最小値は0です - アルファベットがゼロのピクセルにマウスイベントが送信されないため、ユーザーが最後までドラッグすると、そこに停滞します。 –

+0

はいいいえポイントありがとう –

関連する問題