Results 1 to 10 of 10

Thread: Keep focus on parent when interacting with child (and vice versa)?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Keep focus on parent when interacting with child (and vice versa)?

    Hi all,

    I'm trying to maintain focus on the parent and the child at the same time when interacting (clicking) on either one. Just to clarify, this is not a multi-document interface, and uses this code:

    Code:
     Dim ChildProc As Process = Process.GetProcessesByName("MyChildApp").FirstOrDefault
    
            If ChildProc IsNot Nothing Then
    
                SetParent(ChildProc.MainWindowHandle, ChildContainerPanel.Handle)
                SendMessage(ChildProc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
            End If
    So, obviously, when I click on the child form, which is embedded in on my main form, it shifts focus to the child form (and vice versa). This causes all sorts of complications for my application. How can I ensure that if I click on my child form, my form is in focus and if I click on my form, my client form remains in focus?

    What I've tried before asking this question:

    I've tried experimenting with ZOrder if my MainForm_LostFocus, however, it has no effect.

    Thanks for the help.
    [EDIT] - Off topic, but is it normal for me to be placing so much white-space is my code, I do it to make certain related components more in focus to increase readability, however will this effect my application's performance at all?

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Bump

    I'm becoming depressed over how annoying this issue is.

    Please help me

  3. #3
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Try this
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.     Private Const WM_SYSCOMMAND As Integer = &H112
    5.     Private Const SC_MAXIMIZE As Integer = &HF030&
    6.     Private Const WM_ACTIVATE As Integer = &H6
    7.     Private Const WA_ACTIVE As Integer = 1
    8.     Private Const WA_CLICKACTIVE As Integer = 2
    9.  
    10.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    11.     Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    12.     End Function
    13.  
    14.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    15.     Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    16.     End Function
    17.  
    18.     Private ChildProc As Process
    19.     Private WithEvents Timer1 As Timer ' Useful to enable you to control your form e.g. minimize, close, etc...
    20.  
    21.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    22.         ChildProc = Process.GetProcessesByName("Notepad").FirstOrDefault
    23.         If ChildProc IsNot Nothing Then
    24.             Timer1 = New Timer With {.Interval = 500, .Enabled = True}
    25.         End If
    26.     End Sub
    27.  
    28.     Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    29.         If Timer1 IsNot Nothing Then
    30.             Timer1.Start()
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    35.         Timer1.Stop()
    36.         If ChildProc IsNot Nothing Then
    37.             SetParent(ChildProc.MainWindowHandle, Me.Handle)
    38.             SendMessage(ChildProc.MainWindowHandle, WM_ACTIVATE, WA_ACTIVE, 0)
    39.         End If
    40.     End Sub
    41. End Class

    Is this what you want?



  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Quote Originally Posted by 4x2y View Post
    Try this
    vb.net Code:
    1. Imports System.Runtime.InteropServices
    2.  
    3. Public Class Form1
    4.     Private Const WM_SYSCOMMAND As Integer = &H112
    5.     Private Const SC_MAXIMIZE As Integer = &HF030&
    6.     Private Const WM_ACTIVATE As Integer = &H6
    7.     Private Const WA_ACTIVE As Integer = 1
    8.     Private Const WA_CLICKACTIVE As Integer = 2
    9.  
    10.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    11.     Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
    12.     End Function
    13.  
    14.     <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    15.     Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
    16.     End Function
    17.  
    18.     Private ChildProc As Process
    19.     Private WithEvents Timer1 As Timer ' Useful to enable you to control your form e.g. minimize, close, etc...
    20.  
    21.     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    22.         ChildProc = Process.GetProcessesByName("Notepad").FirstOrDefault
    23.         If ChildProc IsNot Nothing Then
    24.             Timer1 = New Timer With {.Interval = 500, .Enabled = True}
    25.         End If
    26.     End Sub
    27.  
    28.     Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
    29.         If Timer1 IsNot Nothing Then
    30.             Timer1.Start()
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    35.         Timer1.Stop()
    36.         If ChildProc IsNot Nothing Then
    37.             SetParent(ChildProc.MainWindowHandle, Me.Handle)
    38.             SendMessage(ChildProc.MainWindowHandle, WM_ACTIVATE, WA_ACTIVE, 0)
    39.         End If
    40.     End Sub
    41. End Class

    Is this what you want?
    Hi

    Thanks for replying, but I'm afraid this is not what I'm after. When the child is in the form and the user clicks inside of the child, this happens:

    Name:  1234.jpg
Views: 153
Size:  13.9 KB

    Do you see how my TestProject form is out of focus. If it were in-focus, it would look like this:

    Name:  12345.jpg
Views: 142
Size:  3.7 KB

    So, when the user is clicking in the child form, the parentform (TestProj) must remain focused.
    Any ideas?

    Here's the code for this form, by the way:

    Code:
    Imports System.Runtime.InteropServices
    Public Class Form1
    
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_MAXIMIZE As Integer = &HF030
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Public Shared Function SetParent(ByVal hWndChild As IntPtr, ByVal hWndNewParent As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
        Friend Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr
        End Function
    
        Private Sub SetChildTimer1_Tick(sender As Object, e As EventArgs) Handles SetChildTimer1.Tick
    
            Dim NtPdProcess As Process = Process.GetProcessesByName("notepad").FirstOrDefault
    
            If NtPdProcess IsNot Nothing Then
    
                SetParent(NtPdProcess.MainWindowHandle, Panel1.Handle)
                SendMessage(NtPdProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    
            End If
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            
          Dim startInfo As New ProcessStartInfo("notepad.exe")
           
          Process.Start(startInfo)
        End Sub
    End Class
    Thanks a lot for the help! - rated!

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Unfortunately only one window can be the Active window at a time, because the Active window is where key presses etc get sent, and they can only be sent to one.

    MDI child forms aren't really different forms as such (as they are part of the parent), so the way things look there is a bit different.

    I don't think I've ever seen a way to make two independent windows both be Active at the same time.

    Quote Originally Posted by Luceee_Qiller View Post
    [EDIT] - Off topic, but is it normal for me to be placing so much white-space is my code, I do it to make certain related components more in focus to increase readability, however will this effect my application's performance at all?
    White-space in the code only affects readability, it is completely ignored when you compile so does not alter performance.

    Exactly how you should do white-space is a matter of opinion, and my view is that directly-related code should not have white-space between it, so I would write SetChildTimer1_Tick like this:
    Code:
        Private Sub SetChildTimer1_Tick(sender As Object, e As EventArgs) Handles SetChildTimer1.Tick
    
            Dim NtPdProcess As Process = Process.GetProcessesByName("notepad").FirstOrDefault
            If NtPdProcess IsNot Nothing Then
                SetParent(NtPdProcess.MainWindowHandle, Panel1.Handle)
                SendMessage(NtPdProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            End If
    
        End Sub
    (if you added another child window with similar code for it, I would have a blank line between the two).

    I feel that adding lots of white-space is bad, because it means that your code is no longer in "paragraphs" (which allow you to see at a glance where different activities take place), and that you are more likely to need to scroll the code window. Having no white-space is also bad because that also eliminates "paragraphs".

    However, it is definitely something that is personal choice, so whatever decision you make it is not wrong.

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Quote Originally Posted by si_the_geek View Post
    Unfortunately only one window can be the Active window at a time, because the Active window is where key presses etc get sent, and they can only be sent to one.

    MDI child forms aren't really different forms as such (as they are part of the parent), so the way things look there is a bit different.

    I don't think I've ever seen a way to make two independent windows both be Active at the same time.

    White-space in the code only affects readability, it is completely ignored when you compile so does not alter performance.

    Exactly how you should do white-space is a matter of opinion, and my view is that directly-related code should not have white-space between it, so I would write SetChildTimer1_Tick like this:
    Code:
        Private Sub SetChildTimer1_Tick(sender As Object, e As EventArgs) Handles SetChildTimer1.Tick
    
            Dim NtPdProcess As Process = Process.GetProcessesByName("notepad").FirstOrDefault
            If NtPdProcess IsNot Nothing Then
                SetParent(NtPdProcess.MainWindowHandle, Panel1.Handle)
                SendMessage(NtPdProcess.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            End If
    
        End Sub
    (if you added another child window with similar code for it, I would have a blank line between the two).

    I feel that adding lots of white-space is bad, because it means that your code is no longer in "paragraphs" (which allow you to see at a glance where different activities take place), and that you are more likely to need to scroll the code window. Having no white-space is also bad because that also eliminates "paragraphs".

    However, it is definitely something that is personal choice, so whatever decision you make it is not wrong.
    Thanks a lot for your reply. I now know that I only need my parent form to be in focus. Is there a way to make it so that the parent form remains in focus even when trying to click in child.

    Thanks for clarifying white-space

  7. #7
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Keep focus on parent when interacting with child (and vice versa)?

    What are you ACTUALLY trying to do? I have a pretty good feeling there's a better way to accomplish your goal, but you are asking for "how do I accomplish this solution" instead of "I want to do X, this is the only way I know how to do it, but it's hard."

    This happens a lot in programming, really. By analogy it's like someone is asking for firewood. So you start chopping firewood. When you're done, they ask for flint and steel. You find some and give it to them. Then they ask you if you've got any large drying trays. Finally, you ask what they're trying to do. "I want a spot of tea." Then you facepalm and use the electric kettle and tea bags to make tea in 10 minutes.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2017
    Posts
    51

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Quote Originally Posted by Sitten Spynne View Post
    What are you ACTUALLY trying to do? I have a pretty good feeling there's a better way to accomplish your goal, but you are asking for "how do I accomplish this solution" instead of "I want to do X, this is the only way I know how to do it, but it's hard."

    This happens a lot in programming, really. By analogy it's like someone is asking for firewood. So you start chopping firewood. When you're done, they ask for flint and steel. You find some and give it to them. Then they ask you if you've got any large drying trays. Finally, you ask what they're trying to do. "I want a spot of tea." Then you facepalm and use the electric kettle and tea bags to make tea in 10 minutes.
    Thank you for the reply. I'm simply trying to set an external application as child within my form, however I want it to act as though it is part of my form (that's why the focusing of the parent form is important). Making the controls of the external form from scratch is not an option, by the way.

    Thanks so much!

  9. #9
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Keep focus on parent when interacting with child (and vice versa)?

    Why is it important that your application is "active" while the user is using a different program? What are you trying to do that you can't if your window is not active?
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: Keep focus on parent when interacting with child (and vice versa)?

    There is a rather simple solution, perhaps a bit of a kludge, using another form and a few lines of code (five or less) but I'm not sure why you need to do this. I suppose the external program you're hosting in your form would be a display only application with no user input. I'm not sure whether it would be considered stealing someone else's work as your own, or just embedding other applications within yours as a convenience.
    For instance, we do that at work as sort of a windows manager for a number of applications that we've written and other third party written applications, allowing the user to layout where the applications will be loaded and sized on an area of the desktop, with switching between the shown applications depending on the context.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width