四个程序的代码哈,不过,前三个是小甜品,很小,练手用的

一开始,先来三个很简洁明了的控制台程序,就是DOS窗口下的,字符界面的程序啦,这个,用来初学练习是再好不过的啦,代码很少,容易查错,作出来,一样的很有成就感。最后还有一个屏保程序,稍微复杂一点。
将以下代码,分别复制出去,假使存为,1.cs , 2.cs 和3.cs 。
用csc.exe编译就行了,如果加了路径,可以在任何目录下,运行 csc.exe 1.cs 回车,就行了。编译好了,再在当前目录上,运行1.exe,就可以看到你写的程序的运行情况了。
如果,没有设好路径,那就要先找到csc.exe 再编译。也可以很简单的,在.net的IDE(集成开发环境)里,编写和运行,这就很简单了,按F5就运行了。注意在新建项目时,请选“控制台应用程序”。
以下就是代码,还有运行截图。第一个程序的截图
using System; //引用了一个叫System的名空间
class HelloWorld  //定义一个叫HelloWorld的类
{
  public static void Main()  //静态的Main方法是程序的入口
  {
   Console.WriteLine("Hello, World!");  //输出Hello, World!
  }
}

//=========================第二个程序的截图

using System;  //引用了一个叫System的名空间

class easyInput  //类的名字与文件名不同也无所谓
{
 public static void Main()
 {
  string strName;  //声明一个string类型的值变量
  Console.Write("please input your name:");  //输出一句话,但不换行
  strName = Console.ReadLine();  //从键盘读入用户的输入,回车表示输入结束
  Console.WriteLine("hello, {0}!", strName);  //格式化输出hello信息
 }
}


//==========================第三个程序的截图

using System;

class listArg0
{
 public static void Main(String[] args)
 {
  string strName;  //声明一个string类型的值变量
  strName = args[0];  //把第一个参数赋给变量strName
  Console.WriteLine("This is the first argument: {0}!", strName);  //格式化输出第一个参数
 }
}


//====================


好了,有了以上3个小程序练手,现在,我们再来写一个复杂一些的程序,屏幕保护程序。

 

namespace Screen_Saver
{
 using System;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Data;


 /// <summary>
 /// http://riji.163.com/weblog/page/metababy
 /// </summary>
 public class ScreenSaver : System.Windows.Forms.Form
 {
  /// <summary>
  /// http://shop1471977.taobao.com
  /// </summary>
  private System.ComponentModel.Container components;
  private System.Windows.Forms.Timer timerSaver;
  private System.Windows.Forms.Label lblMarquee;

  private int iSpeed = 2;
  private string strMarqueeText="花纯春撰写的屏幕保护程序";

  private System.Drawing.Font fontMarquee = new System.Drawing.Font ("Arial", 20,
   System.Drawing.FontStyle.Bold); //定义字体和加粗字体以及字的大小
  private Color colorMarquee = System.Drawing.Color.FromArgb(255,255,255); //字体颜色,这里为白色,可以随便改,

试一下其他的颜色吧,RGB分别表示为红黄蓝,其取值从0~255,不同的组合,出现不同的颜色

  private int iDistance=0;
  private int ixStart= 0;
  private int iyStart= 0;

  public ScreenSaver()
  {
   InitializeComponent();

   lblMarquee.Font=fontMarquee;
   lblMarquee.ForeColor=colorMarquee;
   Cursor.Hide();
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }


  /// <summary>
  /// 花纯春撰写的 c# 屏幕保护程序
  /// 共同学习探讨C#,欢迎来我的BLOG:http://riji.163.com/weblog/page/metababy
  /// </summary>
  private void InitializeComponent()
  {
   System.Resources.ResourceManager resources = new System.Resources.ResourceManager (typeof
    (ScreenSaver));
   this.components = new System.ComponentModel.Container ();
   this.timerSaver = new System.Windows.Forms.Timer (this.components);
   this.lblMarquee = new System.Windows.Forms.Label ();

   timerSaver.Interval = 1;
   timerSaver.Enabled = true;
   timerSaver.Tick += new System.EventHandler (this.timerSaver_Tick);
   lblMarquee.Location = new System.Drawing.Point (88, 0);
   lblMarquee.Size = new System.Drawing.Size (128, 48);
   lblMarquee.ForeColor = System.Drawing.Color.White;
   lblMarquee.TabIndex = 0;
   lblMarquee.Visible = false;
   this.MaximizeBox = false;
   this.StartPosition = FormStartPosition.Manual;
   this.AutoScaleBaseSize = new System.Drawing.Size (5, 13);
   
   this.KeyPreview = true;
   this.WindowState = FormWindowState.Maximized;

   this.ShowInTaskbar = false;
   this.Icon = (System.Drawing.Icon) resources.GetObject ("$this.Icon");
   this.ControlBox = false;
   this.MinimizeBox = false;
   this.BackColor = System.Drawing.Color.Black;
   this.ClientSize = new System.Drawing.Size (300, 300);
   this.KeyDown += new System.Windows.Forms.KeyEventHandler (this.Form1_KeyDown);
   this.MouseDown += new System.Windows.Forms.MouseEventHandler (this.Form1_MouseDown);
   this.MouseMove += new System.Windows.Forms.MouseEventHandler (this.Form1_MouseMove);
   this.Controls.Add (this.lblMarquee);
  }

