PDA

View Full Version : سوال: تغییر تصویر روی دسک تاپ با استفاده از کد نویسی



article
سه شنبه 26 آذر 1387, 20:26 عصر
من برای تغییر عکس زمینه دسک تاپ از کد زیر استفاده کردم درست جواب می ده اما زمانی که اجرا میکنم عکسی که انتخاب میکنم به جای قرار دادن عکس عکسی که روی صفحه است را بر میداره به جاش همون رنگ پیشفرض را می آره دلیلش چی میتون باشه

class Wallpaper
{ const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(
int uAction, int uParam, string lpvParam, int fuWinIni);

public enum Style : int
{
Tiled, Centered, Stretched
}

public void SetWallpaper(string path, Style style)
{
RegistryKey key = Registry.CurrentUser.OpenSubKey(
"Control Panel\\Desktop", true);
switch( style )
{
case Style.Stretched :
key.SetValue(@"WallpaperStyle", "2") ;
key.SetValue(@"TileWallpaper", "0") ;

break;
case Style.Centered :
key.SetValue(@"WallpaperStyle", "1") ;
key.SetValue(@"TileWallpaper", "0");
break;
case Style.Tiled:
key.SetValue(@"WallpaperStyle", "1");
key.SetValue(@"TileWallpaper", "1");
break;
}

SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, path,
SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}


}



private void button1_Click_2(object sender, EventArgs e)
{
openFileDialog1.Filter = "Images files (*.jpg) |*.jpeg|"
+ " All files (*.*) |*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Title = "Demo Open File Dialog";
// Show the OpenFileDialog and if the user clicks th
// Open button, load the file
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
// Save the file name
fileName = openFileDialog1.FileName;
}
Wallpaper w = new Wallpaper();
w.SetWallpaper(fileName,
(Wallpaper.Style)Enum.Parse(typeof(Wallpaper.Style ), "Tiled"));



}