WinApi.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. using System;
  2. using System.Runtime.InteropServices;
  3. namespace Hardcodet.Wpf.TaskbarNotification.Interop
  4. {
  5. /// <summary>
  6. /// Win32 API imports.
  7. /// </summary>
  8. internal static class WinApi
  9. {
  10. private const string User32 = "user32.dll";
  11. /// <summary>
  12. /// Creates, updates or deletes the taskbar icon.
  13. /// </summary>
  14. [DllImport("shell32.Dll", CharSet = CharSet.Unicode)]
  15. public static extern bool Shell_NotifyIcon(NotifyCommand cmd, [In] ref NotifyIconData data);
  16. /// <summary>
  17. /// Creates the helper window that receives messages from the taskar icon.
  18. /// </summary>
  19. [DllImport(User32, EntryPoint = "CreateWindowExW", SetLastError = true)]
  20. public static extern IntPtr CreateWindowEx(int dwExStyle, [MarshalAs(UnmanagedType.LPWStr)] string lpClassName,
  21. [MarshalAs(UnmanagedType.LPWStr)] string lpWindowName, int dwStyle, int x, int y,
  22. int nWidth, int nHeight, IntPtr hWndParent, IntPtr hMenu, IntPtr hInstance,
  23. IntPtr lpParam);
  24. /// <summary>
  25. /// Processes a default windows procedure.
  26. /// </summary>
  27. [DllImport(User32)]
  28. public static extern IntPtr DefWindowProc(IntPtr hWnd, uint msg, IntPtr wparam, IntPtr lparam);
  29. /// <summary>
  30. /// Registers the helper window class.
  31. /// </summary>
  32. [DllImport(User32, EntryPoint = "RegisterClassW", SetLastError = true)]
  33. public static extern short RegisterClass(ref WindowClass lpWndClass);
  34. /// <summary>
  35. /// Registers a listener for a window message.
  36. /// </summary>
  37. /// <param name="lpString"></param>
  38. /// <returns>uint</returns>
  39. [DllImport(User32, EntryPoint = "RegisterWindowMessageW")]
  40. public static extern uint RegisterWindowMessage([MarshalAs(UnmanagedType.LPWStr)] string lpString);
  41. /// <summary>
  42. /// Used to destroy the hidden helper window that receives messages from the
  43. /// taskbar icon.
  44. /// </summary>
  45. /// <param name="hWnd"></param>
  46. /// <returns>bool</returns>
  47. [DllImport(User32, SetLastError = true)]
  48. public static extern bool DestroyWindow(IntPtr hWnd);
  49. /// <summary>
  50. /// Gives focus to a given window.
  51. /// </summary>
  52. /// <param name="hWnd"></param>
  53. /// <returns>bool</returns>
  54. [DllImport(User32)]
  55. public static extern bool SetForegroundWindow(IntPtr hWnd);
  56. /// <summary>
  57. /// Gets the maximum number of milliseconds that can elapse between a
  58. /// first click and a second click for the OS to consider the
  59. /// mouse action a double-click.
  60. /// </summary>
  61. /// <returns>The maximum amount of time, in milliseconds, that can
  62. /// elapse between a first click and a second click for the OS to
  63. /// consider the mouse action a double-click.</returns>
  64. [DllImport(User32, CharSet = CharSet.Auto, ExactSpelling = true)]
  65. public static extern int GetDoubleClickTime();
  66. /// <summary>
  67. /// Gets the screen coordinates of the current mouse position.
  68. /// </summary>
  69. [DllImport(User32, SetLastError = true)]
  70. public static extern bool GetPhysicalCursorPos(ref Point lpPoint);
  71. [DllImport(User32, SetLastError = true)]
  72. public static extern bool GetCursorPos(ref Point lpPoint);
  73. }
  74. }