自動啟動多媒體光盤技術應用十分廣泛,現在的光盤大多應用這些技術,減少用戶單擊進入的時間。
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(); } } }