  protected void timerSaver_Tick (object sender, System.EventArgs e)
  {
   lblMarquee.Text=strMarqueeText;
   lblMarquee.Height=lblMarquee.Font.Height;
   lblMarquee.Width=lblMarquee.Text.Length*2*(int)lblMarquee.Font.Size;//注意,这里,因为用的是汉字,所

以,要乘以2,如果,屏保显示的是英文,就可以不要乘2,一个汉字相当于两个半角英文。

   PlayScreenSaver();
  }

  private void PlayScreenSaver()
  {
   //得到屏幕工作区.
   System.Drawing.Rectangle ssWorkArea = System.Windows.Forms.Screen.GetWorkingArea(this);

   lblMarquee.Location=new System.Drawing.Point(ssWorkArea.Width - iDistance,
    lblMarquee.Location.Y);

   //如果当前是隐藏,则使文本再显示出来.
   lblMarquee.Visible=true;

   // Increment the label distance based on the speed set by the user.
   iDistance += iSpeed;
   // 如果文本移出屏幕,那我们将其放到右边再跑一次.
   if (lblMarquee.Location.X <= -(lblMarquee.Width))
   {
    //复位距离.
    iDistance = 0;

    //如果文本在顶部,则移到中间.
    if (lblMarquee.Location.Y == 0)
     lblMarquee.Location=new System.Drawing.Point(lblMarquee.Location.X,
      (ssWorkArea.Height / 2));

     // 如果文本在中间则移到下面.
    else if(lblMarquee.Location.Y== ssWorkArea.Height /2)
     lblMarquee.Location=new System.Drawing.Point(lblMarquee.Location.X,ssWorkArea.Height

-
      lblMarquee.Height);
     //返回顶部.
    else
     lblMarquee.Location=new System.Drawing.Point(lblMarquee.Location.X,0);
   }
  }

  protected void Form1_MouseDown (object sender, System.Windows.Forms.MouseEventArgs e)
  {
   StopScreenSaver();
  }

  protected void Form1_MouseMove (object sender, System.Windows.Forms.MouseEventArgs e)
  {
   // 记录鼠标当前位置.
   if (ixStart == 0 && iyStart == 0)
   {
    //传递鼠标当前位置(座标)给变量.
    ixStart = e.X;
    iyStart = e.Y;
    return;
   }
    // 屏保运行后,有鼠标动作吗? 如果有,则停止,退出
   else if (e.X != ixStart || e.Y != iyStart)
    StopScreenSaver();

  }

  private void StopScreenSaver()
  {
   Cursor.Show();
   timerSaver.Enabled=false;
   Application.Exit();
  }

  protected void Form1_KeyDown (object sender, System.Windows.Forms.KeyEventArgs e)
  {
   StopScreenSaver();
  }

  /// <summary>
  /// 程序运行起点.
  /// </summary>
  public static void Main(string[] args)
  {
   if (args.Length==1)
   {
    //显示选项对对话框,在这里,你可以自定义想要的选项和功能,我只是演示一下,带不同参数的实现,

下同
    if (args[0].Substring(0,2).Equals("/c"))
    {
     MessageBox.Show("不可用选项",
      "花纯春撰写 C#

屏幕保护程序",MessageBoxButtons.OK,MessageBoxIcon.Information);
     Application.Exit();
    }
     //正常启动屏保.
    else if (args[0]=="/s")
     Application.Run(new ScreenSaver());

     //显示密码对话框
    else if (args[0]=="/a")
    {
     MessageBox.Show("这个屏保不支持密码","花纯春撰写 C#

屏幕保护程序",MessageBoxButtons.OK,MessageBoxIcon.Information);
     Application.Exit();
    }
   }
    //其它的任何参数 --> 运行
   else
    Application.Run(new ScreenSaver());
  }

 }
}
 
 
好了,今天就到这里,多练习,对这些代码以充满好奇心的心态来看待,对不清楚的地方,可以试着改成不同的值,再运行,来看看,到底发

生了什么样的变化,再想想为什么会变,这样就会有兴趣,进一步的学习下去。
以上,只是我的个人理解,如果有不对的地方,请朋友们坦诚地与我交流。
http://riji.163.com/weblog/page/metababy
http://blog.csdn.net/metababy
http://spaces.msn.com/members/metababy
http://shop1471977.taobao.com

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值