To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
Article :: Building Dynamic Systems with Expressions in .NET
How Is XML Like An Interface?
Understanding Covariance and Contravariance
Print VS 2010 Keyboard Shortcut References in Letter (8.5x11in) and A4 (210×297mm) Sizes
Updated Productivity Power Tools



Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier

Reply Post New Thread
 
Thread Tools Display Modes
Old Oct 24th, 2005, 02:10 PM   #1
rm_03
Lively Member
 
rm_03's Avatar
 
Join Date: Aug 04
Posts: 92
rm_03  is on a distinguished road (30+)
VB6 - JPEGs as wallpapers (without conversion)

Hi there,

today I want to show you how to access IActiveDesktop to set
a new wallpaper (Active Desktop has to be enabled).
Actually nothing special, just declare IActiveDesktop in a type library,
and instance it with the "new" keyword in VB.
In this example I did it without the use of a type lib. Hardcore!!

Put the following in a module:
VB Code:
  1. Option Explicit
  2. Private Declare Function IIDFromString Lib "ole32" ( _
  3.   ByVal lpszIID As Long, _
  4.   iid As Any) As Long
  5. Private Declare Function CoCreateInstance Lib "ole32" ( _
  6.   rclsid As Any, _
  7.   ByVal pUnkOuter As Long, _
  8.   ByVal dwClsContext As Long, _
  9.   riid As Any, _
  10.   ByVal ppv As Long) As Long
  11. Private Declare Function CallWindowProcA Lib "user32" ( _
  12.   ByVal addr As Long, _
  13.   ByVal p1 As Long, _
  14.   ByVal p2 As Long, _
  15.   ByVal p3 As Long, _
  16.   ByVal p4 As Long) As Long
  17. Private Declare Sub RtlMoveMemory Lib "kernel32" ( _
  18.   pDst As Any, _
  19.   pSrc As Any, _
  20.   ByVal dlen As Long)
  21. Private Const CLSCTX_INPROC_SERVER  As Long = 1&
  22. Private Const CLSID_ActiveDesktop   As String = "{75048700-EF1F-11D0-9888-006097DEACF9}"
  23. Private Const IID_ActiveDesktop     As String = "{F490EB00-1240-11D1-9888-006097DEACF9}"
  24. Private Type GUID
  25.   data1                   As Long
  26.   data2                   As Integer
  27.   data3                   As Integer
  28.   data4(7)                As Byte
  29. End Type
  30. Private Type IActiveDesktop
  31.   ' IUnknown
  32.   QueryInterface          As Long
  33.   AddRef                  As Long
  34.   Release                 As Long
  35.   ' IActiveDesktop
  36.   ApplyChanges            As Long
  37.   GetWallpaper            As Long
  38.   SetWallpaper            As Long
  39.   GetWallpaperOptions     As Long
  40.   SetWallpaperOptions     As Long
  41.   GetPattern              As Long
  42.   SetPattern              As Long
  43.   GetDesktopItemOptions   As Long
  44.   SetDesktopItemOptions   As Long
  45.   AddDesktopItem          As Long
  46.   AddDesktopItemWithUI    As Long
  47.   ModifyDesktopItem       As Long
  48.   RemoveDesktopItem       As Long
  49.   GetDesktopItemCount     As Long
  50.   GetDesktopItem          As Long
  51.   GetDesktopItemByID      As Long
  52.   GenerateDesktopItemHtml As Long
  53.   AddUrl                  As Long
  54.   GetDesktopItemBySource  As Long
  55. End Type
  56. Private Enum AD_APPLY
  57.   AD_APPLY_SAVE = &H1
  58.   AD_APPLY_HTMLGEN = &H2
  59.   AD_APPLY_REFRESH = &H4
  60.   AD_APPLY_ALL = &H7
  61.   AD_APPLY_FORCE = &H8
  62.   AD_APPLY_BUFFERED_REFRESH = &H10
  63.   AD_APPLY_DYNAMICREFRESH = &H20
  64. End Enum
  65. Public Function ActiveDesktopSetWallpaper( _
  66.     ByVal strFile As String _
  67. ) As Boolean
  68.     Dim vtbl            As IActiveDesktop
  69.     Dim vtblptr         As Long
  70.     Dim classid         As GUID
  71.     Dim iid             As GUID
  72.     Dim obj             As Long
  73.     Dim hRes            As Long
  74.     ' CLSID (BSTR) to CLSID (GUID)
  75.     hRes = IIDFromString(StrPtr(CLSID_ActiveDesktop), classid)
  76.     If hRes <> 0 Then
  77.         Exit Function
  78.     End If
  79.     ' IID (BSTR) to IID (GUID)
  80.     hRes = IIDFromString(StrPtr(IID_ActiveDesktop), iid)
  81.     If hRes <> 0 Then
  82.         Exit Function
  83.     End If
  84.     ' create an instance of IActiveDesktop
  85.     ' (Set IActiveDesktop = New IActiveDesktop)
  86.     hRes = CoCreateInstance(classid, 0, CLSCTX_INPROC_SERVER, iid, VarPtr(obj))
  87.     If hRes <> 0 Then
  88.         Exit Function
  89.     End If
  90.     ' obj points now to a pointer to the VTable
  91.     ' of IActiveDesktop
  92.     '
  93.     ' dereference the VTable pointer
  94.     RtlMoveMemory vtblptr, ByVal obj, 4
  95.     ' copy the VTable to our IActiveDesktop structure
  96.     RtlMoveMemory vtbl, ByVal vtblptr, Len(vtbl)
  97.     ' call IActiveDesktop::SetWallpaper
  98.     '
  99.     ' the first parameter is always the object pointer
  100.     ' the return value should always be a HRESULT (0 = S_OK)
  101.     hRes = CallPointer(vtbl.SetWallpaper, obj, StrPtr(strFile), 0)
  102.     If hRes = 0 Then
  103.         ActiveDesktopSetWallpaper = True
  104.     End If
  105.     ' call IActiveDesktop::ApplyChanges
  106.     hRes = CallPointer(vtbl.ApplyChanges, obj, AD_APPLY_ALL Or AD_APPLY_FORCE)
  107.     ' release IActiveDesktop to free memory
  108.     ' (Set IActiveDesktop = Nothing)
  109.     CallPointer vtbl.Release, obj
  110. End Function
  111. Private Function CallPointer( _
  112.     ByVal fnc As Long, _
  113.     ParamArray params() _
  114. ) As Long
  115.   Dim btASM(&HEC00& - 1)  As Byte
  116.   Dim pASM                As Long
  117.   Dim i                   As Integer
  118.   pASM = VarPtr(btASM(0))
  119.   AddByte pASM, &H58                  ' POP EAX
  120.   AddByte pASM, &H59                  ' POP ECX
  121.   AddByte pASM, &H59                  ' POP ECX
  122.   AddByte pASM, &H59                  ' POP ECX
  123.   AddByte pASM, &H59                  ' POP ECX
  124.   AddByte pASM, &H50                  ' PUSH EAX
  125.   For i = UBound(params) To 0 Step -1
  126.     AddPush pASM, CLng(params(i))     ' PUSH dword
  127.   Next
  128.   AddCall pASM, fnc                   ' CALL rel addr
  129.   AddByte pASM, &HC3                  ' RET
  130.   CallPointer = CallWindowProcA(VarPtr(btASM(0)), 0, 0, 0, 0)
  131. End Function
  132. Private Sub AddPush(pASM As Long, lng As Long)
  133.   AddByte pASM, &H68
  134.   AddLong pASM, lng
  135. End Sub
  136. Private Sub AddCall(pASM As Long, addr As Long)
  137.   AddByte pASM, &HE8
  138.   AddLong pASM, addr - pASM - 4
  139. End Sub
  140. Private Sub AddLong(pASM As Long, lng As Long)
  141.   RtlMoveMemory ByVal pASM, lng, 4
  142.   pASM = pASM + 4
  143. End Sub
  144. Private Sub AddByte(pASM As Long, bt As Byte)
  145.   RtlMoveMemory ByVal pASM, bt, 1
  146.   pASM = pASM + 1
  147. End Sub

