locked
How to know if the form is Active or not ? RRS feed

  • Question

  • Hi, I want to know if I form is Active i.e. on top of all or not. If it is behind something else I want to perform a command else if it is on top then also perform some other task. Which Event to handle that can help me know the above.
    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 9:16 AM

All replies

  • Check form.active property.
    Wednesday, October 5, 2011 9:27 AM
  • Zain_Ali, Form doesn't ahve any Active property. I use Win Froms and 3.5.
    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 9:41 AM
  • you can use Form.ActiveForm == this to determine if form is active. also, you can use Activated and Deactivated events for triggering when form is activated or Deactivated! if you need active your form by code you can use Activate() method!
    Any fool can know. The point is to understand.(Albert Einstein)
    • Proposed as answer by Helen Zhou Monday, October 24, 2011 8:43 AM
    Wednesday, October 5, 2011 9:44 AM
  • hi

    are you talking about mdiparent and mdichild forms open same time?


    Best Regards...Please mark as answer if my post is helpful
    Wednesday, October 5, 2011 10:04 AM
  • In my app I have a Form.  App is not a MDI. When our application is running, the window might be hidden completely or partly behind other windows like IE, Notepad, etc or it will be visible to the user properly. I want to know if my window is visible clearly or not. When my window is hidden behind any window (IE, Notepad, etc) I want the app to fire an event via which I can perform the activities I want to if it is visible completely or hidden even partly or completely.

    I tried using Fomr_Leave, Deactivate but nothing helps me know that the form is hidden behind other windows or visible to the user properly. In these events I tried properties like Visible, TopLevel, TopMost, IsAccessible. Result of all these properties remain same regardless of my form being on top of all windows or behind IE.

    I hope now I have made my question very clear to all of you. If still its not clear I will provide more explaination and details.


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 10:18 AM
  • Check here, this might help you out.

    And this here (to get the topmost form).


    Mitja
    • Edited by Mitja Bonca Wednesday, October 5, 2011 10:25 AM
    Wednesday, October 5, 2011 10:23 AM
  • you can write below code when you want to your form is showing in top of the Z-Order!

    TopMost = true;
    Application.DoEvents();
    TopMost = false;
    

     

     


    Any fool can know. The point is to understand.(Albert Einstein)
    Wednesday, October 5, 2011 10:25 AM
  • To find out if the application is focused:

       Public Declare Function GetForegroundWindow Lib "user32.dll" () As IntPtr
    
       'Note: Return value matches type of Process.Id. Actually it's a DWORD (UInt32)
       Public Declare Function GetWindowThreadProcessId Lib "user32.dll" _
          (ByVal hwnd As IntPtr, ByRef ProcessID As UInt32) As Integer
    
       Public Shared Function ProcessHasFocus() As Boolean
    
          Dim ProcessID As UInt32
          Dim hwnd As IntPtr
    
          hwnd = GetForegroundWindow
          GetWindowThreadProcessId(hwnd, ProcessID)
    
          Return ProcessID = Process.GetCurrentProcess.Id
    
       End Function
    

     

    To be notified about application activation:

       Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
          MyBase.WndProc(m)
    
          Const WM_ACTIVATEAPP As Integer = &H1C
    
          If m.Msg = WM_ACTIVATEAPP Then
             Dim activated As Boolean = m.WParam.ToInt32 <> 0
    
             If activated Then
                Debug.Print("activated")
             Else
                Debug.Print("deactivated")
             End If
          End If
    
       End Sub
    



    Armin
    Wednesday, October 5, 2011 11:11 AM
  • Hi Mitja,

       So you are telling is, use GetDesktopWindow() and from that I can know which window is on top. This will give me the list of all windows open in Z order. But this wont be able to tell me if the window(x) is visible completely to the user or not. Suppose, I get results IE1, IE2, MYForm, VS. So VS is the last and IE1 the first. IE2 ia partly covered by IE1. But MyForm is sitting next to IE2 and visible completely like IE1. I can't ge tthat info to know though MyForm is 3rd in the list but is visible completely to the user.

    Also couldn't find the class name and title name :

    Dim WinClassBuf As String = "", WinTitleBuf As String = ""

    that is originally written as WinClassBuf as String * 255, WinTitleBuf As String * 255


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 11:30 AM
  • You shouldn't care about if it's completely or partially visible or not visible at all. You can not even be sure that it is completely visible if your application has the focus because it could be minimized or windows are (partially) outside the screen bounds.

    What you can know is if the application is focused or not, and perform actions depending on the activation state. (see my code below). That's how other applications do it also. For example, if I get a text message via Skype, it beeps only if it's not focused. It doesn't matter if it's (partially) visible.


    Armin
    Wednesday, October 5, 2011 11:50 AM
  • Thanks Armin Zingler,

       I got your point and also your explaination of Skype.

    I implemented the code in my C# UpdateLayeredWindow application. I have 2 sub-forms (Implemented a Custom TextBox in a form) in my Form as instance variables which I wanted to set TopMost = false if the app is behind any other app else my thoose 2 forms used to be on top always though my app is not there.

    I have the 2 textbox forms and 2 buttons and a checkbox in my form. After implementing your above code, I could set the 2 textboxes to TopMost = false when activated = false in WndProc. But with this, I can't access my buttons i.e. can't click on any button or checkbox. And also the app takes time to start up. Any idea how could I solve it. The WndProc code is :

            protected override void WndProc(ref Message m)
            {
                base.WndProc(ref m);
                
                const int WM_ACTIVATEAPP = 0x001C;  // &H1C;
                bool activated = false;
    
                // If cancel btn is clicked then don't perform the process
                if (this.canceled == false)
                {
                    if (m.Msg == WM_ACTIVATEAPP)
                        activated = m.WParam.ToInt32() != 0;
    
                    if (activated)
                        Console.WriteLine("Activated");
                    else
                    {
                        //Console.WriteLine("DeActivated");
                        frmR_TB.TopMost = false;
                        if (frmR_TB2 != null)
                            frmR_TB2.TopMost = false;
                    }
                }
            }
    

    If you have any idea, suggestion please help em out.

     

     


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 12:31 PM
  • Maybe you just want to set the main form as the owner of the sub forms (by specifying the main form as the owner in the .Show command)? In this case, the sub forms are "top-most" to the application only, and only if it's in the foreground. If you minimize ths main form, the sub forms are minimized automatically, too. From your description I'm still unsure if this is the goal.

    Long story short, could you please post a screenshot of the situation(s) that you are not satisfied with? And add some words what does not fit in these screenshots? Maybe both, when it's activated and when it's not.


    Armin
    Wednesday, October 5, 2011 1:11 PM
  •  

    Armin,

       I have taken screenshots and provided required comments. Screenshots of both without implementing WndProc & with implementing WndProc has been shown. You can see here.

      My app can't get minimized so that's not an issue. When the Cancel btn is clicked I set a canceled flag to true. This is how I have set my sub-form :

                frmR_TB2 = new FormR_TB(pswdTxt1.Size, pswdTxt1);
                
                frmR_TB2.Location = PointToScreen(pswdTxt1.Location);
                frmR_TB2.TB.PasswordChar = '*';
                frmR_TB2.BitmapTB = global::UVC_NoConnectLost.Properties.Resources.pass_bg;
                frmR_TB2.TB.BackgroundImage = global::UVC_NoConnectLost.Properties.Resources.pass_bg;
                frmR_TB2.TB.BackgroundImageLayout = ImageLayout.Stretch;
                frmR_TB2.TB.BorderColor = Color.FromArgb(178, 188, 187);
                frmR_TB2.TB.ForeColor = Color.FromArgb(167, 169, 168);
                frmR_TB2.TB.Leave += new EventHandler(R_TB2_Leave);
                frmR_TB2.TopMost = true;
                
                frmR_TB.Show();
                frmR_TB2.Show(); 
    
    AND on clicking Cancel btn :
    
            private void CancelClicked()
            {
                canceled = true;
                CloseMe();
                return;
            }
    
    I tried to debug, but can't get the Cancen btn fired up. Without WndProc also I tried to debug and it worked properly just the both textboxes are seen in the VS :).


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 1:51 PM
  • Thanks for providing the screenshots, but I'm sorry, I can watch them now. (I would have to agree to any terms of use etc again; I do not want to do this at the momemt) I thought you'd just insert them here.

    Armin
    Wednesday, October 5, 2011 2:36 PM
  • There you go, Armin. Inserted here.
    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Wednesday, October 5, 2011 4:09 PM
  • Thx for providing the screenshots! That helps a lot. So my current understanding is: You set the two sub forms top-most because you want them to be in front of the main form. The problem is that top-most is a system-wide property, i.e. the sub forms are also in front of paint.
    Now, when overriding WndProc, this is solved because you set topmost=false when the application is deactivated.

    What about my suggestion of making the main form the owner of the sub forms?

    Overriding WndProc shouldn't have an influence on application startup, unless, for example, you write all window message to the debug output. The code you've posted shouldn't cause this. Have you additional code in WndProc now? As well I can't explain the behavior of a not responsive UI, but maybe it's the same cuase.


    Armin
    Wednesday, October 5, 2011 4:35 PM
  • Hi Armin,

       Your suggestion i.e.  making the main form the owner of the sub forms makes no difference. It still behaves the same.

       REG the behaviour of WndProc code, WndProc doesn't contains any other code than mentioned. But this is what happens on using it with UpdateLayeredWindow. I had seen other issues also with UpdateLayeredWindow like no Input cursor, and many other minor ones which needed a work around. I have re-made the same app 2-3 times to know the cause of the problem(s) which even the experts here couldn't get easily and suggested me to redo the app. With Input cursor on text field this was the best working solution provided by Thorsten on this forum. And it does provide me the input cursor on text fields, now only wanted to handle the textfields that stays on top always as long the app is open.

     

     


    Thanks
    If you find any answer helpful, then click "Vote As Helpful" and if it also solves your question then also click "Mark As Answer".
    Friday, October 7, 2011 7:57 AM