Skip to content
This repository was archived by the owner on Nov 12, 2022. It is now read-only.

Commit 30d1913

Browse files
committed
More hacks for focusing on putty.
Another hack for my quest to have the putty window properly focused. This deals with when you maximize/restore and minimize & restore. Previously, the putty window will lose focus. Not anymore!
1 parent 04fcc34 commit 30d1913

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace SuperPutty.Classes
7+
{
8+
class RestoreFromMinimizedTracker : WindowEventHandler
9+
{
10+
const int EVENT_SYSTEM_MINIMIZEEND = 23;
11+
12+
public RestoreFromMinimizedTracker(frmSuperPutty form) : base(form)
13+
{
14+
HookEvent(EVENT_SYSTEM_MINIMIZEEND);
15+
}
16+
17+
protected override void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
18+
{
19+
if (hwnd == this.m_form.Handle)
20+
{
21+
this.m_form.FocusCurrentTab();
22+
}
23+
}
24+
}
25+
}

SuperPutty/SuperPutty.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
<Compile Include="Classes\Database.cs" />
101101
<Compile Include="Classes\GlobalHotkeys.cs" />
102102
<Compile Include="Classes\KeyboardListener.cs" />
103+
<Compile Include="Classes\RestoreFromMinimizedTracker.cs" />
103104
<Compile Include="Classes\WindowEventHandler.cs" />
104105
<Compile Include="Classes\WindowTitleTracker.cs" />
105106
<Compile Include="ctlApplicationPanel.cs">

SuperPutty/frmSuperPutty.cs

+20-2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public frmSuperPutty(string[] args)
9797
m_hotkeys = new GlobalHotkeys();
9898
m_keyboard = new KeyboardListener(this, m_hotkeys);
9999
m_titleTracker = new WindowTitleTracker(this);
100+
registerHotkeys();
100101

101102
// Check SQLite Database
102103
openOrCreateSQLiteDatabase();
@@ -186,7 +187,6 @@ public frmSuperPutty(string[] args)
186187
// Set window state and size
187188
setWindowStateAndSize();
188189

189-
registerHotkeys();
190190

191191
focusHacks();
192192
}
@@ -589,7 +589,7 @@ private void selectTab(GlobalHotkeys.Purpose tabPosition)
589589

590590
private void selectTab(int index)
591591
{
592-
if (this.children.Count > 1)
592+
if (this.children.Count > 1 && this.children.Count > (index + 1))
593593
{
594594
this.dockPanel1.ActiveContent.DockHandler.SetActiveTab(index);
595595
}
@@ -895,6 +895,7 @@ private void quickConnectToolStripMenuItem_Click(object sender, EventArgs e)
895895
private DateTime m_lastMouseDownOnTitleBar = DateTime.Now;
896896
private TimeSpan m_delayUntilMouseMove = new TimeSpan(0, 0, 0, 0, 200); // 200ms
897897
private Point m_mouseDownLocation = new Point(0, 0);
898+
private RestoreFromMinimizedTracker m_restoreFromMinimized;
898899

899900
private int GET_X_LPARAM(int lParam)
900901
{
@@ -911,6 +912,11 @@ private bool WndProcForFocus(ref Message m)
911912
const int WM_NCLBUTTONDOWN = 0x00A1;
912913
const int WM_NCMOUSEMOVE = 0x00A0;
913914
const int WM_NCACTIVATE = 0x0086;
915+
const int WM_SYSCOMMAND = 0x0112;
916+
917+
const int SC_MINIMIZE = 0xF030;
918+
const int SC_RESTORE = 0xF120;
919+
const int SC_DRAGMOVE = 0xF012;
914920

915921
switch (m.Msg)
916922
{
@@ -937,6 +943,17 @@ private bool WndProcForFocus(ref Message m)
937943
DefWindowProc(this.Handle, m.Msg, (IntPtr)1, m.LParam);
938944
m.Result = (IntPtr)1;
939945
return false;
946+
case WM_SYSCOMMAND:
947+
// Removing the last 4 bits. This is necessary because
948+
// maximizing by double click gives you 0xF032, not 0xF030.
949+
switch ((int)m.WParam & 0xFFF0)
950+
{
951+
case SC_MINIMIZE:
952+
case SC_RESTORE:
953+
FocusCurrentTab();
954+
break;
955+
}
956+
break;
940957
default:
941958
if (m.Msg == m_shellHookNotify)
942959
{
@@ -973,6 +990,7 @@ private void focusHacks()
973990
this.ResizeEnd += HandleResizeEnd;
974991
m_shellHookNotify = RegisterWindowMessage("SHELLHOOK");
975992
RegisterShellHookWindow(this.Handle);
993+
m_restoreFromMinimized = new RestoreFromMinimizedTracker(this);
976994
}
977995

978996
// Handle various events to keep the child window focused

0 commit comments

Comments
 (0)