refactor Interop classes

This commit is contained in:
NaBian committed 2020-01-18 00:13:41 +08:00
1 parent de5c2e7a6e
commit a3dd633a66
28 files changed
+948 -976

No files matched your search

@@ -97,7 +97,7 @@ namespace HandyControl.Controls
{
var _ = new WindowInteropHelper(window)
{
Owner = NativeMethods.GetDesktopWindow()
Owner = InteropMethods.GetDesktopWindow()
};
}
}
@@ -96,10 +96,10 @@ namespace HandyControl.Controls
public static Color GetColorAt(int x, int y)
{
var desk = UnsafeNativeMethods.GetDesktopWindow();
var dc = UnsafeNativeMethods.GetWindowDC(desk);
var a = (int)UnsafeNativeMethods.GetPixel(dc, x, y);
UnsafeNativeMethods.ReleaseDC(desk, dc);
var desk = InteropMethods.GetDesktopWindow();
var dc = InteropMethods.GetWindowDC(desk);
var a = (int)InteropMethods.GetPixel(dc, x, y);
InteropMethods.ReleaseDC(desk, dc);
return Color.FromArgb(255, (byte) ((a >> 0) & 0xff), (byte) ((a >> 8) & 0xff), (byte) ((a >> 16) & 0xff));
}
}
@@ -33,7 +33,7 @@ namespace HandyControl.Controls
private IconHandle _iconHandle;
private const int WmTrayMouseMessage = NativeMethods.WM_USER + 1024;
private const int WmTrayMouseMessage = InteropValues.WM_USER + 1024;
private string _windowClassName;
@@ -41,7 +41,7 @@ namespace HandyControl.Controls
private IntPtr _messageWindowHandle;
private readonly WndProc _callback;
private readonly InteropValues.WndProc _callback;
private Popup _contextContent;
@@ -165,9 +165,9 @@ namespace HandyControl.Controls
{
if (!_added || DesignerHelper.IsInDesignMode) return;
var data = new NOTIFYICONDATA
var data = new InteropValues.NOTIFYICONDATA
{
uFlags = NativeMethods.NIF_INFO,
uFlags = InteropValues.NIF_INFO,
hWnd = _messageWindowHandle,
uID = _id,
szInfoTitle = title ?? string.Empty,
@@ -177,20 +177,20 @@ namespace HandyControl.Controls
switch (infoType)
{
case NotifyIconInfoType.Info:
data.dwInfoFlags = NativeMethods.NIIF_INFO;
data.dwInfoFlags = InteropValues.NIIF_INFO;
break;
case NotifyIconInfoType.Warning:
data.dwInfoFlags = NativeMethods.NIIF_WARNING;
data.dwInfoFlags = InteropValues.NIIF_WARNING;
break;
case NotifyIconInfoType.Error:
data.dwInfoFlags = NativeMethods.NIIF_ERROR;
data.dwInfoFlags = InteropValues.NIIF_ERROR;
break;
case NotifyIconInfoType.None:
data.dwInfoFlags = NativeMethods.NIIF_NONE;
data.dwInfoFlags = InteropValues.NIIF_NONE;
break;
}
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_MODIFY, data);
InteropMethods.Shell_NotifyIcon(InteropValues.NIM_MODIFY, data);
}
public void Dispose()
@@ -330,7 +330,7 @@ namespace HandyControl.Controls
{
var isTrue = FindNotifyIcon(out var rectNotify);
if (!isTrue) return false;
NativeMethods.GetCursorPos(out var point);
InteropMethods.GetCursorPos(out var point);
if (point.X >= rectNotify.Left && point.X <= rectNotify.Right &&
point.Y >= rectNotify.Top && point.Y <= rectNotify.Bottom)
{
@@ -368,17 +368,17 @@ namespace HandyControl.Controls
//referenced from http://www.cnblogs.com/sczmzx/p/5158127.html
private IntPtr FindTrayToolbarWindow()
{
var hWnd = NativeMethods.FindWindow("Shell_TrayWnd", null);
var hWnd = InteropMethods.FindWindow("Shell_TrayWnd", null);
if (hWnd != IntPtr.Zero)
{
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "TrayNotifyWnd", null);
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "TrayNotifyWnd", null);
if (hWnd != IntPtr.Zero)
{
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysPager", null);
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "SysPager", null);
if (hWnd != IntPtr.Zero)
{
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);
}
}
@@ -389,17 +389,17 @@ namespace HandyControl.Controls
//referenced from http://www.cnblogs.com/sczmzx/p/5158127.html
private IntPtr FindTrayToolbarOverFlowWindow()
{
var hWnd = NativeMethods.FindWindow("NotifyIconOverflowWindow", null);
var hWnd = InteropMethods.FindWindow("NotifyIconOverflowWindow", null);
if (hWnd != IntPtr.Zero)
{
hWnd = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);
hWnd = InteropMethods.FindWindowEx(hWnd, IntPtr.Zero, "ToolbarWindow32", null);
}
return hWnd;
}
private bool FindNotifyIcon(out NativeMethods.RECT rect)
private bool FindNotifyIcon(out InteropValues.RECT rect)
{
var rectNotify = new NativeMethods.RECT();
var rectNotify = new InteropValues.RECT();
var hTrayWnd = FindTrayToolbarWindow();
var isTrue = FindNotifyIcon(hTrayWnd, ref rectNotify);
if (!isTrue)
@@ -412,48 +412,48 @@ namespace HandyControl.Controls
}
//referenced from http://www.cnblogs.com/sczmzx/p/5158127.html
private bool FindNotifyIcon(IntPtr hTrayWnd, ref NativeMethods.RECT rectNotify)
private bool FindNotifyIcon(IntPtr hTrayWnd, ref InteropValues.RECT rectNotify)
{
NativeMethods.GetWindowRect(hTrayWnd, out var rectTray);
var count = (int)NativeMethods.SendMessage(hTrayWnd, NativeMethods.TB_BUTTONCOUNT, 0, IntPtr.Zero);
InteropMethods.GetWindowRect(hTrayWnd, out var rectTray);
var count = (int)InteropMethods.SendMessage(hTrayWnd, InteropValues.TB_BUTTONCOUNT, 0, IntPtr.Zero);
var isFind = false;
if (count > 0)
{
NativeMethods.GetWindowThreadProcessId(hTrayWnd, out var trayPid);
var hProcess = NativeMethods.OpenProcess(NativeMethods.ProcessAccess.VMOperation | NativeMethods.ProcessAccess.VMRead | NativeMethods.ProcessAccess.VMWrite, false, trayPid);
var address = NativeMethods.VirtualAllocEx(hProcess, IntPtr.Zero, 1024, NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);
InteropMethods.GetWindowThreadProcessId(hTrayWnd, out var trayPid);
var hProcess = InteropMethods.OpenProcess(InteropValues.ProcessAccess.VMOperation | InteropValues.ProcessAccess.VMRead | InteropValues.ProcessAccess.VMWrite, false, trayPid);
var address = InteropMethods.VirtualAllocEx(hProcess, IntPtr.Zero, 1024, InteropValues.AllocationType.Commit, InteropValues.MemoryProtection.ReadWrite);
var btnData = new NativeMethods.TBBUTTON();
var trayData = new NativeMethods.TRAYDATA();
var btnData = new InteropValues.TBBUTTON();
var trayData = new InteropValues.TRAYDATA();
var handel = Process.GetCurrentProcess().Id;
for (uint i = 0; i < count; i++)
{
NativeMethods.SendMessage(hTrayWnd, NativeMethods.TB_GETBUTTON, i, address);
var isTrue = NativeMethods.ReadProcessMemory(hProcess, address, out btnData, Marshal.SizeOf(btnData), out _);
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;
}
NativeMethods.ReadProcessMemory(hProcess, btnData.dwData, out trayData, Marshal.SizeOf(trayData), out _);
NativeMethods.GetWindowThreadProcessId(trayData.hwnd, out var dwProcessId);
InteropMethods.ReadProcessMemory(hProcess, btnData.dwData, out trayData, Marshal.SizeOf(trayData), out _);
InteropMethods.GetWindowThreadProcessId(trayData.hwnd, out var dwProcessId);
if (dwProcessId == (uint)handel)
{
var rect = new NativeMethods.RECT();
var lngRect = NativeMethods.VirtualAllocEx(hProcess, IntPtr.Zero, Marshal.SizeOf(typeof(Rect)), NativeMethods.AllocationType.Commit, NativeMethods.MemoryProtection.ReadWrite);
NativeMethods.SendMessage(hTrayWnd, NativeMethods.TB_GETITEMRECT, i, lngRect);
NativeMethods.ReadProcessMemory(hProcess, lngRect, out rect, Marshal.SizeOf(rect), out _);
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 _);
NativeMethods.VirtualFreeEx(hProcess, lngRect, Marshal.SizeOf(rect), NativeMethods.FreeType.Decommit);
NativeMethods.VirtualFreeEx(hProcess, lngRect, 0, NativeMethods.FreeType.Release);
InteropMethods.VirtualFreeEx(hProcess, lngRect, Marshal.SizeOf(rect), InteropValues.FreeType.Decommit);
InteropMethods.VirtualFreeEx(hProcess, lngRect, 0, InteropValues.FreeType.Release);
var left = rectTray.Left + rect.Left;
var top = rectTray.Top + rect.Top;
var botton = rectTray.Top + rect.Bottom;
var right = rectTray.Left + rect.Right;
rectNotify = new NativeMethods.RECT
rectNotify = new InteropValues.RECT
{
Left = left,
Right = right,
@@ -464,9 +464,9 @@ namespace HandyControl.Controls
break;
}
}
NativeMethods.VirtualFreeEx(hProcess, address, 0x4096, NativeMethods.FreeType.Decommit);
NativeMethods.VirtualFreeEx(hProcess, address, 0, NativeMethods.FreeType.Release);
NativeMethods.CloseHandle(hProcess);
InteropMethods.VirtualFreeEx(hProcess, address, 0x4096, InteropValues.FreeType.Decommit);
InteropMethods.VirtualFreeEx(hProcess, address, 0, InteropValues.FreeType.Release);
InteropMethods.CloseHandle(hProcess);
}
return isFind;
}
@@ -496,13 +496,13 @@ namespace HandyControl.Controls
if (DesignerHelper.IsInDesignMode) return;
_isTransparent = isTransparent;
var data = new NOTIFYICONDATA
var data = new InteropValues.NOTIFYICONDATA
{
uCallbackMessage = WmTrayMouseMessage,
uFlags = NativeMethods.NIF_MESSAGE | NativeMethods.NIF_ICON | NativeMethods.NIF_TIP,
uFlags = InteropValues.NIF_MESSAGE | InteropValues.NIF_ICON | InteropValues.NIF_TIP,
hWnd = _messageWindowHandle,
uID = _id,
dwInfoFlags = NativeMethods.NIF_TIP,
dwInfoFlags = InteropValues.NIF_TIP,
hIcon = isTransparent ? IntPtr.Zero : _iconCurrentHandle,
szTip = Text
};
@@ -511,17 +511,17 @@ namespace HandyControl.Controls
{
if (!_added)
{
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_ADD, data);
InteropMethods.Shell_NotifyIcon(InteropValues.NIM_ADD, data);
_added = true;
}
else
{
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_MODIFY, data);
InteropMethods.Shell_NotifyIcon(InteropValues.NIM_MODIFY, data);
}
}
else if (_added)
{
UnsafeNativeMethods.Shell_NotifyIcon(NativeMethods.NIM_DELETE, data);
InteropMethods.Shell_NotifyIcon(InteropValues.NIM_DELETE, data);
_added = false;
}
}
@@ -530,7 +530,7 @@ namespace HandyControl.Controls
private void RegisterClass()
{
_windowClassName = $"HandyControl.Controls.NotifyIcon{Guid.NewGuid()}";
var wndclass = new WNDCLASS
var wndclass = new InteropValues.WNDCLASS
{
style = 0,
lpfnWndProc = _callback,
@@ -544,9 +544,9 @@ namespace HandyControl.Controls
lpszClassName = _windowClassName
};
UnsafeNativeMethods.RegisterClass(wndclass);
_wmTaskbarCreated = NativeMethods.RegisterWindowMessage("TaskbarCreated");
_messageWindowHandle = UnsafeNativeMethods.CreateWindowEx(0, _windowClassName, "", 0, 0, 0, 1, 1,
InteropMethods.RegisterClass(wndclass);
_wmTaskbarCreated = InteropMethods.RegisterWindowMessage("TaskbarCreated");
_messageWindowHandle = InteropMethods.CreateWindowEx(0, _windowClassName, "", 0, 0, 0, 1, 1,
IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
}
@@ -562,17 +562,17 @@ namespace HandyControl.Controls
{
switch (lparam.ToInt64())
{
case NativeMethods.WM_LBUTTONDBLCLK:
case InteropValues.WM_LBUTTONDBLCLK:
WmMouseDown(MouseButton.Left, 2);
break;
case NativeMethods.WM_LBUTTONUP:
case InteropValues.WM_LBUTTONUP:
WmMouseUp(MouseButton.Left);
break;
case NativeMethods.WM_RBUTTONUP:
case InteropValues.WM_RBUTTONUP:
ShowContextMenu();
WmMouseUp(MouseButton.Right);
break;
case NativeMethods.WM_MOUSEMOVE:
case InteropValues.WM_MOUSEMOVE:
if (!_dispatcherTimerPos.IsEnabled)
{
_dispatcherTimerPos.Interval = TimeSpan.FromMilliseconds(200);
@@ -583,7 +583,7 @@ namespace HandyControl.Controls
}
}
return UnsafeNativeMethods.DefWindowProc(hWnd, msg, wparam, lparam);
return InteropMethods.DefWindowProc(hWnd, msg, wparam, lparam);
}
private void WmMouseDown(MouseButton button, int clicks)
@@ -632,7 +632,7 @@ namespace HandyControl.Controls
Content = ContextContent
};
_contextContent.IsOpen = true;
UnsafeNativeMethods.SetForegroundWindow(_contextContent.Child.GetHandle());
InteropMethods.SetForegroundWindow(_contextContent.Child.GetHandle());
}
else if (ContextMenu != null)
{
@@ -655,7 +655,7 @@ namespace HandyControl.Controls
ContextMenu.Placement = PlacementMode.Mouse;
ContextMenu.IsOpen = true;
UnsafeNativeMethods.SetForegroundWindow(ContextMenu.GetHandle());
InteropMethods.SetForegroundWindow(ContextMenu.GetHandle());
}
}