Chủ Nhật, 20 tháng 10, 2013

Single-instance trong lập trình C#

1) Form được mở một lần
// Đối với Normal Form
private void titleCuocThi_Click(object sender, EventArgs e)
{
     foreach (Form f in Application.OpenForms)
     {
           if (f is frmCuocThi)
           {
                f.WindowState = FormWindowState.Normal;
                f.Focus();
                return;
           }
     }
     frmCuocThi frm = new frmCuocThi();
     frm.Show();
}

// Đối với Child Form
private void titleCuocThi_Click(object sender, EventArgs e)
{
     foreach (Form child in this.MdiChildren)
     {
           if (child.GetType() == typeof(frmCuocThi))
           {
                chidl.WindowState = FormWindowState.Normal;
                chidl.Focus();
                return;
           }
     }
     frmCuocThi frm = new frmCuocThi();
frm.MdiParent = this;
     frm.Show();
}
2) Ứng dụng Single-instance

Tóm lại là thêm 3 lớp này vào Project
Download
Trong frmMain thì thêm các phương thức và các sự kiện sau:
 protected override void WndProc(ref Message message)
 {
            if (message.Msg == SingleInstance.WM_SHOWFIRSTINSTANCE)
            {
                ShowWindow();
            }
            base.WndProc(ref message);
}

public void ShowWindow()
 {
            //if (minimizedToTray)
            //{
            //    notifyIcon.Visible = false;
            //    this.Show();
            //    this.WindowState = FormWindowState.Normal;
            //    minimizedToTray = false;
            //}
            //else
            {
                WinApi.ShowToFront(this.Handle);
            }
}

//private void btnMinToTray_Click(object sender, EventArgs e)
 //{
        //    // Tie this function to a button on your main form that will minimize your
        //    // application to the notification icon area (aka system tray).
        //    MinimizeToTray();
 //}
        
//void MinimizeToTray()
//{
        //    notifyIcon = new NotifyIcon();
        //    //notifyIcon.Click += new EventHandler(NotifyIconClick);
        //    notifyIcon.DoubleClick += new EventHandler(NotifyIconClick);
        //    notifyIcon.Icon = this.Icon;
        //    notifyIcon.Text = ProgramInfo.AssemblyTitle;
        //    notifyIcon.Visible = true;
        //    this.WindowState = FormWindowState.Minimized;
        //    this.Hide();
        //    minimizedToTray = true;
//}
        
//public void ShowWindow()
//{
        //    if (minimizedToTray)
        //    {
        //        notifyIcon.Visible = false;
        //        this.Show();
        //        this.WindowState = FormWindowState.Normal;
        //        minimizedToTray = false;
        //    }
        //    else
        //    {
        //        WinApi.ShowToFront(this.Handle);
        //    }
//}

//void NotifyIconClick(Object sender, System.EventArgs e)
 //{
        //    ShowWindow();
 //}

2 nhận xét:

  1. static class Program
    {
    ///
    /// The main entry point for the application.
    ///
    [STAThread]
    static void Main()
    {
    //Application.EnableVisualStyles();
    //Application.SetCompatibleTextRenderingDefault(false);
    //Application.Run(new Form1());
    if (!SingleInstance.Start())
    {
    SingleInstance.ShowFirstInstance();
    return;
    }

    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    try
    {
    frmMain mainForm = new frmMain();
    Application.Run(mainForm);
    }
    catch (Exception e)
    {
    MessageBox.Show(e.Message);
    }
    SingleInstance.Stop();
    }
    }

    Trả lờiXóa