Wallpaper Changer In C #

xiaoxiao2021-03-06  86

Wallpaper Changer in C # by Kfiz Arbel

Using system;

Using system.drawing;

Using system.collections;

Using system.componentmodel;

Using system.windows.forms;

Using system.Runtime.InteropServices;

Using system.io;

Namespace Wallchange

{

///

/// summary description for Form1.

///

Public Class Form1: System.Windows.Forms.form

{

Private system.windows.Forms.Button button1;

Private system.windows.forms.openfiledialog openfiledialog1;

Private system.windows.Forms.Button Button2;

Public const INT SPI_SETDESKWALLPAPER = 20;

Public const Int spif_sendchange = 0x2;

Public int picindex;

Private system.componentmodel.icontainer components;

Private system.windows.forms.checkedlistbox mycheckedlistbox;

Private system.windows.Forms.Button Btndel;

Private system.windows.forms.timer Timer1;

String filename;

String localpath;

Private system.windows.Forms.Label label1;

Private system.windows.Forms.checkbox checkbox1;

Private system.windows.Forms.comBobox CMBTime;

Private Int GlobalTimer;

Private int timerinterval;

Int [] myarray;

Public Form1 ()

{

//

// Required for Windows Form Designer Support

//

InitializationComponent ();

//

// Todo: Add Any Constructionor Code After InitializationComponent Call

//

GlobalTimer = 0;

TimerInterval = 20;

MyArray = new int = {1, 2, 5, 10, 30, 60, 120, 1440, 2880};

LocalPath = system.io.directory.getcurrentdirectory ();

Picindex = 0;

}

///

/// Clean Up Any Resources Being Used.

///

Protected Override Void Dispose (Bool Disposing)

{

IF (Disposing)

{

IF (Components! = NULL)

{

Components.dispose ();

}

}

Base.dispose (Disposing);

}

#Region Windows Form Designer Generated Code

///

/// Required Method for Designer Support - Do Not Modify /// The Contents of this Method with the code editor.

///

Private vidinitiRizeComponent ()

{

This.Components = new system.componentmodel.container ();

System.Resources.ResourceManager Resources = New System.Resources.ResourceManager (TypeOf (Form1));

This.button1 = new system.windows.Forms.Button ();

This.openfileDialog1 = new system.windows.Forms.openFiledialog ();

This.Button2 = new system.windows.Forms.Button ();

THIS.MYCHECKEDLISTBOX = new system.windows.forms.checkedListbox ();

This.btndel = new system.windows.Forms.Button ();

this.timer1 = new system.windows.forms.timer (this.components);

THIS.LABEL1 = New System.windows.Forms.label ();

This.CheckBox1 = new system.windows.forms.checkbox ();

This.cmbtime = new system.windows.forms.comboBOX ();

THIS.SUSPENDLAYOUT ();

//

// Button1

//

This.Button1.Location = new system.drawing.point (64, 8);

This.button1.name = "button1";

This.button1.size = new system.drawing.size (64, 24);

this.button1.tabindex = 0;

This.button1.text = "change";

This.Button1.click = new system.eventhandler (this.button1_click);

//

// Button2

//

This.button2.location = new system.drawing.point (8, 8);

This.Button2.name = "button2";

This.button2.size = new system.drawing.size (48, 24);

This.button2.tabindex = 2;

THIS.BUTTON2.TEXT = "& Open";

This.button2.click = new system.eventhandler (this.button2_click);

//

// mycheckedlistbox

//

this.myCheckedListBox.columnWidth = 300;

THIS.MYCHECKEDLISTBOX.HORIZONTALSCROLLBAR = TRUE;

THIS.MYCHECKEDLISTBOX.LOCATION = New System.drawing.point (8, 48); this.myCheckedListBox.name = "mycheckedlistbox";

THIS.MYCHECKEDLISTBOX.SIZE = New System.drawing.size (440, 154);

This.myCheckedListBox.sorted = true;

this.myCheckedListBox.tabindex = 3;

This.myCheckedListBox.selectedIndexchanged = new system.eventhandler (this.mycheckedlistbox_selected);

//

// btndel

//

this.btndel.location = new system.drawing.point (136, 8);

This.btndel.name = "btndel";

this.btndel.size = new system.drawing.size (64, 24);

this.btndel.tabindex = 4;

this.btndel.text = "delete";

this.btndel.click = new system.eventhandler (this.btndel_click);

//

// Timer1

//

THIS.TIMER1.ENABLED = TRUE;

this.timer1.interval = 5000;

This.Timer1.Tick = new system.eventhandler (this.timer1_thing);

//

// label1

//

THIS.Label1.Location = new system.drawing.point (376, 8);

THIS.LABEL1.NAME = "label1";

THIS.Label1.size = new system.drawing.size (72, 24);

THIS.Label1.tabindex = 5;

THIS.LABEL1.TEXT = "Written By: Kfir Arbel";

THIS.Label1.TextAlign = system.drawing.contentAlignment.middleCenter;

//

// checkbox1

//

this.checkbox1.location = new system.drawing.point (208, 8);

this.checkbox1.name = "checkbox1";

This.checkbox1.size = new system.drawing.size (80, 24);

this.checkbox1.tabindex = 6;

this.checkbox1.text = "random";

//

// CMBTIME

//

this.cmbtime.Items.addrange (new object [] {

"1 min",

"2 min",

"5 min",

"10 min",

"30 min",

"1 hour",

"2 Hour",

"1 day",

"2 day"});

This.cmbtime.location = new system.drawing.point (280, 8); this.cmbtime.name = "cmbtime";

This.cmbtime.size = new system.drawing.size (72, 21);

this.cmbtime.tabindex = 7;

this.cmbtime.text = "time";

this.cmbtime.selected = new system.eventhandler (this.cmbtime_selected);

//

// Form1

//

THIS.AUTOSCALEBASESIZE = New System.drawing.size (5, 13);

THIS.CLIENTSIZE = New System.drawing.size (472, 221);

This.Controls.add (this.cmbtime);

this.Controls.add (this.checkbox1);

This.Controls.add (this.label1);

This.Controls.add (this.btndel);

This.Controls.add (this.myCheckedListbox);

This.Controls.add (this.button2);

This.Controls.add (this.button1);

This.icon = ((System.drawing.icon) ("$ this.icon")))

THIS.NAME = "Form1";

this.Text = "Wallpaper";

This.WindowsTate = system.windows.forms.formwindowstate.minimized;

This.Load = New System.EventHandler (this.form1_load);

This.ResumeLayout (false);

}

#ndregion

///

/// The main entry point for the application.

///

[Stathread]

Static void main ()

{

Application.run (New Form1 ());

}

Private void Button1_Click (Object Sender, System.Eventargs E)

{

// if (file.exists (textbox1.text))

// {

FsetBMP ();

INT NRESULT = WinAPI.SystemParametersInfo (SPI_SETDESKWALLPAPER, 1, FILENAME, SPIF_SENDCHANGE);

Updatefile ();

}

Private void fsetbmp ()

{

Picindex = (Picindex 1)% mycheckedlistbox.checkedItems.count;

if (CheckBox1.Checked == True)

{

Random rnd = new random ();

Picindex = (rnd.next (mycheckedlistbox.checkedItems.count))

% mycheckedlistbox.checkeditems.count;

}

MyCheckedListBox.selectedIndex = Picindex; if (filename.endswith (".jpg") ||

FILENAME.ENDSWITH (".jpg") ||

FILENAME.ENDSWITH (".jpg") ||

FILENAME.ENDSWITH (".jpeg") ||

FILENAME.ENDSWITH (".jpeg") ||

FILENAME.ENDSWITH (".jpeg")

)

{

Bitmap theimage = new bitmap (filename);

THEIMAGE.SAVE (LocalPath "/new.bmp", system.drawing.image.imageformat.bmp);

Filename = localpath "/new.bmp";

}

}

Private void updatefile ()

{

Using (FileStream Fs = New FileStream (LocalPath "/my.ini", filemode.openorcreate)))

{

Streamwriter M_StreamWriter = New StreamWriter (FS);

M_StreamWriter.flush ();

M_StreamWriter.Basestream.seek (0, seekorigin.begin);

M_StreamWriter.writeline (Picindex.toString ());

M_StreamWriter.flush ();

M_StreamWriter.Close ();

}

}

