近日看见一篇文章《用Visual C#编写仿MSN Messager的滚动提示窗口》,原文地址(原文含源码)
http://www.microsoft.com/China/Community/program/originalarticles/TechDoc/vcsharpmsn.mspx,按照文章的说明,的确可以实现。原文最后通过一个按钮的点击来引导程序:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 form = new Form2();
form.HeightMax = 120;//窗体滚动的高度
form.WidthMax = 148;//窗体滚动的宽度
form.ScrollShow();
}但我在程序里通过定时器timer定时地触发引导却不可以。我先声明System.Timers.Timer t = new System.Timers.Timer(10000);然后public Form1()
{
InitializeComponent();
t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
t.AutoReset = true;
t.Enabled = true;
}
public void theout(object source, System.Timers.ElapsedEventArgs e) 

                       Form2 form = new Form2();
               form.HeightMax = 120;//窗体滚动的高度
                 form.WidthMax = 148;//窗体滚动的宽度
                 form.ScrollShow();
                },这样发现程序运行时总是不正常,窗体一弹出就自动死掉了。 无语凝咽,迷惑中……

解决方案 »

  1.   

    这些代码写在load()里
    t.Elapsed += new System.Timers.ElapsedEventHandler(theout); 
    t.AutoReset = true; 
    t.Enabled = true; 
      

  2.   


    谢谢【mlqxj35674 】,但经测试,问题依旧。弹出的窗口一出就死。
      

  3.   

    这里好像没什么问题,问题可能出现在Form2的代码
      

  4.   

    这是因为每次定时器被触发都运行在一个独立的thread中
    无法操作主thread的ui
    一般是定义一个委托,在定时器中使用委托来处理
    例:
    public partial class Form1 : Form
        {
            delegate void FormShowDelegate();
            FormShowDelegate formShowDelegate;
            public Form1()
            {
                InitializeComponent();
                formShowDelegate = new FormShowDelegate(showWindow);
                System.Timers.Timer t = new System.Timers.Timer(100);
                t.Elapsed += new System.Timers.ElapsedEventHandler(theout);
                t.AutoReset = false;
                t.Enabled = true; 
            }        void showWindow() {
                Form form = new Form();
                form.Show();
            }        private void button1_Click(object sender, EventArgs e)
            {
                showWindow();        }
                    public void theout(object source, System.Timers.ElapsedEventArgs e)
            {
                this.Invoke(formShowDelegate);
            }
            
        }
      

  5.   

    from2,完全按照原文所说制造,应该也没问题的。Form2源码:using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using System.Data.SqlClient;namespace i6_tip
    {
    /// <summary>
    /// Form2 的摘要说明。
    /// </summary>
    public class Form2 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.Timer timer1;
    private System.Windows.Forms.Timer timer2;
    private System.Windows.Forms.Timer timer3;
    private System.ComponentModel.IContainer components;
    public int StayTime = 5000;
    private int heightMax, widthMax;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;

    public int HeightMax
    {
    set
    {
    heightMax = value;
    }
    get
    {
    return heightMax;
    }
    } public int WidthMax
    {
    set
    {
    widthMax = value;
    }
    get
    {
    return widthMax;
    }
    }
    public void ScrollShow()
    {
    this.Width = widthMax;
    this.Height = 0;
    this.Show();
    this.timer1.Enabled = true;
    }
    private void ScrollUp()
    {
    if(Height < heightMax)
    {
    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();
    }
    }
    public Form2()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if(components != null)
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.components = new System.ComponentModel.Container();
    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.label1 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    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);
    // 
    // label1
    // 
    this.label1.BackColor = System.Drawing.Color.Red;
    this.label1.Location = new System.Drawing.Point(0, 0);
    this.label1.Name = "label1";
    this.label1.Size = new System.Drawing.Size(288, 23);
    this.label1.TabIndex = 0;
    this.label1.Text = "label1";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(0, 32);
    this.label2.Name = "label2";
    this.label2.Size = new System.Drawing.Size(256, 23);
    this.label2.TabIndex = 1;
    this.label2.Text = "label2";
    // 
    // Form2
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(192)), ((System.Byte)(0)));
    this.ClientSize = new System.Drawing.Size(280, 72);
    this.Controls.Add(this.label2);
    this.Controls.Add(this.label1);
    this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
    this.Name = "Form2";
    this.ShowInTaskbar = false;
    this.Text = "Form2";
    this.TopMost = true;
    this.Load += new System.EventHandler(this.Form2_Load);
    this.ResumeLayout(false);
    }
    #endregion 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(); }
    private void Form2_Load(object sender, System.EventArgs e)
    {
    Screen[] screens = Screen.AllScreens;
    Screen screen = screens[0];//获取屏幕变量
    this.Location = new Point(screen.WorkingArea.Width - widthMax - 20, screen.WorkingArea.Height - 34);//WorkingArea为Windows桌面的工作区
    this.timer2.Interval = StayTime;
    this.getdata(); }
    public void getdata()
    {
    string purs="select fg_approveobj_master.*,fg_approveobj_step.id as appobjectstepid ,fg_approveobj_step.flow_step_no,fg_approveobj_step.step_id,fg_approveobj_step.check_flag   from fg_approveobj_master,fg_approveobj_step,fg_approveobj_step_user  where fg_approveobj_master.id=fg_approveobj_step.master_id    and fg_approveobj_step.id=fg_approveobj_step_user.appstepid    and fg_approveobj_step_user.userid='00040068'   and fg_approveobj_step.check_flag='1' and fg_approveobj_master.flow_flag!='3' and fg_approveobj_master.op_type='PURS' order by fg_approveobj_master.id";
    string preq="select fg_approveobj_master.*,fg_approveobj_step.id as appobjectstepid ,fg_approveobj_step.flow_step_no,fg_approveobj_step.step_id,fg_approveobj_step.check_flag   from fg_approveobj_master,fg_approveobj_step,fg_approveobj_step_user  where fg_approveobj_master.id=fg_approveobj_step.master_id    and fg_approveobj_step.id=fg_approveobj_step_user.appstepid    and fg_approveobj_step_user.userid='95110009 '   and fg_approveobj_step.check_flag='1' and fg_approveobj_master.flow_flag!='3' and fg_approveobj_master.op_type='PREQ' order by fg_approveobj_master.id";
        
    string constr="server=SMART2008;database=ng0001;uid=sa;pwd=sa";
    SqlConnection con=new SqlConnection(constr);
    DataSet ds=new DataSet ();
    SqlDataAdapter da=new SqlDataAdapter (purs,con);
    da.Fill (ds,"purs");
    SqlDataAdapter da2=new SqlDataAdapter (preq,con);
    da2.Fill (ds,"preq");
    int ncount=ds.Tables["purs"].Rows.Count;
    int ncount2=ds.Tables["preq"].Rows.Count; if(ncount>0)

    {
    this.label1 .Text="您有"+ncount+"条采购订单等待审批!";

    }
    if(ncount2>0)

    {this.label2 .Text="您有"+ncount2+"条请购单等待审批!";}
    }
    }
    }