在c#中的第一个winform中检测第二个winform窗口状态 [英] To Detect 2nd winform window state in 1st winform in c#

查看:193
本文介绍了在c#中的第一个winform中检测第二个winform窗口状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个胜利形式,我使用了最小化的第二个胜利形式



 这个 .WindowState = FormWindowState.Minimized 
this .ShowInTaskbar = true ;

并且我在第一个winform中有计时器,每17秒设置一次,当计时器的下一个周期在第一个winform中击中事件时,我试图获得第二个win-form窗口状态

 frmPopup objPopup =  new  frmPopup( false ); 
MessageBox.Show( + objPopup.WindowState + );







Form_2_Code:



  private   void  Form2_SizeChanged(对象发​​件人,EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
isMinimized = 1 ;
}
}
私有 void Minimize_Click( object sender,EventArgs e)
{
this .Resize - = new EventHandler(Form2_SizeChanged);
this .DialogResult = System.Windows.Forms.DialogResult.Cancel;
this .WindowState = FormWindowState.Minimized;
this .ShowInTaskbar = true ;
this .Resize + = new EventHandler(Form2_SizeChanged);
}

private void btnOK_Click( object sender,EventArgs e)
{
Process [] processes = Process.GetProcessesByName( XXXX);
if (processes.Length!= 0
{
lblWarning.Visible = true ;
}
else
.DialogResult = System.Windows。 Forms.DialogResult.OK;
}

private void btnCancel_Click( object sender,EventArgs e)
{
this .DialogResult = System.Windows.Forms.DialogResult。取消;
}



Form_1_code:



frmPopup objPopup = new frmPopup(false) ; //全球声明



 私人  void  timer3_Tick( object  sender,EventArgs e)
{
Process [] process1 = Process.GetProcessesByName( XXXX);
int iWindowstate = objPopup.isMinimized; // objPopup是Form2的对象,在Form1中声明为全局
if ((iWindowstate == 0 && process1.Length == 0 )||(iWindowstate == 1 && process1.Length == 0 ))
{
if (objPopup.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
// dialog.Result.ok
}
}
timer3.Enabled = true ;
}





我的问题:如果我最小化第二个winform我需要被放置在任务栏中尽管我可以能够获得第二个winform的windowstate我需要将第二个winform放置在任务栏中,而不是在最小化状态的systemsTray中但它消失了

解决方案

Form2的Minimize_Click()方法设置它 DialogResult 值。根据链接中的备注部分,这样做会隐藏使用 ShowDialog 方法。



但是,如果有引用,隐藏表格仍应可访问。





在Form1的Timer.Tick事件处理程序的第一行代码上设置一个断点。

它真的没有被调用或做到了,但是没有执行你期望的动作?

如果没有被调用:是否有一些代码如

 timer3.Tick  -  = WhateverMethod;  //   
timer3.Stop(); //
timer3.Enabled = false ; //
timer3 = new Timer(); // 初始化

第一个从活动中取消订阅您的方法所以它不会被调用。

第2行和第3行完全禁用了计时器。因此事件甚至不会触发。

第4行将timer3引用设置为新的Timer对象。即使不应该用旧物体发动它的事件,我的眼睛(或鼻子)中的代码味道也要更详细地检查。

[/ Edit]


I have two win form's,i have minimized 2nd win form using

this.WindowState = FormWindowState.Minimized
this.ShowInTaskbar = true;

and i have timer in 1st winform which is set to fire for every 17s and when next cycle of timer hits the event in 1st winform i am trying to obtain 2nd win-form window state using

frmPopup objPopup = new frmPopup(false);
MessageBox.Show(""+objPopup.WindowState+"");




Form_2_Code:

 private void Form2_SizeChanged(object sender, EventArgs e)
    {
        if (WindowState == FormWindowState.Minimized)
        {
            isMinimized = 1;
        }
    }
private void Minimize_Click(object sender, EventArgs e)
    {
            this.Resize -= new EventHandler(Form2_SizeChanged);
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.WindowState = FormWindowState.Minimized;
            this.ShowInTaskbar = true;
            this.Resize += new EventHandler(Form2_SizeChanged);
   }

  private void btnOK_Click(object sender, EventArgs e)
    {
        Process[] processes = Process.GetProcessesByName("XXXX");
        if (processes.Length != 0)
        {
            lblWarning.Visible = true;
        }
        else
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
    }

    private void btnCancel_Click(object sender, EventArgs e)
    {
        this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
    }


Form_1_code :

frmPopup objPopup = new frmPopup(false); // global declaration

       private void timer3_Tick(object sender, EventArgs e)
       {
          Process[] process1 = Process.GetProcessesByName("XXXX");
          int iWindowstate = objPopup.isMinimized; // objPopup is object for Form2 which is declared as global in Form1
           if ((iWindowstate == 0 && process1.Length == 0) || (iWindowstate == 1 && process1.Length == 0))
           {
            if (objPopup.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
               //dialog.Result.ok
             }
          }
timer3.Enabled = true;
       }



My Question : if i minimize 2nd winform i need to be placed in taskbar eventhough i can able to get windowstate of 2nd winform i need to 2nd winform to be placed in taskbar not in systemsTray in minimized state but its disappears

解决方案

Form2's Minimize_Click() method sets its DialogResult value. According to the "Remarks" section in the link, doing so hides forms that have been shown using the ShowDialog method.

However, the hidden form should still be accessible if there is a reference to it.

[Edit]
Set a breakpoint onto the first line of code in Form1's Timer.Tick event handler.
Does it really not get called or does it, but doesn't perform the action you expect?
If it doesn't get called: Is there some code like

timer3.Tick -= WhateverMethod;  // or
timer3.Stop();                  // or
timer3.Enabled = false;         // or
timer3 = new Timer();           // other than the initialization

The first one unsubscribes your method from the event so it doesn't get called.
Lines 2 and 3 disable the timer altogether. So the event doesn't even fire.
Line 4 would set the 'timer3' reference to a new Timer object. Even though that should not intervene with the old object firing its event, it would be a "code smell" in my eyes (or nose) to inspect in more detail.
[/Edit]


这篇关于在c#中的第一个winform中检测第二个winform窗口状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