Replace desktop background program using C #

zhaozj2021-02-16  50

Replace desktop background program using C #

Today is the weekend, but there is no place to go, so there is Internet online, suddenly discovered one

VB calls the API to replace the desktop program, I think that since VB can c # can be alive, so

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

Let us first let us know an API, SystemParametersInfo, this API function

It is very simple to complete some of the appearance settings for the system through some parameters.

The function prototype is as follows: Bool SystemParametersInfo (Uint UIAction, UINT UIPARAM, PVOID PVPARAM, UINT FWININI);

This function returns a BOOL value. Non 0 success, otherwise certainly fail, then the words are based on MSDN

GetLastError will also be set (About this point can be referred to MSDN)

What must also be mentioned here is that there are a lot of these parameters in this table about the uAction constant table.

Setting work. Because it will affect. Two parameters in front. The third parameter is usage

The path of the picture. The fourth parameter looks at the name. The user configuration parameters set with this function are saved in Win.ini

Or in the registry, or save it in these two places. Generally 0x1 or 0x2 is OK.

Below I gave the code that the API becomes C # is as follows:

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)] Public Static Extern Int SystemParametersInfo (int UAction, Int Uparam, String LpvParam, Int Fuwini);

//image

Did you see the picture above? I am mainly talking about the two button,

The first thing to call is that the 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, then, because you can only set the BMP image to your desktop, you must transform it, it is mine. method

Maybe you still have better, then talk.

Then the replacement button, the code is as follows:

private void button2_Click (object sender, System.EventArgs e) {int nResult; if (File.Exists (filepath)) {nResult = SystemParametersInfo (20, 1, filepath, 0x1 | 0x2); if (nResult == 0) MessageBox. Show ("No Updated Success!"); Else MessageBox.show ("Replacement Background Image ...");} else messagebox.show ("File does not exist!");} This implementation is simple, just It is possible to call the API mentioned above.

Ok, I will give you all the code, it is 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 {///

/// Form1 Abstract Description. /// 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; /// /// Required designer variable. /// 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 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = null) {Components.dispose ();}} Base.Dispose (4);} #Region Windows Form Designer Generated code /// /// Designer supports the required method - do not use the code editor to modify the // this method.

/// private () {this.button1 = new system.windows.Forms.Button (); this.button2 = new system.windows.Forms.Button (); this.textbox1 = new system ..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 = "Replacement 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.group2, 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 = "Replacement 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.dr awing.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 = "Backbrovebox2 // this.groupbox2.location = new system.drawing.point (8, 104); this.groupbox2.name =" groupbox2 "; this .ghoupBox2.size = new system.drawing.size (376, 8);

THIS.GroupBox2.Tabindex = 3; this.groupbox2.tabstop = false; // // Form1 // this.drawing.size = 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

///

/// The primary entry point of the application. /// [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 (20, 1, filepath, 0x1 | 0x2); if (nResult == 0) MessageBox. Show ("No Updated Success!"); Else MessageBox.show ("Replacement Background Image ...");} 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;}

}}}

转载请注明原文地址:https://www.9cbs.com/read-27450.html

New Post(0)