C # Switch wallpaper using activedesktop, suitable for most image formats

Source: Internet
Author: User
My advertisement unit. Please click here. Thank you!

Operation Procedure: First add class: using system; using system. runtime. interopservices; namespace workhelper {Enum wpstyle {center = 0, tile = 1, stretch = 2, max = 3} struct wallpaperopt {public int dwsize; Public wpstyle dwstyle ;} struct componentsopt {public int dwsize; [financialas (unmanagedtype. bool)] public bool fenablecomponents; [financialas (unmanagedtype. bool)] public bool factivedesktop;} struct comppos {public Const int component_top = 0x3fffffff; Public const int component_default_left = 0 xFFFF; Public const int component_default_top = 0 xFFFF; Public int dwsize; Public int ileft; Public int iTOP; Public int dwwidth; public int dwheight; Public int izindex; [financialas (unmanagedtype. bool)] public bool fcanresize; [financialas (unmanagedtype. bool)] public bool fcanresizex; [financialas (unmanagedtype. bool)] pub LIC bool fcanresizey; Public int ipreferredleftpercent; Public int ipreferredtoppercent;} [flags] Enum itemstate {normal = 0x00000001, fullscreen = 00000002, split = 0x00000004, validsizestatebits = normal | split | fullscreen, validstatebits = normal | split | fullscreen | unchecked (INT) 0x80000000) | 0x40000000} struct compstateinfo {public int dwsize; public int ileft; Public int iTOP; Publ IC int dwwidth; Public int dwheight; Public int dwitemstate;} Enum comp_type {htmldoc = 0, picture = 1, website = 2, control = 3, cfhtml = 4, max = 4} [structlayout (layoutkind. sequential, charset = charset. unicode)] struct component {private const int internet_max_url_length = 2084; // = // internet_max_scheme_length (32) + ": // \ 0 ". length + // internet_max_path_length (2048) Public int dwsize; Public int dwid; Public comp_type icomponenttype; [financialas (unmanagedtype. bool)] public bool fchecked; [financialas (unmanagedtype. bool)] public bool fdirty; [financialas (unmanagedtype. bool)] public bool fnoscroll; Public comppos cppos; [financialas (unmanagedtype. byvaltstr, sizeconst = 260)] Public String wszfriendlyname; [financialas (unmanagedtype. byvaltstr, sizeconst = internet_max_url_length)] public String wszsource; [financialas (unmanagedtype. byvaltstr, sizeconst = signature)] Public String comment; # If ad_ie5 public int dwcuritemstate; Public compstateinfo csioriginal; Public compstateinfo csirestored; # endif} Enum comment {Limit = 0x00000000, dti_addui_dispsubwizard = 0x00000001, dti_addui_positionitem = 0x00000002,} [flags] Enum ad_apply {save = 0x0 0000001, htmlgen = 0x00000002, refresh = 0x00000004, all = save | htmlgen | refresh, force = 0x00000008, buffered_refresh = 0x00000010, dynamicrefresh = 0x00000020} [flags] Enum comp_elem {type = 0x00000001, checked = 0x00000002, dirty = 0x00000004, noscroll = 0x00000008, pos_left = 0x00000010, pos_top = 0x00000020, size_width = 0x00000040, size_height = 0x00000080, pos_zindex = 0x00000100, sour Ce = 0x00000200, friendlyname = 0x00000400, subscribedurl = 0x00000800, original_csi = 0x00001000, restored_csi = 0x00002000, curitemstate = 0x00004000, all = type | checked | dirty | noscroll | pos_left | size_width | size_height | pos_zindex | source | friendlyname | pos_top | subscribedurl | contents | curitemstate} [flags] Enum addurl {silent = 0x0001} [comimport (), guid ("F490EB00-1240-11D1-9888-006097DEACF9"), interfacetype (cominterfacetype. interfaceisiunknown)] interface iactivedesktop {void applychanges (ad_apply dwflags); void getwallpaper ([financialas (unmanagedtype. lpwstr)] system. text. stringbuilder pwszwallpaper, int cchwallpaper, int dwreserved); void setwallpaper ([exploralas (unmanagedtype. lpwstr)] string pwszwallpaper, int dwreserved); void getwallpaperopti ONS (ref wallpaperopt pwpo, int dwreserved); void setwallpaperoptions ([in] ref wallpaperopt pwpo, int dwreserved); void getpattern ([financialas (unmanagedtype. lpwstr)] system. text. stringbuilder pwszpattern, int cchpattern, int dwreserved); void setpattern ([financialas (unmanagedtype. lpwstr)] string pwszpattern, int dwreserved); void getasktopitemoptions (ref componentsopt PCO, int dwreserved); void setde Sktopitemoptions ([in] ref componentsopt PCO, int dwreserved); void addmediatopitem ([in] ref component pcomp, int dwreserved); void aggregate (intptr hwnd, [in] ref component pcomp, revoke dwflags); void modify1_topitem ([in] ref component pcomp, comp_elem dwflags); void remove1_topitem ([in] ref component pcomp, int dwreserved); void get1_topitemcount (Out int lpicount, int dwrese Rved); void getasktopitem (INT ncomponent, ref component pcomp, int dwreserved); void getasktopitembyid (intptr dwid, ref component pcomp, int dwreserved. lpwstr)] string pwszfilename, [in] ref component pcomp, int dwreserved); void addurl (intptr hwnd, [financialas (unmanagedtype. lpwstr)] string pszsource, [in] ref component pcomp, addurl dwflags ); Void getasktopitembysource ([delealas (unmanagedtype. lpwstr)] string pwszsource, ref component pcomp, int dwreserved);} [comimport (), GUID ("750000000-ef1f-11d0-9888-006097deacf9")] class activedesktop /*: iactivedesktop */{} is called: (the background image style is finally completed) Public bool set0000bg (string filename) {try {activedesktop ad = new activedesktop (); iactivedesktop IAD = AD as iactivedesktop; if (IAD! = NULL) {// wpstyle_center center 0 // wpstyle_tile Tile 1 // wpstyle_stretch stretch 2 wpstyle dwstyle = wpstyle. stretch; // adjust the display background image style wallpaperopt Wp = new wallpaperopt (); WP. dwsize = system. runtime. interopservices. marshal. sizeof (WP); WP. dwstyle = dwstyle; IAD. setwallpaperoptions (ref WP, 0); IAD. setwallpaper (filename, 0); IAD. applychanges (ad_apply.all); system. runtime. interopservices. marshal. releasecomobject (AD); Ad = NULL;} return true;} catch {return false ;}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.