-
Nov 18th, 2017, 03:04 PM
#1
Thread Starter
Member
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?
-
Nov 18th, 2017, 08:36 PM
#2
Thread Starter
Member
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
-
Nov 18th, 2017, 10:00 PM
#3
Re: Keep focus on parent when interacting with child (and vice versa)?
Try this
vb.net Code:
Imports System.Runtime.InteropServices
Public Class Form1
Private Const WM_SYSCOMMAND As Integer = &H112
Private Const SC_MAXIMIZE As Integer = &HF030&
Private Const WM_ACTIVATE As Integer = &H6
Private Const WA_ACTIVE As Integer = 1
Private Const WA_CLICKACTIVE As Integer = 2
<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 ChildProc As Process
Private WithEvents Timer1 As Timer ' Useful to enable you to control your form e.g. minimize, close, etc...
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ChildProc = Process.GetProcessesByName("Notepad").FirstOrDefault
If ChildProc IsNot Nothing Then
Timer1 = New Timer With {.Interval = 500, .Enabled = True}
End If
End Sub
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
If Timer1 IsNot Nothing Then
Timer1.Start()
End If
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
If ChildProc IsNot Nothing Then
SetParent(ChildProc.MainWindowHandle, Me.Handle)
SendMessage(ChildProc.MainWindowHandle, WM_ACTIVATE, WA_ACTIVE, 0)
End If
End Sub
End Class
Is this what you want?
-
Nov 19th, 2017, 04:40 AM
#4
Thread Starter
Member
Re: Keep focus on parent when interacting with child (and vice versa)?
Originally Posted by 4x2y
Try this
vb.net Code:
Imports System.Runtime.InteropServices Public Class Form1 Private Const WM_SYSCOMMAND As Integer = &H112 Private Const SC_MAXIMIZE As Integer = &HF030& Private Const WM_ACTIVATE As Integer = &H6 Private Const WA_ACTIVE As Integer = 1 Private Const WA_CLICKACTIVE As Integer = 2 <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 ChildProc As Process Private WithEvents Timer1 As Timer ' Useful to enable you to control your form e.g. minimize, close, etc... Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click ChildProc = Process.GetProcessesByName("Notepad").FirstOrDefault If ChildProc IsNot Nothing Then Timer1 = New Timer With {.Interval = 500, .Enabled = True} End If End Sub Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated If Timer1 IsNot Nothing Then Timer1.Start() End If End Sub Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick Timer1.Stop() If ChildProc IsNot Nothing Then SetParent(ChildProc.MainWindowHandle, Me.Handle) SendMessage(ChildProc.MainWindowHandle, WM_ACTIVATE, WA_ACTIVE, 0) End If End Sub 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:
Do you see how my TestProject form is out of focus. If it were in-focus, it would look like this:
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!
-
Nov 19th, 2017, 07:09 AM
#5
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.
Originally Posted by Luceee_Qiller
[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.
-
Nov 19th, 2017, 07:12 AM
#6
Thread Starter
Member
Re: Keep focus on parent when interacting with child (and vice versa)?
Originally Posted by si_the_geek
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
-
Nov 19th, 2017, 09:08 AM
#7
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.
-
Nov 19th, 2017, 11:47 AM
#8
Thread Starter
Member
Re: Keep focus on parent when interacting with child (and vice versa)?
Originally Posted by Sitten Spynne
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!
-
Nov 19th, 2017, 02:58 PM
#9
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.
-
Nov 19th, 2017, 05:50 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|