这是程序自动创建的分类。
相对路径
.NET Compact Framework 提供了Directory.GetCurrentDirectory Method 取当前运行文件的目录,可是运行的时候会抛出 NotSupportedException 。
替代方案使用 Path.GetDirectoryName Method 和 AssemblyName.CodeBase Property 来代替。
当前运行路径
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().ManifestModule.FullyQualifiedName);
.NET Compact Framework 提供了Directory.GetCurrentDirectory Method 取当前运行文件的目录,可是运行的时候会抛出 NotSupportedException 。
替代方案使用 Path.GetDirectoryName Method 和 AssemblyName.CodeBase Property 来代替。
当前运行路径
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
首先,我们建立一个.net cf 2.0下,名称为SoundPlay的WM5 ppc项目,当然语言我选择的是C#。
然后界面设计如下:其中:
lblFileName——Text属性:文件地址
btnOpen——Text属性:浏览文件
btnPlay——Text属性:播放声音
新建一个类:Sound,代码和小镇的差不多,不过我做过一小点修改,所以也贴出来,方便大家查阅:
using System;
using System.Runtime.InteropServices;
using System.IO;
using System.Collections.Generic;
using System.Text;
namespace SoundPlay
{
public class Sound
{
private byte[] m_soundBytes;
private enum Flags
{
SND_SYNC = 0x0000, /* play synchronously (default) */
SND_ASYNC = 0x0001, /* play asynchronously */
SND_NODEFAULT = 0x0002, /* silence (!default) if sound not found */
SND_MEMORY = 0x0004, /* pszSound points to a memory file */
SND_LOOP = 0x0008, /* loop the sound until next sndPlaySound */
SND_NOSTOP = 0x0010, /* don't stop any currently playing sound */
SND_NOWAIT = 0x00002000, /* don't wait if the driver is busy */
SND_ALIAS = 0x00010000, /* name is a registry alias */
SND_ALIAS_ID = 0x00110000, /* alias is a predefined ID */
SND_FILENAME = 0x00020000, /* name is file name */
SND_RESOURCE = 0x00040004 /* name is resource name or atom */
}
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MobilePlaySound(string szSound, IntPtr hMod, int flags);//播放外部声音文件
[DllImport("CoreDll.DLL", EntryPoint = "PlaySound", SetLastError = true)]
private extern static int MoiblePlaySoundBytes(byte[] szSound, IntPtr hMod, int flags);//播放嵌入声音资源
/// <summary>
/// Construct the Sound object to play sound data from the specified stream.
/// </summary>
public Sound(Stream stream)
{
// read the data from the stream
m_soundBytes = new byte[stream.Length];
stream.Read(m_soundBytes, 0, (int)stream.Length);
}
/// <summary>
/// 从一个声音byte流构造函数Sound
/// </summary>
/// <param name="snd"></param>
public Sound(byte[] snd)
{
m_soundBytes = snd;
}
/// <summary>
/// 构造一个空Sound函数,此函数用于播放外部文件
/// </summary>
public Sound()
{
}
/// <summary>
/// Play the sound File播放外部文件的Play方法
/// </summary>
public void Play(string filename)
{
MobilePlaySound(filename, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_FILENAME));
}
/// <summary>
/// Play The Stream Sound,此方法应该对应从Byte和Stream构造的Sound
/// 注意,它的Flags设置为SND_MEMORY,因为我们的声音文件已经转换到内存中了
/// </summary>
public void Play()
{
MoiblePlaySoundBytes(m_soundBytes, IntPtr.Zero, (int)(Flags.SND_ASYNC | Flags.SND_MEMORY));
}
}
}
然后,主界面上的按钮事件代码如下:
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog ofd = new OpenFileDialog())
{
if (ofd.ShowDialog() == DialogResult.OK)
{
lblFileName.Text = ofd.FileName;
}
}
}
private void btnPlay_Click(object sender, EventArgs e)
{
Sound sound = new Sound();
sound.Play(lblFileName.Text);
}
一个很没营养,很基础逐行读文本的软体源码!
namespace Open_Url
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(135, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 0;
this.label1.Text = "批量打开网址";
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.Color.White;
this.label2.Location = new System.Drawing.Point(15, 49);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 1;
this.label2.Text = "使用方法:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.ForeColor = System.Drawing.Color.White;
this.label3.Location = new System.Drawing.Point(15, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(155, 12);
this.label3.TabIndex = 2;
this.label3.Text = "1.选择记录网址的txt文件。";
//
// label4
//
this.label4.AutoSize = true;
this.label4.ForeColor = System.Drawing.Color.White;
this.label4.Location = new System.Drawing.Point(15, 109);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(269, 12);
this.label4.TabIndex = 3;
this.label4.Text = "2.点击开始,程序执行过程中你可以控制其暂停。";
//
// label5
//
this.label5.AutoSize = true;
this.label5.ForeColor = System.Drawing.Color.White;
this.label5.Location = new System.Drawing.Point(15, 138);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(257, 12);
this.label5.TabIndex = 4;
this.label5.Text = "注:网址格式为:<a href="http://www.xx.com" target="_blank">http://www.xx.com</a>,一行一个。";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.ForeColor = System.Drawing.Color.White;
this.button1.Location = new System.Drawing.Point(17, 253);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 5;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.Black;
this.button2.ForeColor = System.Drawing.Color.White;
this.button2.Location = new System.Drawing.Point(137, 253);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 6;
this.button2.Text = "选择...";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.ForeColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(17, 311);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(304, 21);
this.textBox1.TabIndex = 7;
//
// button3
//
this.button3.BackColor = System.Drawing.Color.Black;
this.button3.ForeColor = System.Drawing.Color.White;
this.button3.Location = new System.Drawing.Point(246, 253);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 8;
this.button3.Text = "暂停";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.LinkColor = System.Drawing.Color.White;
this.linkLabel1.Location = new System.Drawing.Point(238, 372);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(83, 12);
this.linkLabel1.TabIndex = 9;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "www.ppcqq.com";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(331, 393);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.ForeColor = System.Drawing.Color.Black;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Open Url Ver1.0";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.LinkLabel linkLabel1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace Open_Url
{
public partial class Form1 : Form
{
String FileName = "";
String str = "";
StreamReader sr = null;
FileStream fs1 = null;
ThreadStart myThreadStart1 = null;
Thread thread = null;
int isPause = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请选择txt文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
myThreadStart1 = new ThreadStart(openurl);
thread = new Thread(myThreadStart1);
thread.Start();
}
}
public void openurl()
{
fs1 = new FileStream(FileName, FileMode.Open);
sr = new StreamReader(fs1);
while (!sr.EndOfStream)
{
Thread.Sleep(1000);
str = sr.ReadLine();
// Console.WriteLine(str);
System.Diagnostics.Process.Start("IEXPLORE.EXE", str);
}
MessageBox.Show("网页已全部打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
sr.Close();
fs1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = ".txt|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = this.openFileDialog1.FileName;
textBox1.Text = FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (isPause == 0)
{
if (thread.IsAlive == true)
{
thread.Suspend();
isPause = 1;
button3.Text = "继续";
}
}
else
{
if (thread.IsAlive == true)
{
thread.Resume();
isPause = 0;
button3.Text = "暂停";
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.ppcqq.com");
}
}
}
namespace Open_Url
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.button3 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.linkLabel1 = new System.Windows.Forms.LinkLabel();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(135, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(77, 12);
this.label1.TabIndex = 0;
this.label1.Text = "批量打开网址";
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.Color.White;
this.label2.Location = new System.Drawing.Point(15, 49);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(65, 12);
this.label2.TabIndex = 1;
this.label2.Text = "使用方法:";
//
// label3
//
this.label3.AutoSize = true;
this.label3.ForeColor = System.Drawing.Color.White;
this.label3.Location = new System.Drawing.Point(15, 80);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(155, 12);
this.label3.TabIndex = 2;
this.label3.Text = "1.选择记录网址的txt文件。";
//
// label4
//
this.label4.AutoSize = true;
this.label4.ForeColor = System.Drawing.Color.White;
this.label4.Location = new System.Drawing.Point(15, 109);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(269, 12);
this.label4.TabIndex = 3;
this.label4.Text = "2.点击开始,程序执行过程中你可以控制其暂停。";
//
// label5
//
this.label5.AutoSize = true;
this.label5.ForeColor = System.Drawing.Color.White;
this.label5.Location = new System.Drawing.Point(15, 138);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(257, 12);
this.label5.TabIndex = 4;
this.label5.Text = "注:网址格式为:<a href="http://www.xx.com" target="_blank">http://www.xx.com</a>,一行一个。";
//
// button1
//
this.button1.BackColor = System.Drawing.Color.Black;
this.button1.ForeColor = System.Drawing.Color.White;
this.button1.Location = new System.Drawing.Point(17, 253);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 5;
this.button1.Text = "开始";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.Black;
this.button2.ForeColor = System.Drawing.Color.White;
this.button2.Location = new System.Drawing.Point(137, 253);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 6;
this.button2.Text = "选择...";
this.button2.UseVisualStyleBackColor = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.ForeColor = System.Drawing.Color.White;
this.textBox1.Location = new System.Drawing.Point(17, 311);
this.textBox1.Name = "textBox1";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(304, 21);
this.textBox1.TabIndex = 7;
//
// button3
//
this.button3.BackColor = System.Drawing.Color.Black;
this.button3.ForeColor = System.Drawing.Color.White;
this.button3.Location = new System.Drawing.Point(246, 253);
this.button3.Name = "button3";
this.button3.Size = new System.Drawing.Size(75, 23);
this.button3.TabIndex = 8;
this.button3.Text = "暂停";
this.button3.UseVisualStyleBackColor = false;
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// openFileDialog1
//
this.openFileDialog1.FileName = "openFileDialog1";
//
// linkLabel1
//
this.linkLabel1.AutoSize = true;
this.linkLabel1.LinkColor = System.Drawing.Color.White;
this.linkLabel1.Location = new System.Drawing.Point(238, 372);
this.linkLabel1.Name = "linkLabel1";
this.linkLabel1.Size = new System.Drawing.Size(83, 12);
this.linkLabel1.TabIndex = 9;
this.linkLabel1.TabStop = true;
this.linkLabel1.Text = "www.ppcqq.com";
this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(331, 393);
this.Controls.Add(this.linkLabel1);
this.Controls.Add(this.button3);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.label5);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.ForeColor = System.Drawing.Color.Black;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Text = "Open Url Ver1.0";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3;
private System.Windows.Forms.OpenFileDialog openFileDialog1;
private System.Windows.Forms.LinkLabel linkLabel1;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace Open_Url
{
public partial class Form1 : Form
{
String FileName = "";
String str = "";
StreamReader sr = null;
FileStream fs1 = null;
ThreadStart myThreadStart1 = null;
Thread thread = null;
int isPause = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("请选择txt文件", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
myThreadStart1 = new ThreadStart(openurl);
thread = new Thread(myThreadStart1);
thread.Start();
}
}
public void openurl()
{
fs1 = new FileStream(FileName, FileMode.Open);
sr = new StreamReader(fs1);
while (!sr.EndOfStream)
{
Thread.Sleep(1000);
str = sr.ReadLine();
// Console.WriteLine(str);
System.Diagnostics.Process.Start("IEXPLORE.EXE", str);
}
MessageBox.Show("网页已全部打开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
sr.Close();
fs1.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.openFileDialog1.Filter = ".txt|*.*";
if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = this.openFileDialog1.FileName;
textBox1.Text = FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (isPause == 0)
{
if (thread.IsAlive == true)
{
thread.Suspend();
isPause = 1;
button3.Text = "继续";
}
}
else
{
if (thread.IsAlive == true)
{
thread.Resume();
isPause = 0;
button3.Text = "暂停";
}
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("IEXPLORE.EXE", "http://www.ppcqq.com");
}
}
}
我们首先新建一个winform项目,那个form1.cs就先不动他,我们先再建立一个windows窗口,命名为splash,设置窗体属性如下:
ShowInTaskbar=false:因为是splash窗口,所以不用在任务栏显示了。然后拖动一个progressbar 和Timer控件到splash窗口如上图。
StartPosition=CenterScreen;
TransparencyKey =Black;
FormBorderStyle =None:我还没见过那个splash窗口有功能键呢
好了,我们现在回到form1这个窗口吧,在InitializeComponent 这个方法下面加入如下代码:
Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);
这里我们新建了一个线程,用于启动DoSplash这个方法(稍后提到),然后告诉他应该启动后持续3秒(3000ms=3s),然后退出,留出一秒钟来让程序收尾。
然后我们来看看DoSplash这个方法做了什么,首先,我们在form1中添加这个方法:
private void DoSplash()
{
splash sp = new splash();
sp.ShowDialog();
}
很简单,就是实例化那个splash窗口。
下面,我们开始设置splash这个窗口,首先,设置timer控件的Interval属性为1000,progressbar的step为1,maximum为3,好了,我们双击这个splash窗口,以便给他的form_load写入代码如下:
timer1.Enabled = true;
然后,设置timer1的Tick事件代码如下:
private void timer1_Tick(object sender, EventArgs e)
{
if (progressBarX1.Value < progressBarX1.Maximum)
{
progressBarX1.Step = 1;
progressBarX1.PerformStep();
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
namespace NumMask
{
public class Masker
{
ArrayList numbers = new ArrayList();//定义一个ArrayList变量,用于收集从主程序传递来的号码
ArrayList newnum = new ArrayList();//这是一个返回处理结果的Arraylist
public ArrayList NumberChange(ArrayList number)
{
foreach (string num in number)
{
numbers.Add(num);//把接收到的号码放入numbers中
}
foreach (string n in numbers)//进行处理
{
char[] temp = n.ToCharArray();
for (int i = 3; i <= 7; i++)//把号码的3到7位用*号代替,这里应该有更好的方法,我的比较笨,希望大家可以给我建议
{
temp[i] = '*';
}
string s = new string(temp);
newnum.Add(s);//把Mask后的号码放入结果数组等待返回
}
return newnum;
}
}
}
然后生成它,得到NumMask.dll文,再建立一个winform项目,添加刚才的NumMask.dll为引用。winform组件界面如下:
ListBox1、button
我们没有使用数据库作为号码来源,而是使用了简单的定义好的号码,在实际中可以把数据库中的号码字段读取到一个ArrayList中,然后调用NumMask来处理。
ListBox1用于显示处理结果,我们看看“开始转换”按钮的代码:记得在开头添加using NumMask;
private void button1_Click(object sender, EventArgs e)
{
ArrayList al = new ArrayList();//定义al为原始的号码数组
ArrayList ab = new ArrayList();//定义ab为处理后的结果
al.Add("13888567890");//向原始号码中添加号码
al.Add("15878990987");
NumMask.Masker mk = new Masker();//实例化一个Masker
ab=mk.NumberChange(al);//向其中传入原始数据al,用ab接收结果
foreach (string nums in ab)
{
listBox1.Items.Add(nums);//输出结果
}
}







