C # switching between dynamic desktop background images

Source: Internet
Author: User

Problem description: staring at the desktop background. It's interesting if the desktop background changes like win7. The alarm generates an idea instantly. Use C # To write an image that dynamically switches the desktop background. After thinking about how to implement this idea, I thought about the following problems:

(1): How can I switch images to better meet customer requirements.

(2): C # what technology is used to switch the background image of the desktop.

(3): how to store and read images.

Solution:

(1): In what way? Console? Winform? Finally, we decided to use Windows Services for implementation. Because it can be started with windows, and the user changes without knowing it, it is easy to do his own thing silently. I have never worked on Windows Services before. I searched the internet and understood the principle. So I want to make a simple example. Function example: Write a service and prompt a message regularly. The code is soon written. After registration, the service cannot be started and no information is prompted. The problem is sharp. I only need to set the service attribute after an hour of Search: allow the Service to interact with the desktop. I set it and restarted the service. The prompt message is displayed, but this problem cannot be solved by the customer every time. The online search solution is provided. The key code is as follows:

 

Code

Private void serviceinstallerpolicafterinstall (Object sender, installeventargs E)
{
Connectionoptions cooptions = new connectionoptions ();
Cooptions. impersonation = impersonationlevel. Impersonate;
Managementscope mgmtscope = new system. Management. managementscope (@ "Root \ cimv2", cooptions );
Mgmtscope. Connect ();
Managementobject wmiservice;
Wmiservice = new managementobject ("win32_service.name = '" + this. serviceinstaller1.servicename + "'");
Managementbaseobject inparam = wmiservice. getmethodparameters ("change ");
Inparam ["Reply topinteract"] = true;
Managementbaseobject outparam = wmiservice. invokemethod ("change", inparam, null );
}

 

The first problem is finally solved.

(2): How to switch the image, search, it seems that there is a way to call the system API, but the image can only be in BMP format. The Code is as follows:

 

Code

# Region system innerface
[Dllimport ("user32.dll", entrypoint = "systemparametersinfo")]
Public static extern int systemparametersinfo (
Int uaction,
Int uparam,
String lpvparam,
Int fuwinini
);
# Endregion

# Region timer elapsed
Private void dynamictimer_elapsed (Object sender, system. Timers. elapsedeventargs E)
{
If (picindex <picturepath. Count)
{
Setmediatoppicture (picturepath [picindex]. tostring ());
Picindex + = 1;
}
Else
{
Picindex = 0;
Setmediatoppicture (picturepath [picindex]. tostring ());
}
}
# Endregion

 

(3): how to store and read images. I chose to use XML files for storage. You need to store two pieces of information, one is how long it takes to switch the image background, and the other is the image path. When the service starts, I store the image path in a list. If the XML image information is updated, restart the service to see the effect. The key code is as follows:

 

Code

Protected override void onstart (string [] ARGs)
{
// Todo: Add code here to start the service.
Xmldocument xmldoc = new xmldocument ();
Xmldoc. Load (xml_path );
Xmlelement xmlele = (xmlelement) xmldoc. selectsinglenode ("paths ");
Timeinterval = int. parse (xmlele. getattribute ("time"). Trim ());
Foreach (xmlnode node in xmldoc. selectsinglenode ("paths"). childnodes)
{
Picturepath. Add (node. innertext );
}
This. dynamictimer. interval = timeinterval;
This. dynamictimer. Enabled = true;
Picindex = 0;
}

 

Conclusion: all the key issues have been solved, and the running and debugging have implemented their own ideas. There are still many shortcomings in the program, such as: 1) set a pallet program to start the Restart service and provide a user interface to maintain the image path and switching time, I use the DOS command to restart the service. 2) system functions can only be set in the BMP format, and images in various forms should be implemented. 3) an installation package should be created, it's convenient for everyone to use it.

  

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.