设计主窗口,如下图:
[ 相关贴图 ]
主要包括五个文本框,五个标签,三个按钮,一个选择框和一个timer。
WinForm设置如下:
text:收取邮件
startPosition:CenterScreen
MaximizeBox:false
三个按钮:
最小化按钮:最小化窗口,这里就是隐藏主窗口。
取邮件按钮:实现取得POP3信箱中的邮件数量,并用信息窗口提示。
应用按钮:保存设置到注册表中
timer1的功能:完成在一定时间间隔内取POP3信箱中邮件数量。
1.托盘的实现:
从tool palette选择组件contextMenu(加两个菜单项,设置和退出)、notifyIcon(text设为:邮箱检测,图标ICON选择一个16x16的小图标即可,contextMenu设为前面加入的contextMenu1,Visible设为true) 这样一个完整的托盘程序就设好了。
2.用户提示信息的实现:
新建一窗口WinForm1,把窗体的FormBorderStyle属性设置为None(无边框模式),然后把TopMost属性(总在最上方)属性设置为True,把ShowInTaskbar属性(是否在 Windows 任务栏中显示窗体)设置为False,并在窗体上加上一文字标签,将窗体的背景设置为你想要的图片和合适的大小。最后再放上三个Timer控件,其中,timer1控制窗体滚出的动画,timer2控制窗体停留时间,timer3控制窗体的滚入动画,将它们的Interval属性设置为10,如图:
[ 相关贴图 ]
WinForm1代码如下:
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;namespace pop3{/// summary/// Summary description for WinForm1./// /summarypublic class WinForm1 : System.Windows.Forms.Form{/// summary/// Required designer variable./// /summaryprivate System.ComponentModel.IContainer components;private System.Windows.Forms.Timer timer1;private System.Windows.Forms.Timer timer2;private System.Windows.Forms.Timer timer3;private System.Windows.Forms.LinkLabel linkLabel1;public string str_num;public WinForm1(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// summary/// Clean up any resources being used./// /summaryprotected override void Dispose (bool disposing){if (disposing){if (components != null){components.Dispose();}}base.Dispose(disposing);}#region Windows Form Designer generated code/// summary/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// /summaryprivate void InitializeComponent(){this.components = new System.ComponentModel.Container();System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm1));this.timer1 = new System.Windows.Forms.Timer(this.components);this.timer2 = new System.Windows.Forms.Timer(this.components);this.timer3 = new System.Windows.Forms.Timer(this.components);this.linkLabel1 = new System.Windows.Forms.LinkLabel();this.SuspendLayout();//// timer1//this.timer1.Interval = 10;this.timer1.Tick += new System.EventHandler(this.timer1_Tick);//// timer2//this.timer2.Interval = 10;this.timer2.Tick += new System.EventHandler(this.timer2_Tick);//// timer3//this.timer3.Interval = 10;this.timer3.Tick += new System.EventHandler(this.timer3_Tick);//// linkLabel1//this.linkLabel1.AutoSize = true;this.linkLabel1.BackColor = System.Drawing.Color.WhiteSmoke;this.linkLabel1.Location = new System.Drawing.Point(56, 56);this.linkLabel1.Name = "linkLabel1";this.linkLabel1.Size = new System.Drawing.Size(79, 17);this.linkLabel1.TabIndex = 0;this.linkLabel1.TabStop = true;this.linkLabel1.Text = "您有新邮件!";//// WinForm1//this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.BackColor = System.Drawing.SystemColors.Info;this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));this.ClientSize = new System.Drawing.Size(194, 122);this.Controls.Add(this.linkLabel1);this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;this.Name = "WinForm1";this.ShowInTaskbar = false;this.Text = "信息提示";this.TopMost = true;this.Load += new System.EventHandler(this.WinForm1_Load);this.Activated += new System.EventHandler(this.WinForm1_Activated);this.ResumeLayout(false);}#endregionprivate void WinForm1_Load(object sender, System.EventArgs e){Screen[] screens = Screen.AllScreens;Screen screen = screens[0];this.Location = new Point(screen.WorkingArea.Width - 200, screen.WorkingArea.Height - 30);this.timer2.Interval = 5000;}private void ScrollUp(){if(Height 122){this.Height += 3;this.Location = new Point(this.Location.X, this.Location.Y - 3);}else{this.timer1.Enabled = false;this.timer2.Enabled = true;}}private void ScrollDown(){if(Height 3){this.Height -= 3;this.Location = new Point(this.Location.X, this.Location.Y + 3);}else{this.timer3.Enabled = false;this.Close();}}private void timer1_Tick(object sender, System.EventArgs e){ScrollUp();}private void timer2_Tick(object sender, System.EventArgs e){timer2.Enabled = false;timer3.Enabled = true;}private void timer3_Tick(object sender, System.EventArgs e){ScrollDown();}public void ScrollShow(){this.Width = 194;this.Height = 0;this.Show();this.timer1.Enabled = true;}private void WinForm1_Activated(object sender, System.EventArgs e){linkLabel1.Text="您有"+str_num+"封新邮件!";}}}
具体的实现,可以参考一下网上的一篇文章:《用VC#编写仿MSN Messager的滚动提示窗口》,已经记不清出处了,可以通过这个网址http://yousoft.hi.com.cn/article_view.asp?id=6595浏览。
3.获取邮件数量的实现,代码见下:
string str_server;if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!="")){TcpClient tcpc = new TcpClient(tB_server.Text,110);Byte[] outbytes;string input;NetworkStream ns = null;try{ns = tcpc.GetStream();StreamReader sr = new StreamReader(ns);tB_status.Text=sr.ReadLine();input = "USER " + tB_user.Text + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+sr.ReadLine();input = "PASS " + tB_pwd.Text + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+sr.ReadLine();input = "STAT" + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)str_server=sr.ReadLine();str_num="";if (str_server.StartsWith("+OK")){str_num=str_server.Split(' ')[1];} else str_num="";tB_status.Text=tB_status.Text+"rn"+"A:"+str_server;input = "QUIT" + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+"B:"+sr.ReadLine();}catch(InvalidOperationException ioe){tB_status.Text="Could not connect to mail server";}if (str_num!="") {WinForm1 form = new WinForm1();form.str_num=str_num;form.ScrollShow();}
程序与邮件服务器建立一个TCP连接,然后发送POP3的一些命令,取到服务器传回的信息,得到邮件的数量。
STAT命令的回应中有两个数字,分别表示邮件的数量和邮件的大小。一般格式是这样的+OK 数量 大小,所以只要根据服务器返回的信息,提取+OK,然后再按空格分隔得到数量。代码见上。
4.注册表的读写:
我们知道,要实现程序在开机后自己运行,可以通过写注册表HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRun实现。
另外,为了方便用户,我们把邮件服务器,用户,密码等信息存入注册表,不用每次都再输一次。
保存信息代码如下:
RegistryKey rk;Registry.CurrentUser.CreateSubKey("Software\yousoft");rk=Registry.CurrentUser.OpenSubKey("Software\yousoft",true);rk.Setvalue("server",tB_server.Text);rk.Setvalue("user",tB_user.Text);rk.Setvalue("pwd",tB_pwd.Text);rk.Setvalue("interval",tB_mins.Text);if (cB_autorun.Checked) {rk=Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run",true);rk.Setvalue("chkmail",Application.ExecutablePath);} else{rk=Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run",true);rk.Deletevalue("chkmail");}rk.Close();
启动程序时读入信息:
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/)
private void WinForm_Load(object sender, System.EventArgs e){RegistryKey rk;rk=Registry.CurrentUser.OpenSubKey("Software\yousoft",false);object srv=rk.Getvalue("server");if (srv!=null) tB_server.Text=srv.ToString();object usr=rk.Getvalue("user");if (usr!=null) tB_user.Text=usr.ToString();object pwd=rk.Getvalue("pwd");if (pwd!=null) tB_pwd.Text=pwd.ToString();object mins=rk.Getvalue("interval");if (mins!=null) tB_mins.Text=mins.ToString();rk.Close();}WinForm的完整代码如下:using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Net.Sockets;using System.IO;using System.Net;using Microsoft.Win32;namespace pop3{/// summary/// Summary description for WinForm./// /summarypublic class WinForm : System.Windows.Forms.Form{/// summary/// Required designer variable./// /summaryprivate System.ComponentModel.IContainer components;private System.Windows.Forms.Button button1;private System.Windows.Forms.Label label1;private System.Windows.Forms.Label label2;private System.Windows.Forms.Label label3;private System.Windows.Forms.TextBox tB_server;private System.Windows.Forms.TextBox tB_user;private System.Windows.Forms.TextBox tB_pwd;private System.Windows.Forms.TextBox tB_status;private System.Windows.Forms.NotifyIcon notifyIcon1;private System.Windows.Forms.ContextMenu contextMenu1;private System.Windows.Forms.MenuItem menuItem1;private System.Windows.Forms.MenuItem menuItem2;private System.Windows.Forms.MenuItem menuItem3;private bool f_open=true;private string str_num="";private System.Windows.Forms.Button button2;private System.Windows.Forms.Timer timer1;private System.Windows.Forms.TextBox tB_mins;private System.Windows.Forms.Label label4;private System.Windows.Forms.Label label5;private System.Windows.Forms.Button button3;private System.Windows.Forms.CheckBox cB_autorun;public WinForm(){//// Required for Windows Form Designer support//InitializeComponent();//// TODO: Add any constructor code after InitializeComponent call//}/// summary/// Clean up any resources being used./// /summaryprotected override void Dispose (bool disposing){if (disposing){if (components != null){components.Dispose();}}base.Dispose(disposing);}#region Windows Form Designer generated code/// summary/// Required method for Designer support - do not modify/// the contents of this method with the code editor./// /summaryprivate void InitializeComponent(){this.components = new System.ComponentModel.Container();System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(WinForm));this.button1 = new System.Windows.Forms.Button();this.label1 = new System.Windows.Forms.Label();this.label2 = new System.Windows.Forms.Label();this.label3 = new System.Windows.Forms.Label();this.tB_server = new System.Windows.Forms.TextBox();this.tB_user = new System.Windows.Forms.TextBox();this.tB_pwd = new System.Windows.Forms.TextBox();this.tB_status = new System.Windows.Forms.TextBox();this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);this.contextMenu1 = new System.Windows.Forms.ContextMenu();this.menuItem1 = new System.Windows.Forms.MenuItem();this.menuItem2 = new System.Windows.Forms.MenuItem();this.menuItem3 = new System.Windows.Forms.MenuItem();this.button2 = new System.Windows.Forms.Button();this.timer1 = new System.Windows.Forms.Timer(this.components);this.tB_mins = new System.Windows.Forms.TextBox();this.label4 = new System.Windows.Forms.Label();this.label5 = new System.Windows.Forms.Label();this.button3 = new System.Windows.Forms.Button();this.cB_autorun = new System.Windows.Forms.CheckBox();this.SuspendLayout();//// button1//this.button1.Location = new System.Drawing.Point(152, 176);this.button1.Name = "button1";this.button1.TabIndex = 0;this.button1.Text = "取邮件";this.button1.Click += new System.EventHandler(this.button1_Click);//// label1//this.label1.Location = new System.Drawing.Point(24, 32);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(80, 23);this.label1.TabIndex = 1;this.label1.Text = "邮件服务器:";//// label2//this.label2.Location = new System.Drawing.Point(48, 64);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(56, 23);this.label2.TabIndex = 2;this.label2.Text = "用户名:";//// label3//this.label3.Location = new System.Drawing.Point(58, 96);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(48, 23);this.label3.TabIndex = 3;this.label3.Text = "密码:";//// tB_server//this.tB_server.Location = new System.Drawing.Point(112, 24);this.tB_server.Name = "tB_server";this.tB_server.Size = new System.Drawing.Size(176, 21);this.tB_server.TabIndex = 4;this.tB_server.Text = "";this.tB_server.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);//// tB_user//this.tB_user.Location = new System.Drawing.Point(112, 60);this.tB_user.Name = "tB_user";this.tB_user.Size = new System.Drawing.Size(176, 21);this.tB_user.TabIndex = 5;this.tB_user.Text = "";this.tB_user.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);//// tB_pwd//this.tB_pwd.Location = new System.Drawing.Point(112, 91);this.tB_pwd.Name = "tB_pwd";this.tB_pwd.PasswordChar = '*';this.tB_pwd.Size = new System.Drawing.Size(176, 21);this.tB_pwd.TabIndex = 6;this.tB_pwd.Text = "";this.tB_pwd.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);//// tB_status//this.tB_status.Location = new System.Drawing.Point(16, 208);this.tB_status.Multiline = true;this.tB_status.Name = "tB_status";this.tB_status.Size = new System.Drawing.Size(328, 80);this.tB_status.TabIndex = 7;this.tB_status.Text = "";//// notifyIcon1//this.notifyIcon1.ContextMenu = this.contextMenu1;this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));this.notifyIcon1.Text = "邮件检测";this.notifyIcon1.Visible = true;//// contextMenu1//this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {this.menuItem1,this.menuItem2,this.menuItem3});//// menuItem1//this.menuItem1.Index = 0;this.menuItem1.Text = "设置&S";this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);//// menuItem2//this.menuItem2.Index = 1;this.menuItem2.Text = "-";//// menuItem3//this.menuItem3.Index = 2;this.menuItem3.Text = "退出&Q";this.menuItem3.Click += new System.EventHandler(this.menuItem3_Click);//// button2//this.button2.Location = new System.Drawing.Point(48, 176);this.button2.Name = "button2";this.button2.TabIndex = 8;this.button2.Text = "最小化";this.button2.Click += new System.EventHandler(this.button2_Click);//// timer1//this.timer1.Interval = 5000;this.timer1.Tick += new System.EventHandler(this.timer1_Tick);//// tB_mins//this.tB_mins.Location = new System.Drawing.Point(112, 124);this.tB_mins.Name = "tB_mins";this.tB_mins.Size = new System.Drawing.Size(144, 21);this.tB_mins.TabIndex = 10;this.tB_mins.Text = "20000";this.tB_mins.TextChanged += new System.EventHandler(this.tB_pwd_TextChanged);//// label4//this.label4.Location = new System.Drawing.Point(32, 128);this.label4.Name = "label4";this.label4.Size = new System.Drawing.Size(72, 23);this.label4.TabIndex = 9;this.label4.Text = "时间间隔:";//// label5//this.label5.Location = new System.Drawing.Point(264, 129);this.label5.Name = "label5";this.label5.Size = new System.Drawing.Size(40, 16);this.label5.TabIndex = 11;this.label5.Text = "毫秒";//// button3//this.button3.Location = new System.Drawing.Point(264, 176);this.button3.Name = "button3";this.button3.TabIndex = 12;this.button3.Text = "应用";this.button3.Click += new System.EventHandler(this.button3_Click);//// cB_autorun//this.cB_autorun.Location = new System.Drawing.Point(112, 152);this.cB_autorun.Name = "cB_autorun";this.cB_autorun.Size = new System.Drawing.Size(160, 24);this.cB_autorun.TabIndex = 13;this.cB_autorun.Text = "启动时自动执行";//// WinForm//this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);this.ClientSize = new System.Drawing.Size(368, 301);this.Controls.Add(this.cB_autorun);this.Controls.Add(this.button3);this.Controls.Add(this.label5);this.Controls.Add(this.tB_mins);this.Controls.Add(this.label4);this.Controls.Add(this.button2);this.Controls.Add(this.tB_status);this.Controls.Add(this.tB_pwd);this.Controls.Add(this.tB_user);this.Controls.Add(this.tB_server);this.Controls.Add(this.label3);this.Controls.Add(this.label2);this.Controls.Add(this.label1);this.Controls.Add(this.button1);this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));this.MaximizeBox = false;this.Name = "WinForm";this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;this.Text = "收取邮件";this.Load += new System.EventHandler(this.WinForm_Load);this.Activated += new System.EventHandler(this.WinForm_Activated);this.ResumeLayout(false);}#endregion/// summary/// The main entry point for the application./// /summary[STAThread]static void Main(){Application.Run(new WinForm());}private void button1_Click(object sender, System.EventArgs e){string str_server;if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!="")){TcpClient tcpc = new TcpClient(tB_server.Text,110);Byte[] outbytes;string input;NetworkStream ns = null;try{ns = tcpc.GetStream();StreamReader sr = new StreamReader(ns);tB_status.Text=sr.ReadLine();input = "USER " + tB_user.Text + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+sr.ReadLine();input = "PASS " + tB_pwd.Text + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+sr.ReadLine();input = "STAT" + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)str_server=sr.ReadLine();str_num="";if (str_server.StartsWith("+OK")){str_num=str_server.Split(' ')[1];} else str_num="";tB_status.Text=tB_status.Text+"rn"+str_server;input = "QUIT" + "rn";outbytes = System.Text.Encoding.ASCII.GetBytes(input.ToCharArray());ns.Write(outbytes,0,outbytes.Length)tB_status.Text=tB_status.Text+"rn"+sr.ReadLine();}catch(InvalidOperationException ioe){tB_status.Text="Could not connect to mail server";}if (str_num!="") {WinForm1 form = new WinForm1();form.str_num=str_num;form.ScrollShow();}}}private void WinForm_Activated(object sender, System.EventArgs e){if (f_open) {this.Hide();f_open=false;}}private void menuItem3_Click(object sender, System.EventArgs e){this.Close();}private void menuItem1_Click(object sender, System.EventArgs e){this.Show();this.Focus();}private void button2_Click(object sender, System.EventArgs e){this.Hide();}private void timer1_Tick(object sender, System.EventArgs e){button1_Click(sender,e);}private void tB_pwd_TextChanged(object sender, System.EventArgs e){timer1.Enabled=false;if ((tB_server.Text!="") && (tB_user.Text!="") && (tB_pwd.Text!="")){timer1.Interval=System.Convert.ToInt32(tB_mins.Text);timer1.Enabled=true;} else{timer1.Enabled=false;}}private void button3_Click(object sender, System.EventArgs e){RegistryKey rk;Registry.CurrentUser.CreateSubKey("Software\yousoft");rk=Registry.CurrentUser.OpenSubKey("Software\yousoft",true);rk.Setvalue("server",tB_server.Text);rk.Setvalue("user",tB_user.Text);rk.Setvalue("pwd",tB_pwd.Text);rk.Setvalue("interval",tB_mins.Text);if (cB_autorun.Checked) {rk=Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run",true);rk.Setvalue("chkmail",Application.ExecutablePath);} else{rk=Registry.CurrentUser.OpenSubKey("Software\Microsoft\Windows\CurrentVersion\Run",true);rk.Deletevalue("chkmail");}rk.Close();}private void WinForm_Load(object sender, System.EventArgs e){RegistryKey rk;rk=Registry.CurrentUser.OpenSubKey("Software\yousoft",false);object srv=rk.Getvalue("server");if (srv!=null) tB_server.Text=srv.ToString();object usr=rk.Getvalue("user");if (usr!=null) tB_user.Text=usr.ToString();object pwd=rk.Getvalue("pwd");if (pwd!=null) tB_pwd.Text=pwd.ToString();object mins=rk.Getvalue("interval");if (mins!=null) tB_mins.Text=mins.ToString();rk.Close();}}}
按F9编译程序并运行。如图:
[ 相关贴图 ]
[ 相关贴图 ]
[ 相关贴图 ]
四.小结:
本文向读者介绍了如何运用Borland C# builder实现POP3信箱的监视,其中大量运用了Windows Forms编程和.Net框架下的网络编程的原理。通过本文,希望能使读者对.Net中的Borland C# builder有一个感性的认识,同时对Windows Forms编程和其中的网络编程也有个大致的了解。如果有兴趣,可以到悠游在线(网址:http://yousoft.hi.com.cn)下载全部的源代码或与本人交流。