2015-02-05 3 views
0

응용 프로그램 컨텍스트 클래스가 있고 그 중에서도 시스템 트레이에 Form1 클래스를 보내는 몇 가지 메서드가 있습니다. 어떻게 아직 시스템 트레이에, 예를 실행하는 동안 From1 이미 인스턴스화 된 클래스를 보여주기 위해 : 나는 새로운 클래스를 인스턴스화하는 경우다른 클래스 (ApplicationContext)에서 이미 인스턴스화 된 (실행중인) 클래스 Form1을 표시하는 방법

class MyApplicationContext : ApplicationContext 
{ 
    private NotifyIcon TrayIcon; 
    private ContextMenuStrip TrayIconContextMenu; 
    private ToolStripMenuItem CloseMenuItem; 

    private void TrayIcon_DoubleClick(object sender, EventArgs e) 
    { 
     // How to show Form1 hire. 
    } 
    //... 
} 



public partial class Form1 : Form 
    { 
      public Form1() 
      { 
       InitializeComponent(); 
      } 
      //... 
    } 

그것은 나에게 새로운 형태의 내 모든 실행중인 프로세스를 제공하는 것은 사라질 것입니다 예 :

private void TrayIcon_DoubleClick(object sender, EventArgs e) 
{ 
     Form1 frm = new Form1(); 
     frm.Show(); //How to show my form? 
} 

프로토 타입 패턴이 여기에서 작동하는지 확실하지 않은 경우 더 쉬운 해결책이 있습니까? 당신은 어딘가에 당신이 이벤트에 액세스 할 수있는 양식 객체를 저장해야합니다

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Windows; 

namespace DJBackupDB 
{ 
    class MyApplicationContext : ApplicationContext 
    { 
     //Component declarations 
     private NotifyIcon TrayIcon; 
     private ContextMenuStrip TrayIconContextMenu; 
     private ToolStripMenuItem CloseMenuItem; 
     public static Form1 form; 

     public MyApplicationContext() 
     { 
      Application.ApplicationExit += new EventHandler(this.OnApplicationExit); 
      InitializeComponent(); 
      TrayIcon.Visible = true; 
     } 

     private void InitializeComponent() 
     { 
      TrayIcon = new NotifyIcon(); 

      TrayIcon.BalloonTipIcon = ToolTipIcon.Info; 
      TrayIcon.BalloonTipText = 
       "I noticed that you double-clicked me! What can I do for you?"; 
      TrayIcon.BalloonTipTitle = "You called Master?"; 
      TrayIcon.Text = "My fabulous tray icon demo application"; 


      //The icon is added to the project resources. 
      //Here I assume that the name of the file is 'TrayIcon.ico' 
      TrayIcon.Icon = Properties.Resources.logo; 

      //Optional - handle doubleclicks on the icon: 
      TrayIcon.DoubleClick += TrayIcon_DoubleClick; 

      //Optional - Add a context menu to the TrayIcon: 
      TrayIconContextMenu = new ContextMenuStrip(); 
      CloseMenuItem = new ToolStripMenuItem(); 
      TrayIconContextMenu.SuspendLayout(); 

      // 
      // TrayIconContextMenu 
      // 
      this.TrayIconContextMenu.Items.AddRange(new ToolStripItem[] { 
      this.CloseMenuItem}); 
      this.TrayIconContextMenu.Name = "TrayIconContextMenu"; 
      this.TrayIconContextMenu.Size = new Size(153, 70); 
      // 
      // CloseMenuItem 
      // 
      this.CloseMenuItem.Name = "CloseMenuItem"; 
      this.CloseMenuItem.Size = new Size(152, 22); 
      this.CloseMenuItem.Text = "Close the tray icon program"; 
      this.CloseMenuItem.Click += new EventHandler(this.CloseMenuItem_Click); 

      TrayIconContextMenu.ResumeLayout(false); 
      TrayIcon.ContextMenuStrip = TrayIconContextMenu; 
     } 

     private void OnApplicationExit(object sender, EventArgs e) 
     { 
      //Cleanup so that the icon will be removed when the application is closed 
      TrayIcon.Visible = false; 
     } 

     private void TrayIcon_DoubleClick(object sender, EventArgs e) 
     { 
      form.Show(); 
     } 

     private void CloseMenuItem_Click(object sender, EventArgs e) 
     { 
      if (MessageBox.Show("Do you really want to close me?", 
        "Are you sure?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, 
        MessageBoxDefaultButton.Button2) == DialogResult.Yes) 
      { 
       Application.Exit(); 
      } 
     } 
    } 
} 

답변

1

:

내 클래스입니다. NotifyIcon 및 기타 사항과 마찬가지로 양식의 클래스 멤버 필드를 만듭니다.

private Form1 form; 

그런 다음 ApplicationContext를 만들 때 양식 필드를 Form1 개체로 설정하십시오.

그런 다음 클릭 이벤트 핸들러 내부에, 당신은 단지 호출 할 수 있습니다 :이와

this.form.Show(); 
+0

문제는 내가 MyApplicationContext 클래스에서이 개체에 액세스 할 필요가있다. – codemonkey

+0

@ D.J. MyApplicationContext를 어디에서 인스턴스화합니까? Form1에? "MyApplicationContext appContext = new MyApplicationContext (this);'그런 다음 MyApplicationContext()의 생성자를 Form1 객체를 받아 들일 수 있도록 변경한다 : public MyApplicationContext (Form1 form) {this. 양식 = 양식; }' – itsme86

+0

Form1에서 MyApplicationContext 클래스를 호출하지 않습니다. MyApplicationContext 클래스 전체를 편집했습니다. – codemonkey