How to use it:
VB Code:
  1. If ActiveDesktopSetWallpaper("C:\wallpaper.jpg") Then
  2.     MsgBox "Successfully set new wallpaper"
  3. Else
  4.     MsgBox "Failed to set new wallpaper"
  5. End If
rm_03 is offline   Reply With Quote
Old Dec 11th, 2005, 06:55 AM   #2
ktxmax
New Member
 
Join Date: Dec 05
Posts: 2
ktxmax is an unknown quantity at this point (<10)
Re: VB6 - JPEGs as wallpapers (without conversion)

Hello,can you show how to modify this code to apply Active Desktop Item?? And how to make use of this COMPONENT to setup the Active Destop Item setting??

http://msdn.microsoft.com/library/de.../component.asp

VB Code:
  1. typedef struct _tagCOMPONENT {
  2.     DWORD dwSize;
  3.     DWORD dwID;
  4.     int iComponentType;
  5.     BOOL fChecked;
  6.     BOOL fDirty;
  7.     BOOL fNoScroll;
  8.     COMPPOS cpPos;
  9.     WCHAR wszFriendlyName[MAX_PATH];
  10.     WCHAR wszSource[INTERNET_MAX_URL_LENGTH];
  11.     WCHAR wszSubscribedURL[INTERNET_MAX_URL_LENGTH];
  12.     DWORD dwCurItemState;
  13.     COMPSTATEINFO csiOriginal;
  14.     COMPSTATEINFO csiRestored;
  15. } COMPONENT, *LPCO

Thanks for any guide.

Last edited by ktxmax; Dec 11th, 2005 at 07:00 AM.
ktxmax is offline   Reply With Quote
Old Jan 7th, 2006, 03:25 PM   #3
schoolbusdriver
Fanatic Member
 
schoolbusdriver's Avatar
 
Join Date: Jan 06
Location: O'er yonder
Posts: 1,020
schoolbusdriver is a jewel in the rough (200+)schoolbusdriver is a jewel in the rough (200+)schoolbusdriver is a jewel in the rough (200+)
Thumbs up Re: VB6 - JPEGs as wallpapers (without conversion)

Many thanks for this rm_03. You've understated your code! It works for HTM files too.

Last edited by schoolbusdriver; Jun 30th, 2006 at 02:59 AM. Reason: Too much waffle.
schoolbusdriver is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 08:07 PM.





Acceptable Use Policy

Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.