Private void button2_click (Object Sender, System.Eventargs E)

{

// Change the initial directory if need

OpenfileDialog1.initialdirectory = @ "c: /";

IF (OpenFileDialog1.Showdialog () == DialogResult.ok)

{

FileName = OpenFiledialog1.FileName;

MyCheckedListBox.Items.add (filename, checkstate.checked);

Updatefile ();

// using (filestream fs = new filestream ("my.ini", filemode.openorcreate))

// {

//

// streamwriter m_streamwriter = new streamwriter (fs);

// m_streamwriter.flush ();

// m_streamwriter.basestream.seek (0, seekorigin.begin);

// //m_streamwriter.writeline (DateTime.now.toString () ":");

// m_streamwriter.writeline (Picindex.toString ());

//

// for (int i = 0; i

// {

// //listbox1.items.add (MyCheckedListBox.checkedItems[i]) ;/ m_streamwriter.writeline (MyCheckedListBox.Items [i] .tostring ());

//}

// m_streamwriter.flush ();

// m_streamwriter.close ();

//}

}

}

Private Void Form1_Load (Object Sender, System.EventArgs E)

{

IF (file.exists ("my.ini")))

{

Using (FileStream Fs = New FileStream ("My.ini", FileMode.Openorcreate)

{

StreamReader M_StreamReader = New StreamReader (FS);

M_StreamReader.basestream.seek; 00.s # 0, Seekorigin.begin;

String strline = m_streamreader.readline ();

Picindex = int32.parse (strline);

m_streamReader.close ();

}

}

MyCheckedListbox.Items.clear ();

FSETLIST ("*. bmp");

FSETLIST ("*. jpg");

FSETLIST ("*. jpeg");

MyCheckedListBox.selectedIndex = Picindex;

Filename = MyCheckedListBox.Items [Picindex] .tostring ();

}

