Skip to content

Commit

Permalink
Merge pull request #851 from vityank/childrenAttachImprovement
Browse files Browse the repository at this point in the history
Improve the way child spawned windows attach Fixes Issue #851
  • Loading branch information
jimradford committed Dec 2, 2021
2 parents b860978 + fb4113c commit e47d8cf
Showing 1 changed file with 4 additions and 82 deletions.
86 changes: 4 additions & 82 deletions SuperPutty/ctlApplicationPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ private void MoveWindow(string src, int x, int y)
bool success = NativeMethods.MoveWindow(m_AppWin, x, y, this.Width, this.Height, this.Visible);
if (Log.IsInfoEnabled)
{
Log.InfoFormat("MoveWindow [{3,-15}{4,20}] w={0,4}, h={1,4}, visible={2}, success={5}", this.Width, this.Height, this.Visible, src, this.Name, success);
Log.InfoFormat("MoveWindow [{3,-15}{4,20}] w={0,4}, h={1,4}, visible={2}, success={5}, hnd={6}", this.Width, this.Height, this.Visible, src, this.Name, success, m_AppWin);
}
}
}
Expand All @@ -164,16 +164,12 @@ public bool ReFocusPuTTY(string caller)
this.MoveWindow("RestoreTabSwitch");
if (this.ExternalProcessCaptured && NativeMethods.GetForegroundWindow() != this.m_AppWin)
{
//Log.InfoFormat("[{0}] ReFocusPuTTY - puttyTab={1}, caller={2}", this.m_AppWin, this.Parent.Text, caller);
settingForeground = true;
result = NativeMethods.SetForegroundWindow(this.m_AppWin);
if (result)
NativeMethods.InvalidateRect(this.m_AppWin, IntPtr.Zero, false);
Log.InfoFormat("[{0}] ReFocusPuTTY - puttyTab={1}, caller={2}, result={3}", this.m_AppWin, this.Parent.Text, caller, result);
}
//return (this.m_AppWin != null
// && NativeMethods.GetForegroundWindow() != this.m_AppWin
// && !NativeMethods.SetForegroundWindow(this.m_AppWin));

return result;
}
Expand Down Expand Up @@ -298,81 +294,6 @@ private void bgWinTracker_DoWork(object sender, DoWorkEventArgs e)
}
}

private void __UNUSED_REFONLY_UpdateTrackedWindowHandle()
{
bool bNeedsUpdate = true;
if (this.b_AppWinFinal)
return;
switch (this.proto)
{
case SuperPutty.Data.ConnectionProtocol.RDP:
Log.Info("Update track is called.");
this.m_Process.Refresh();
IntPtr winCandidate = this.m_Process.MainWindowHandle;
Log.Info("Update, WinCnd="+((int)winCandidate));
if (winCandidate == IntPtr.Zero || winCandidate == this.m_AppWin)
break;
NativeMethods.RECT rect = new NativeMethods.RECT();
if (!NativeMethods.GetWindowRect(winCandidate, ref rect))
break;
int w = rect.Right - rect.Left;
int h = rect.Bottom - rect.Top;
Log.Info("Checking window handle [PID="+ this.m_Process.Id +"] to " + ((int)winCandidate) + ". Win size is " + w + "x" + h + ". Title is: " + this.m_Process.MainWindowTitle);
if ((this.m_Process.MainWindowTitle.Contains(":") && this.m_Process.MainWindowTitle.Contains(" - Remote Desktop Connection")) /*|| (w >= 400 && h >= 120)*/) /* WIP - Heuristic using caption(Non-localized OS) or size for other cases, quite hacky till find better way */
{
//this.b_AppWinFinal = true;
Log.Info("Updated window handle to " + ((int)winCandidate) + ". Win size is " + w + "x" + h + ". Title is: " + this.m_Process.MainWindowTitle);
this.m_AppWin = winCandidate;
}
else if (this.m_Process.MainWindowTitle.Equals("Remote Desktop Connection") /*|| w < 400*/) /* RDP master process or auth dialog */
{
System.Threading.Thread.Sleep(5000);
IntPtr childWin = NativeMethods.GetWindow(winCandidate, NativeMethods.GetWindowCmd.GW_CHILD);
Log.Info("Got child win: " + ((int)childWin));
StringBuilder childWinTitleBuf = new StringBuilder(256);
int childWinTitleLen = NativeMethods.GetWindowText(childWin, childWinTitleBuf, childWinTitleBuf.Capacity - 1);
if (childWinTitleLen > 0 && childWinTitleBuf.ToString().Contains(" - Remote Desktop Connection"))
{
this.b_AppWinFinal = true;
Log.Info("Updated transitional child window handle to " + ((int)childWin) + ". Win size is " + w + "x" + h + ". Title is: " + this.m_Process.MainWindowTitle);
this.m_AppWin = childWin;
}
else
{
//this.b_AppWinFinal = true;
Log.Info("Updated transitional window handle to " + ((int)winCandidate) + ". Win size is " + w + "x" + h + ". Title is: " + this.m_Process.MainWindowTitle);
this.m_AppWin = winCandidate;
}
}
else
{
Log.Info("Updated transitional window handle 2 to " + ((int)winCandidate) + ". Win size is " + w + "x" + h + ". Title is: " + this.m_Process.MainWindowTitle);
this.m_AppWin = winCandidate;
}
break;
default:
this.b_AppWinFinal = true;
bNeedsUpdate = false;
break;
}

if (this.b_AppWinFinal && bNeedsUpdate)
{
this.AttachToWindow(); /* Must attach to the window */

if (this.Visible && this.m_Created && this.ExternalProcessCaptured)
{
// Move the child so it's located over the parent
this.MoveWindow("UpdateWindow");

if (RefocusOnVisChanged && NativeMethods.GetForegroundWindow() != this.m_AppWin)
{
this.BeginInvoke(new MethodInvoker(delegate { this.ReFocusPuTTY("UpdateWindow"); }));
}
}
}
}

#region Focus Change Handling
/*************************** Begin Hack to watch for windows focus change events **************************************
* This is based on this form post:
Expand Down Expand Up @@ -522,7 +443,7 @@ protected override void OnVisibleChanged(EventArgs e)
m_AppWin = IntPtr.Zero;
try
{
if(!File.Exists(ApplicationName))
if (!File.Exists(ApplicationName))
{
MessageBox.Show(ApplicationName + " not found in configured path, please go into tools->settings and set the correct path", "Application Not Found");
return;
Expand All @@ -533,7 +454,8 @@ protected override void OnVisibleChanged(EventArgs e)
StartInfo =
{
FileName = ApplicationName,
Arguments = ApplicationParameters
Arguments = ApplicationParameters,
WindowStyle = ProcessWindowStyle.Maximized
}
};

Expand Down

0 comments on commit e47d8cf

Please sign in to comment.