// NOTE: this overload is provided in case the caller doesn't have a window to send the application command from; note that this scenario isn't quite as fail-proof as providing an hwnd (since explorer.exe could be crashed, restarting, etc.) public static MorphicResult <MorphicUnit, Win32ApiError> GenerateApplicationCommandEvent(AppCommand appCommand) { var taskbarHwnd = PInvoke.User32.FindWindow("Shell_TrayWnd", null); if (taskbarHwnd == IntPtr.Zero) { // could not find taskbar window var win32ErrorCode = PInvoke.Kernel32.GetLastError(); return(MorphicResult.ErrorResult(Win32ApiError.Win32Error((uint)win32ErrorCode))); } // NOTE: many implementations of SendMessage(...WM_APPCOMMAND) use a sourceHwnd of IntPtr.Zero; we've chosen to use the taskbar as both source and target hwnd out of an abundance of caution; if this doesn't work well in practice, try following the example of others by setting the sourceHwnd to IntPtr.Zero return(AppCommandUtils.GenerateApplicationCommandEvent(taskbarHwnd, taskbarHwnd, appCommand)); }
public static MorphicResult <Point, Win32ApiError> GetCurrentPosition() { var point = new PInvoke.POINT(); var getCursorPosResult = PInvoke.User32.GetCursorPos(out point); if (getCursorPosResult == false) { var win32ErrorCode = PInvoke.Kernel32.GetLastError(); if (win32ErrorCode != PInvoke.Win32ErrorCode.ERROR_SUCCESS) { return(MorphicResult.ErrorResult(Win32ApiError.Win32Error((uint)win32ErrorCode))); } } var result = new Point(point.x, point.y); return(MorphicResult.OkResult(result)); }
// NOTE: in the future, we may want to consider offering an animation of our cursor's move to the center of the display, via a second (options) parameter public static MorphicResult <MorphicUnit, Win32ApiError> MoveCursorToCenterOfDisplay(Morphic.WindowsNative.Display.Display display) { // get the current display's bounds // for PerMonitorV2 DPI-aware clients, this function will return the display rectangle in PHYSICAL pixels // for non-DPI-aware clients, this function will return the display rectangle in VIRTUAL pixels var getDisplayRectangleInPixelsResult = display.GetDisplayRectangleInPixels(); if (getDisplayRectangleInPixelsResult.IsError == true) { var win32ErrorCode = PInvoke.Kernel32.GetLastError(); if (win32ErrorCode != PInvoke.Win32ErrorCode.ERROR_SUCCESS) { return(MorphicResult.ErrorResult(Win32ApiError.Win32Error((uint)win32ErrorCode))); } } var currentDisplayPhysicalBounds = getDisplayRectangleInPixelsResult.Value !; // calculate the center point on the display var centerX = currentDisplayPhysicalBounds.Left + (currentDisplayPhysicalBounds.Width / 2); var centerY = currentDisplayPhysicalBounds.Top + (currentDisplayPhysicalBounds.Height / 2); // move the mouse cursor to the center point on the display var setCursorPosResult = PInvoke.User32.SetCursorPos(centerX, centerY); if (setCursorPosResult == false) { var win32ErrorCode = PInvoke.Kernel32.GetLastError(); if (win32ErrorCode != PInvoke.Win32ErrorCode.ERROR_SUCCESS) { return(MorphicResult.ErrorResult(Win32ApiError.Win32Error((uint)win32ErrorCode))); } } // return success return(MorphicResult.OkResult()); }