Private void fsetlist (String strex)

{

String [] DIRS = Directory.getFiles (localpath, strexT);

FOREACH (String Dir in DIRS)

{

IF (Dir.equals (localpath "/new.bmp") == false)

{

MyCheckedListBox.Items.Add (Dir, Checkstate.Checked);

}

}

}

Public bool m_bcheckchangepossible = true;

Private void mycheckedlistbox_selectedIndexchanged (Object Sender, System.Eventargs E)

{

IF (m_bcheckchangepossible)

Filename = mycheckedlistbox.selectedItem.toString ();

}

Private void btndel_click (Object Sender, System.Eventargs E)

{

m_bcheckchangepossible = false;

INDEX = mycheckedlistbox.selectedIndex;

MyCheckedListBox.Items.Removeat (Index);

Filename = mycheckedlistbox.selectedIndex.tostring ();

m_bcheckchangepossible = true;

}

Private void Timer1_Tick (Object Sender, System.EventArgs E)

{

GlobalTimer = GlobalTimer 1; IF (GlobalTimer> TimerInterVal)

{

// int NRESULT;

// if (filename.endswith (".jpg")))

// {

// bitmap theimage = new bitmap (filename);

// theimage.save (localpath "/new.bmp", system.drawing.image.imageformat.bmp);

// filename = localpath "/new.bmp";

//}

// // if (file.exists (textbox1.text))

// / {{

// Nresult = WinAPI.SystemParametersInfo (SPI_SETDESKWALLPAPER, 1, FILENAME, SPIF_SENDCHANGE);

// picindex = (Picindex 1)% mycheckedlistbox.checkedItems.count;

// mycheckedListbox.selectedIndex = Picindex;

FsetBMP ();

INT NRESULT = WinAPI.SystemParametersInfo (SPI_SETDESKWALLPAPER, 1, FILENAME, SPIF_SENDCHANGE);

Updatefile ();

GlobalTimer = 0;

}

}

Private void CMBTIME_SELECTEDINDEXCHANGED (Object Sender, System.EventArgs E)

{

TimerInterval = myArray [cMbtime.selected "* 12;

}

}

Public Class WinAPI

{

[DLLIMPORT ("User32.dll", Charset = Charset.Auto)]

Public Static Extern Int SystemParametersInfo (int UAction, int UPARAM, STRING LPVPARAM, INT Fuinini);

Public const INT SPI_SETDESKWALLPAPER = 20;

Public const Int spif_sendchange = 0x2;

}

}

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

New Post(0)