Increasing Form Transparency from 0 to 100

Hello every1

How can I Increase my form transparency from 0 to 100

I have wrote the code bellow in the tick event of my timer1. and I`ve set the timer1.Interval =1, and it is working, But it is very very very slower than 1 mili second. I know there must be a little delay for running the code of tick event, but it is so slower than the interval as 1 second . ohhhhhhh pls help me on this.

Am I supposed to use the "user32.dll" If Yes how can I use that What is the differences between setting the form transparency by using "user32.dll" and my way at all

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Me.Opacity = i

' variable ( i ) is a public variable

i = i + 0.06

If i = 1 Then Timer1.Stop()

End Sub



Answer this question

Increasing Form Transparency from 0 to 100

  • zdrae

    SH,

    You know..... give that that timer has an interval resolution of 1 millisecond, but an accuracy of 55 ms is really, really horrible.



  • Devin

    Look at the help for the forms timer: it's limited to about 55 milliseconds. Not sure why you'd need the fade steps to run faster than that - a user won't be able to see it (it'd be nice if it'd go to a bit less than that, but it's always been that way...).

    Plus, in your code above, the timer will never stop .



  • tackett

    What is the size of the form in pixels WHat kind of video card do you have because the ability to do this quickly will be card dependent.



  • ashk1860

    I rarely have a need to do anything fast. Most of the time I use a timer to give me a non-sleeping delay. Given the hardware that we have today there's no excuse for a 55ms timer.



  • Brahm

    Actually, it's a minimum value rather than resolution: I don't use form timers for much, except slow stuff, anyway. Usually, I'll use the thread timer for fast updates (but you have to be careful what you do with them, because of firing on a different thread, and running from a thread pool).



  • mamo

    thnks guys But:

    I have seen a timer with an interval as 1 ms in VB6 and it was working very good, But I can`t change those codes to Vb.NET 2005. The code below is from the vb6 prog. The form increases his transparency very very light and lovely but in vb.net , ooooooohhhhh, i am your student but I am still thinking about that "user32.dll".

    These Private Function and also the Trans Function is declared in a module:

    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

    Private Declare Function SetWindowPos Lib "user32" (ByVal hWnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long

    Private Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags 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

    This function I think is exactly the one which uses the user32.dll library:

    '/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/
    'Trancparent
    '/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/*/

    Public Function Trans(ByVal hWnd As Long, Perc As Integer) As Long
    Dim Msg As Long
    On Error Resume Next
    If Perc < 0 Or Perc > 255 Then
    Trans = 1
    Else
    Msg = GetWindowLong(hWnd, GWL_EXSTYLE)
    Msg = Msg Or WS_EX_LAYERED
    SetWindowLong hWnd, GWL_EXSTYLE, Msg
    SetLayeredWindowAttributes hWnd, 0, Perc, LWA_ALPHA
    Trans = 0
    End If
    If Err Then
    Trans = 2
    End If
    End Function

    And this is the Timer Event in the main form:

    Private Sub Timer1_Timer()
    If HScroll1.Value < 252 Then
    HScroll1.Value = HScroll1.Value + 6
    Trans FrmMain.hWnd, HScroll1.Value
    Else
    Trans FrmMain.hWnd, 255
    Timer1.Enabled = False
    End If
    End Sub


  • EricGeorge

    ' You are using a System.Windows.Forms.Timer.

    ' The timer below has much better 'resolution'

     

    Public Class Form1

     

        Friend WithEvents aTimer As New System.Timers.Timer()

        Dim i As Double = 0

     

        Private Sub Form1_Load(ByVal sender As Object, _

        ByVal e As System.EventArgs) Handles Me.Load

            AddHandler aTimer.Elapsed, AddressOf OnTimedEvent

            Me.Opacity = 0

            aTimer.Interval = 1

            aTimer.Enabled = True

        End Sub

     

        Private Sub OnTimedEvent(ByVal source As Object, _

        ByVal e As Timers.ElapsedEventArgs)

     

            i += 0.01

            BeginInvoke(_NewNumber, New Object() {i})

     

        End Sub

     

        Private _NewNumber As New NewNumber(AddressOf ChangeOpac)

     

        Delegate Sub NewNumber(ByVal num As Double)

     

        Private Sub ChangeOpac(ByVal num As Double)

            If i <= 1 Then

                Me.Opacity = i

            Else

                Me.Opacity = 1

                aTimer.Enabled = False

            End If

     

        End Sub

    End Class

     



  • ReneeC

    ReneeC

    That just the "rule of Kanstrup" ;-)

    "Each time the hardware gets 2 times faster, the software nerds makes the software 2.5 times less efficient."

    In the childhood of the computer, we typically had a respons time of 100 mS on a 2 MHz 8080 system, which used 8 cycles for each instruction, so that it executed approximately 250,000 instructions per second. Today, modern RISC computers execute approximately 2,000,000,000 instructions per second, a factor 2**13. So according to this rule, the reduction in respons time is (2.5/2)**13 = 18 times. This fits very well with practical experience with automation systems, where a respons time of approximately 1.8 sec. is quite common for SCADA systems etc. I have even seen respons times up to 5 sec. on a small demo systems on an automation fair!

    This is just the way it is - just look at Vista. Except for game programmers, you and me (see http://www.innovatic.dk/programm.htm) almost nobody is interested in efficiency!


  • Increasing Form Transparency from 0 to 100