Replacing a desktop background program with C #

Source: Internet
Author: User
Tags bmp image bool function prototype set background split
Program

Replacing a desktop background program with C #

Today is the weekend, but there is no place to go, so there is the internet came, suddenly found an article with

VB to invoke the API to replace the desktop program, I think since VB can C # also must be able to do, so on

Try to do it, okay, let's look at my code. Step by step, you can do it.

So let's start with an API, SystemParametersInfo, the function of this API

It is simple to use some parameter settings to complete some of the system's appearance settings.

The function prototype is as follows:
BOOL SystemParametersInfo (
UINT Uiaction,
UINT Uiparam,
Pvoid Pvparam,
UINT Fwinini
);

The function returns a bool value. Non 0 success, or of course it's a failure, that's what MSDN says.

GetLastError will also be set (refer to MSDN for this)

One thing to mention here is that the table contains a lot of information about these parameters in the uaction.

The setup work. Because it will affect the. Front two parameters. The third argument in our use here is to get

The path to the picture. The fourth parameter is also guessed by the name. The user configuration parameters set with this function are saved in the Win.ini

Or the registration table, or at the same time, in these two places. It's usually 0x1 or 0x2.

I'm giving you the following code for the API into C #:

[DllImport ("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uaction, int uparam, string lpvparam, int fuwinini);

Image


See the picture above? I'm going to talk about the two button,

The first call to the selection button code is as follows:

private void Button1_Click (object sender, System.EventArgs e)
{
Openfiledialog1.initialdirectory = @ "C:\";
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
TextBox1.Text = Openfiledialog1.filename;
String[] Stra=textbox1.text.split ('. ');
Bitmap bm=new Bitmap (TextBox1.Text);
if (stra[1]!= "BMP")
{
filepath=stra[0]+ ". bmp";
Bm. Save (filepath);
}
Else
Filepath=textbox1.text;
THIS.PICTUREBOX1.IMAGE=BM;
}
As you can see, because the BMP image is only set to the desktop, it has to be converted, which is my method

Maybe you have a better one, so let's talk about it.

Then the Change button, the code is as follows:

private void Button2_Click (object sender, System.EventArgs e)
{
int nresult;
if (file.exists (filepath))
{
Nresult = SystemParametersInfo (1, filepath, 0x1 | 0x2);
if (nresult==0)
MessageBox.Show ("No update successful!");
Else
MessageBox.Show ("Changing background picture ...");
}
Else
MessageBox.Show ("file does not exist!");

}
This is a simple implementation, just to invoke the API mentioned above.

OK, I'll give you all the code, very simple, as follows:


Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using System.Data;
Using System.Runtime.InteropServices;
Using System.IO;
Namespace Desktopwalk
{
<summary>
Summary description of the Form1.
</summary>
public class Form1:System.Windows.Forms.Form
{
Private System.Windows.Forms.Button button1;
Private System.Windows.Forms.Button button2;
Private System.Windows.Forms.TextBox TextBox1;
Private System.Windows.Forms.GroupBox GroupBox1;
Private System.Windows.Forms.GroupBox GroupBox2;
Private System.Windows.Forms.Label Label1;
Private System.Windows.Forms.PictureBox PictureBox1;
Private System.Windows.Forms.Label Label2;
Private System.Windows.Forms.OpenFileDialog OpenFileDialog1;
private string filepath;
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.Container components = null;
[DllImport ("user32.dll", CharSet=CharSet.Auto)]
public static extern int SystemParametersInfo (int uaction, int uparam, string lpvparam, int fuwinini);

Public Form1 ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();

//
TODO: Add any constructor code after the InitializeComponent call
//
}

<summary>
Clean up all resources that are in use.
</summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}

#region Windows Form Designer generated code
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This.button1 = new System.Windows.Forms.Button ();
This.button2 = new System.Windows.Forms.Button ();
This.textbox1 = new System.Windows.Forms.TextBox ();
This.groupbox1 = new System.Windows.Forms.GroupBox ();
This.label2 = new System.Windows.Forms.Label ();
This.picturebox1 = new System.Windows.Forms.PictureBox ();
This.label1 = new System.Windows.Forms.Label ();
This.groupbox2 = new System.Windows.Forms.GroupBox ();
This.openfiledialog1 = new System.Windows.Forms.OpenFileDialog ();
This.groupBox1.SuspendLayout ();
This. SuspendLayout ();
//
Button1
//
This.button1.Location = new System.Drawing.Point (312, 62);
This.button1.Name = "Button1";
This.button1.TabIndex = 0;
This.button1.Text = "Select Background";
This.button1.Click + = new System.EventHandler (This.button1_click);
//
Button2
//
This.button2.Location = new System.Drawing.Point (312, 120);
This.button2.Name = "Button2";
This.button2.TabIndex = 1;
This.button2.Text = "Change Background";
This.button2.Click + = new System.EventHandler (This.button2_click);
//
TextBox1
//
This.textBox1.Location = new System.Drawing.Point (16, 64);
This.textBox1.Name = "TextBox1";
This.textBox1.Size = new System.Drawing.Size (272, 21);
This.textBox1.TabIndex = 2;
This.textBox1.Text = "";
//
GroupBox1
//
This.groupBox1.Controls.AddRange (new system.windows.forms.control[] {
This.label2,
This.picturebox1,
This.label1,
This.groupbox2,
This.button1,
This.button2,
This.textbox1});
This.groupBox1.Location = new System.Drawing.Point (16, 16);
This.groupBox1.Name = "GroupBox1";
This.groupBox1.Size = new System.Drawing.Size (392, 240);
This.groupBox1.TabIndex = 3;
This.groupBox1.TabStop = false;
This.groupBox1.Text = "Change background picture";
//
Label2
//
This.label2.Location = new System.Drawing.Point (8, 128);
This.label2.Name = "Label2";
This.label2.Size = new System.Drawing.Size (72, 23);
This.label2.TabIndex = 6;
This.label2.Text = "Preview Picture:";
//
PictureBox1
//
This.pictureBox1.Location = new System.Drawing.Point (104, 120);
This.pictureBox1.Name = "PictureBox1";
This.pictureBox1.Size = new System.Drawing.Size (184, 104);
This.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
This.pictureBox1.TabIndex = 5;
This.pictureBox1.TabStop = false;
//
Label1
//
This.label1.Location = new System.Drawing.Point (24, 24);
This.label1.Name = "Label1";
This.label1.Size = new System.Drawing.Size (64, 23);
This.label1.TabIndex = 4;
This.label1.Text = "background picture:";
//
GroupBox2
//
This.groupBox2.Location = new System.Drawing.Point (8, 104);
This.groupBox2.Name = "GroupBox2";
This.groupBox2.Size = new System.Drawing.Size (376, 8);
This.groupBox2.TabIndex = 3;
This.groupBox2.TabStop = false;
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (432, 269);
This. Controls.AddRange (new system.windows.forms.control[] {
This.groupbox1});
This. MaximizeBox = false;
This. Name = "Form1";
This. StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
This. Text = "Set Background";
This.groupBox1.ResumeLayout (FALSE);
This. ResumeLayout (FALSE);

}
#endregion

<summary>
The main entry point for the application.
</summary>
[STAThread]
static void Main ()
{
Application.Run (New Form1 ());
}

private void Button2_Click (object sender, System.EventArgs e)
{
int nresult;
if (file.exists (filepath))
{
Nresult = SystemParametersInfo (1, filepath, 0x1 | 0x2);
if (nresult==0)
MessageBox.Show ("No update successful!");
Else
MessageBox.Show ("Changing background picture ...");
}
Else
MessageBox.Show ("file does not exist!");

}

private void Button1_Click (object sender, System.EventArgs e)
{
Openfiledialog1.initialdirectory = @ "C:\";
if (openfiledialog1.showdialog () = = DialogResult.OK)
{
TextBox1.Text = Openfiledialog1.filename;
String[] Stra=textbox1.text.split ('. ');
Bitmap bm=new Bitmap (TextBox1.Text);
if (stra[1]!= "BMP")
{
filepath=stra[0]+ ". bmp";
Bm. Save (filepath);
}
Else
Filepath=textbox1.text;
THIS.PICTUREBOX1.IMAGE=BM;
}

}
}
}



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.