代码改变世界

1.窗体与界面设计-多媒体光盘

2013-06-16 14:21  BytesLoop  阅读(545)  评论(0编辑  收藏  举报

自动启动多媒体光盘技术应用十分广泛,现在的光盘大多应用这些技术,减少用户单击进入的时间。

027 自动启动的多媒体光盘程序

实现光盘的自动运行非常简单,当用户打开自动运行的光盘时,会发现光盘中有几个特殊的文件,分别为 "autorun.exe"、"run.ico" 和 "autorun.inf",其中 "autorun.exe" 是光盘自动播放时执行的可执行文件,"run.ico" 是光盘的图标,"autorun.inf" 是一个 INI 文件。只要光盘中包含这些文件,那么在将光盘放入光驱时,就会自动运行。

1.创建一个项目,默认窗体为 Form1,向 Form1 窗体中添加 Button 控件。

2.运行程序,将可执行文件命名为 "autorun.exe",该文件就是光盘自动播放时打开的文件。

3.选择一个图标,命名为 "run.ico",该图标将在光驱读盘时显示。

4.在刻录光盘时,同时将上面的 "autorun.exe"、"run.ico" 和 "autorun.inf" 3个文件刻录到光盘中。

namespace _027_AutoRun
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //定义一个以一种特定的编码向流中写入字符的对象
            StreamWriter sw = new StreamWriter("AutoRun.inf", false);
            sw.WriteLine("[autorun]");
            sw.WriteLine("OPEN=AUTORUN.EXE");
            sw.WriteLine("ICON=run.ICO");
            sw.Close();     //关闭当前的StreamWrite对象和基础流
        }
    }
}

028 为触摸屏程序添加虚拟键盘

本例中用到 Lable 控件的透明属性和字符串截取技术,主要使用 Substring 方法。

1.创建一个项目,默认窗体为 Form1,向 Form1 窗体中添加两个 Panel 控件、一个 TextBox 控件和多个 Label 控件,每个 Label 控件对应背景图片上的一个按钮。

2.为 Panel 控件添加背景图片,并将 Label 控件的 BackColor 属性设置为透明,AutoSize 属性设置为 False。

namespace _028_TouchKey
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //处理Label的不包含事件数据的事件方法
            lbl_0.Click += new EventHandler(lbl_Click);
            lbl_1.Click += new EventHandler(lbl_Click);
            lbl_2.Click += new EventHandler(lbl_Click);
            lbl_3.Click += new EventHandler(lbl_Click);
            lbl_4.Click += new EventHandler(lbl_Click);
            lbl_5.Click += new EventHandler(lbl_Click);
            lbl_6.Click += new EventHandler(lbl_Click);
            lbl_7.Click += new EventHandler(lbl_Click);
            lbl_8.Click += new EventHandler(lbl_Click);
            lbl_9.Click += new EventHandler(lbl_Click);
            lbl_Q.Click += new EventHandler(lbl_Click);
            lbl_W.Click += new EventHandler(lbl_Click);
            lbl_R.Click += new EventHandler(lbl_Click);
            lbl_E.Click += new EventHandler(lbl_Click);
            lbl_T.Click += new EventHandler(lbl_Click);
            lbl_Y.Click += new EventHandler(lbl_Click);
            lbl_U.Click += new EventHandler(lbl_Click);
            lbl_I.Click += new EventHandler(lbl_Click);
            lbl_O.Click += new EventHandler(lbl_Click);
            lbl_P.Click += new EventHandler(lbl_Click);
            lbl_A.Click += new EventHandler(lbl_Click);
            lbl_S.Click += new EventHandler(lbl_Click);
            lbl_D.Click += new EventHandler(lbl_Click);
            lbl_F.Click += new EventHandler(lbl_Click);
            lbl_G.Click += new EventHandler(lbl_Click);
            lbl_H.Click += new EventHandler(lbl_Click);
            lbl_J.Click += new EventHandler(lbl_Click);
            lbl_K.Click += new EventHandler(lbl_Click);
            lbl_L.Click += new EventHandler(lbl_Click);
            lbl_Z.Click += new EventHandler(lbl_Click);
            lbl_X.Click += new EventHandler(lbl_Click);
            lbl_C.Click += new EventHandler(lbl_Click);
            lbl_V.Click += new EventHandler(lbl_Click);
            lbl_B.Click += new EventHandler(lbl_Click);
            lbl_N.Click += new EventHandler(lbl_Click);
            lbl_M.Click += new EventHandler(lbl_Click);
            label44.Click += new EventHandler(label44_Click);
        }

        private void label44_Click(object sender, EventArgs e)
        {
            if (textBox1.Text.Length > 0)
            {
                Label l = (Label)sender;
                textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
                textBox1.SelectionStart = textBox1.Text.Length;
            }
        }

        void lbl_Click(object sender, EventArgs e)
        {
            Label l = (Label)sender;                        //定义一个Label型的变量l
            textBox1.Text += l.Name.Substring(4, 1);        //为textBox1赋值
            textBox1.SelectionStart = textBox1.Text.Length; //设置文本框中选定文本的起始点
        }

        private void label1_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}