EventEmail

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace EventEmail
{
    public class MailData
    {
        public readonly string from, to, subject, body;
        public MailData(string from, string to, string subject, string body)
        {
            this.from = from;
            this.to = to;
            this.subject = subject;
            this.body = body;
        }
    }

    public class Fax
    {
        private TextBox _tBox;
        public Fax(MailManager mm, TextBox tBox)
        {
            mm.MailMsgEvent += new MailMsgEventHandler(FaxMsg);
            _tBox = tBox;
        }

        private void FaxMsg(Object sender, MailData e)
        {
            _tBox.Text += string.Format("消息到传真:{4}来自:{0}{4}发到:{1}{4}主题:{2}{4}内容:{3}{4}{4}", e.from, e.to, e.subject, e.body, Environment.NewLine);
        }

    }

}

------------------------------------

using System;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.Text;
using System.Drawing;

////委托类似于类,当你确定要处理一件事,但CLR不能确定处理方法时,可以考虑用委托。委托更多的是在事件中的应用。没有委托就没有事件。触发事件的效果就是靠委托来实现的,事件就是委托,但委托不是事件,它包含事件,但是也有更大的用处(线程..etc) , 你调用的每一个事件其实都是在调用一个委托,委托是用来封装方法, 也就是将方法作为参数
namespace EventEmail
{
    public partial class Form1 : Form
    {
        private Fax fax = null;
        //private CallPhone cell = null;
        private MailManager mm = null;
        public Form1()
        {
            InitializeComponent();
            mm = new MailManager();
            fax = new Fax(mm, txtReceiver);
            //cell = new CallPhone(mm, txtReceiver);
        }
        private void btnSend_Click(object sender, EventArgs e)
        {
             mm.SimulateArrivingMsg(txtFrom.Text, txtTo.Text, txtSubject.Text, txtBody.Text);//Execute delegate

            //if (MessageBox.Show("是否要预览打印文档", "打印预览", MessageBoxButtons.YesNo) == DialogResult.Yes)
            //{                //开启操作系统的防锯齿功能       
            //    this.printPreviewDialog1.UseAntiAlias = true;
            //    //设置要预览的文档           
            //    this.printPreviewDialog1.Document = this.printDocument1;                //打开预览窗口   
            //    printPreviewDialog1.ShowDialog();
            //}
            //else
            //{
            //    //调用Print方法直接打印文档      
            //    //this.printDocument1.Print();      
            //}

        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            //通过GDI+绘制打印文档       
            e.Graphics.DrawString("蝶恋花", new Font("宋体", 15), Brushes.Black, 350, 80);
            e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 185, 720, 185);
            e.Graphics.DrawString("伫倚危楼风细细,望极春愁,黯黯生天际。", new Font("宋体", 12), Brushes.Black, 110, 195);
            e.Graphics.DrawString("草色烟光残照里,无言谁会凭阑意。", new Font("宋体", 12), Brushes.Black, 110, 220);
            e.Graphics.DrawString("拟把疏狂图一醉,对酒当歌,强乐还无味。", new Font("宋体", 12), Brushes.Black, 110, 245);
            e.Graphics.DrawString("衣带渐宽终不悔。为伊消得人憔悴。", new Font("宋体", 12), Brushes.Black, 110, 270);
            e.Graphics.DrawLine(new Pen(Color.Black, (float)3.00), 100, 300, 720, 300);
        }


        private void btnClear_Click(object sender, EventArgs e)
        {
            this.txtReceiver.Text = "";
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("Let's go ~!");
        }
    }

    public delegate void MailMsgEventHandler(object sender, MailData e);

    public class MailManager
    {
        public event MailMsgEventHandler MailMsgEvent;
        //protected virtual void OnMailMsg(MailData e)
        //{
        //    if (this.MailMsgEvent != null)
        //    {
        //        MailMsgEvent(this, e);
        //    }
        //}

        public void SimulateArrivingMsg(string from, string to, string subject, string body)
        {
            MailData e = new MailData(from, to, subject, body);
            MailMsgEvent(this, e);
        }
    }


}

posted on 2011-11-15 22:43  breakpoint  阅读(154)  评论(0编辑  收藏  举报

导航