VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • c#制作更换壁纸程序

制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
 

  VB调用API来更换桌面的程序,我想既然VB可以C#也一定能行,所以就

  试着做了一下,好吧,来看看我的代码吧.一步一步来,你也能行.

  那还是先让我们来了解一个API吧,SystemParametersInfo,这个API的功能

  很简单就是通过一些参数的设置来完成对系统的一些外观设置.

  函数原型如下:

  BOOL SystemParametersInfo(
   UINT uiAction,
   UINT uiParam,
   PVOID pvParam,
   UINT fWinIni
  );
  该函数返回一个Bool值.非0成功,否则当然是失败了,那样的话根据MSDN的说法

  还将会设置GetLastError(关于这一点可以参考MSDN)

  这里还必须提到的一点是,关于uAction常数表,这张表里面包括了很多关于这些参数

  的设置工作.因为它将影响到.前面两个参数.第三个参数在我们这里的用法是得到

  图片的路径.第四个参数看名字也猜的到.随同这个函数设置的用户配置参数保存在win.ini

  或注册表里,或同时保存在这两个地方.一般是0X1或者0X2就可以了.

  下面我在给出有关该API变成C#的代码如下:

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

  看见上面的图了吗?我来主要说说那个两个button,

  首先叫到的是选择按钮代码如下:

  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;
    }
  正如你看到的,那样,由于只能将BMP图象设置成桌面所以必须要转化一下,上面是我的方法

 

  也许你还有更好的,那就说说吧.

  然后是更换按钮,代码如下:

  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("没有更新成功!");
    else
    MessageBox.Show("正在更换背景图片...");
    }
    else
    MessageBox.Show("文件不存在!");
   }
  这个实现起来在简单不过了,仅仅是调用刚才上面讲到的API就可以了.

  好了,我把全部代码都给你,很简单,如下:

   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>
   /// 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>
   /// 必需的设计器变量。
   /// </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()
   {
    //
    // windows 窗体设计器支持所必需的

 

  也许你还有更好的,那就说说吧.

  然后是更换按钮,代码如下:

  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("没有更新成功!");
    else
    MessageBox.Show("正在更换背景图片...");
    }
    else
    MessageBox.Show("文件不存在!");
   }
  这个实现起来在简单不过了,仅仅是调用刚才上面讲到的API就可以了.

  好了,我把全部代码都给你,很简单,如下:

   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>
   /// 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>
   /// 必需的设计器变量。
   /// </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()
   {
    //
    // windows 窗体设计器支持所必需的



相关教程