Skip to content

Commit 53e8f8e

Browse files
committed
Issues #61 Check Desktop Window
1 parent bd8687d commit 53e8f8e

1 file changed

Lines changed: 30 additions & 20 deletions

File tree

SmartContextMenu/Forms/MainForm.cs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,31 +162,41 @@ private void StartProgramMenuItemHooked(object sender, KeyboardEventArgs e) => I
162162

163163
private void MoveToHooked(object sender, KeyboardEventArgs e) => Invoke((MethodInvoker)delegate
164164
{
165+
e.Succeeded = true;
166+
165167
var monitorHandles = SystemUtils.GetMonitors();
166-
if (monitorHandles.Count > 1)
168+
if (monitorHandles.Count < 2)
167169
{
168-
var handle = User32.GetForegroundWindow();
169-
var parentHandle = WindowUtils.GetParentWindow(handle);
170-
var monitorHandle = User32.MonitorFromWindow(parentHandle, Constants.MONITOR_DEFAULTTONEAREST);
171-
var monitor = monitorHandles.Select((x, i) => new { Handle = x, Index = i }).Where(x => x.Handle == monitorHandle).FirstOrDefault();
172-
if (monitor != default)
173-
{
174-
var monitorIndex = monitor.Index;
175-
if (e.NextMonitor)
176-
{
177-
monitorIndex = monitorIndex == (monitorHandles.Count - 1) ? 0 : monitorIndex + 1;
178-
}
170+
return;
171+
}
179172

180-
if (e.PreviousMonitor)
181-
{
182-
monitorIndex = monitorIndex == 0 ? monitorHandles.Count - 1 : monitorIndex - 1;
183-
}
173+
var handle = User32.GetForegroundWindow();
174+
var parentHandle = WindowUtils.GetParentWindow(handle);
175+
if (parentHandle == IntPtr.Zero || WindowUtils.IsDesktopWindow(parentHandle))
176+
{
177+
return;
178+
}
184179

185-
monitorHandle = monitorHandles[monitorIndex];
186-
WindowUtils.MoveToMonitor(parentHandle, monitorHandle);
187-
}
180+
var monitorHandle = User32.MonitorFromWindow(parentHandle, Constants.MONITOR_DEFAULTTONEAREST);
181+
var monitor = monitorHandles.Select((x, i) => new { Handle = x, Index = i }).Where(x => x.Handle == monitorHandle).FirstOrDefault();
182+
if (monitor == null)
183+
{
184+
return;
188185
}
189-
e.Succeeded = true;
186+
187+
var monitorIndex = monitor.Index;
188+
if (e.NextMonitor)
189+
{
190+
monitorIndex = monitorIndex == (monitorHandles.Count - 1) ? 0 : monitorIndex + 1;
191+
}
192+
193+
if (e.PreviousMonitor)
194+
{
195+
monitorIndex = monitorIndex == 0 ? monitorHandles.Count - 1 : monitorIndex - 1;
196+
}
197+
198+
monitorHandle = monitorHandles[monitorIndex];
199+
WindowUtils.MoveToMonitor(parentHandle, monitorHandle);
190200
});
191201

192202
private void EscKeyHooked(object sender, KeyboardEventArgs e) => Invoke((MethodInvoker)delegate

0 commit comments

Comments
 (0)