Implementing the IActiveDesktop in VB.NET ( Part 1 )

dynamic_sysop

Senior Contributor
Joined
Oct 1, 2002
Messages
1,039
Location
Ashby, Leicestershire.
i've decided to post an example on Implementing the IActiveDesktop interface in vb.net , examples in c# / j# can be found on my personal website.
here's the vb.net example ( part 1 ) ...
Visual Basic:
    <DllImport("OLE32.DLL")> _
    Public Shared Function CoCreateInstance( _
         ByRef ClassGuid As Guid, _
         ByVal pUnkOuter As IntPtr, _
         ByVal dwClsContext As Integer, _
         ByRef InterfaceGuid As Guid, _
         ByRef Result As IActiveDesktop) As IntPtr
    End Function

    Private CLSID_ActiveDesktop As New Guid("75048700-EF1F-11D0-9888-006097DEACF9")
    Private IID_IActiveDesktop As New Guid("F490EB00-1240-11D1-9888-006097DEACF9")
    Private Const CLSCTX_INPROC_SERVER As Integer = 1

    Private ActiveDesktop As IActiveDesktop

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CoCreateInstance(CLSID_ActiveDesktop, _
        IntPtr.Zero, CLSCTX_INPROC_SERVER, _
        IID_IActiveDesktop, ActiveDesktop)

    End Sub
    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If SUCCEEDED(ActiveDesktop) Then
            Marshal.ReleaseComObject(ActiveDesktop)
        End If
    End Sub

    Private Function SUCCEEDED(ByVal obj As Object) As Boolean
        '/// checks to see if an object is created or " Nothing "
        If Not obj Is Nothing Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Function makethumb() As Boolean
        Return False
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If SUCCEEDED(ActiveDesktop) Then
            Dim sBuilder As New StringBuilder(_win32.MAX_PATH)
            ActiveDesktop.GetWallpaper(sBuilder, sBuilder.Capacity, 0)
            Label1.Text = sBuilder.ToString
            '/// create a thumbnail image of your current wallpaper and display in a picturebox.
            Dim imgCallback As New Image.GetThumbnailImageAbort(AddressOf makethumb)
            Dim bmp As Bitmap = Bitmap.FromFile(Label1.Text)
            Dim imgThumb As Image = bmp.GetThumbnailImage((PictureBox1.Width / bmp.Width) * bmp.Width, (PictureBox1.Height / bmp.Height) * bmp.Height, imgCallback, IntPtr.Zero)
            PictureBox1.Image = imgThumb
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If SUCCEEDED(ActiveDesktop) Then
            Dim OD As New OpenFileDialog
            With OD
                .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures)
                .Filter = "Jpeg |*.jpg|Bitmap |*.bmp"
                If .ShowDialog = DialogResult.OK Then
                    ActiveDesktop.SetWallpaper(OD.FileName, 0)
                    ActiveDesktop.ApplyChanges(_win32.AD_APPLY_FORCE Or _win32.AD_APPLY_SAVE Or _win32.AD_APPLY_REFRESH)
                End If
            End With
        End If
    End Sub

End Class
and the sample project also.
 

Attachments

Back
Top