Skip to content

Commit

Permalink
fixed #473
Browse files Browse the repository at this point in the history
  • Loading branch information
NaBian committed Sep 19, 2020
1 parent d1f6b5f commit b30c6ff
Showing 1 changed file with 29 additions and 31 deletions.
60 changes: 29 additions & 31 deletions src/Shared/HandyControl_Shared/Controls/Other/NotifyIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -351,16 +351,15 @@ private void DispatcherTimerBlinkTick(object sender, EventArgs e)

private bool CheckMouseIsEnter()
{
var isTrue = FindNotifyIcon(out var rectNotify);
var isTrue = FindNotifyIcon(out var rectNotifyList);
if (!isTrue) return false;
InteropMethods.GetCursorPos(out var point);
if (point.X >= rectNotify.Left && point.X <= rectNotify.Right &&
point.Y >= rectNotify.Top && point.Y <= rectNotify.Bottom)
{
return true;
}

return false;
InteropMethods.GetCursorPos(out var point);
return rectNotifyList.Any(rectNotify =>
point.X >= rectNotify.Left &&
point.X <= rectNotify.Right &&
point.Y >= rectNotify.Top &&
point.Y <= rectNotify.Bottom);
}

private void DispatcherTimerPos_Tick(object sender, EventArgs e)
Expand Down Expand Up @@ -397,15 +396,14 @@ private IntPtr FindTrayToolbarWindow()
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "TrayNotifyWnd", null);
if (hWnd != IntPtr.Zero)
{

hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysPager", null);
if (hWnd != IntPtr.Zero)
{
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);

}
}
}

return hWnd;
}

Expand All @@ -420,22 +418,22 @@ private IntPtr FindTrayToolbarOverFlowWindow()
return hWnd;
}

private bool FindNotifyIcon(out InteropValues.RECT rect)
private bool FindNotifyIcon(out List<InteropValues.RECT> rectList)
{
var rectNotify = new InteropValues.RECT();
var rectNotifyList = new List<InteropValues.RECT>();
var hTrayWnd = FindTrayToolbarWindow();
var isTrue = FindNotifyIcon(hTrayWnd, ref rectNotify);
var isTrue = FindNotifyIcon(hTrayWnd, ref rectNotifyList);
if (!isTrue)
{
hTrayWnd = FindTrayToolbarOverFlowWindow();
isTrue = FindNotifyIcon(hTrayWnd, ref rectNotify);
isTrue = FindNotifyIcon(hTrayWnd, ref rectNotifyList);
}
rect = rectNotify;
rectList = rectNotifyList;
return isTrue;
}

//referenced from http://www.cnblogs.com/sczmzx/p/5158127.html
private bool FindNotifyIcon(IntPtr hTrayWnd, ref InteropValues.RECT rectNotify)
private bool FindNotifyIcon(IntPtr hTrayWnd, ref List<InteropValues.RECT> rectNotifyList)
{
InteropMethods.GetWindowRect(hTrayWnd, out var rectTray);
var count = (int)InteropMethods.SendMessage(hTrayWnd, InteropValues.TB_BUTTONCOUNT, 0, IntPtr.Zero);
Expand All @@ -449,23 +447,27 @@ private bool FindNotifyIcon(IntPtr hTrayWnd, ref InteropValues.RECT rectNotify)

var btnData = new InteropValues.TBBUTTON();
var trayData = new InteropValues.TRAYDATA();
var handel = Process.GetCurrentProcess().Id;
var handle = Process.GetCurrentProcess().Id;

for (uint i = 0; i < count; i++)
{
InteropMethods.SendMessage(hTrayWnd, InteropValues.TB_GETBUTTON, i, address);
var isTrue = InteropMethods.ReadProcessMemory(hProcess, address, out btnData, Marshal.SizeOf(btnData), out _);
if (!isTrue) continue;

if (btnData.dwData == IntPtr.Zero)
{
btnData.dwData = btnData.iString;
}

InteropMethods.ReadProcessMemory(hProcess, btnData.dwData, out trayData, Marshal.SizeOf(trayData), out _);
InteropMethods.GetWindowThreadProcessId(trayData.hwnd, out var dwProcessId);
if (dwProcessId == (uint)handel)

if (dwProcessId == (uint)handle)
{
var rect = new InteropValues.RECT();
var lngRect = InteropMethods.VirtualAllocEx(hProcess, IntPtr.Zero, Marshal.SizeOf(typeof(Rect)), InteropValues.AllocationType.Commit, InteropValues.MemoryProtection.ReadWrite);

InteropMethods.SendMessage(hTrayWnd, InteropValues.TB_GETITEMRECT, i, lngRect);
InteropMethods.ReadProcessMemory(hProcess, lngRect, out rect, Marshal.SizeOf(rect), out _);

Expand All @@ -476,15 +478,14 @@ private bool FindNotifyIcon(IntPtr hTrayWnd, ref InteropValues.RECT rectNotify)
var top = rectTray.Top + rect.Top;
var botton = rectTray.Top + rect.Bottom;
var right = rectTray.Left + rect.Right;
rectNotify = new InteropValues.RECT
rectNotifyList.Add(new InteropValues.RECT
{
Left = left,
Right = right,
Top = top,
Bottom = botton
};
});
isFind = true;
break;
}
}
InteropMethods.VirtualFreeEx(hProcess, address, 0x4096, InteropValues.FreeType.Decommit);
Expand Down Expand Up @@ -641,17 +642,14 @@ private void ShowContextMenu()

if (ContextContent != null)
{
if (_contextContent == null)
_contextContent ??= new Popup
{
_contextContent = new Popup
{
Placement = PlacementMode.Mouse,
AllowsTransparency = true,
StaysOpen = false,
UseLayoutRounding = true,
SnapsToDevicePixels = true
};
}
Placement = PlacementMode.Mouse,
AllowsTransparency = true,
StaysOpen = false,
UseLayoutRounding = true,
SnapsToDevicePixels = true
};

_contextContent.Child = new ContentControl
{
Expand Down

0 comments on commit b30c6ff

Please sign in to comment.