Skip to content

Commit

Permalink
Wrap function now toggles from tray icon click
Browse files Browse the repository at this point in the history
  • Loading branch information
danieldownes committed Jun 17, 2024
1 parent e6f25c3 commit e7fa298
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 47 deletions.
13 changes: 8 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ Mouse Wrap is a small utility to enhance the mobility of the Windows mouse point

This edgeless experience in a desktop environment allows for faster interaction, especially when larger screen resolutions are involved.

The project has been running for 25 years! I love coffee, pledge support via [Buy Me a Coffee](https://buymeacoffee.com/danieldownes/mouse-wrap-4) and your contribution will be very much appreciated.

# Latest Release: Version 3.3
Size: 1.1Mb
Version: 3.3 (4th Feb. 2013)
Expand All @@ -23,8 +25,9 @@ Linux Wine 1.1 or higher
## Roadmap:
[x] 1. Basic mouse wrapping on a single desktop.
[x] 2. Edge case, consider task bar placement while dragging windows.
[ ] 3. Tray icon with toggle control.
[ ] 4. Add/Remove to Windows Startup support (without installer).
[ ] 5. Text based option file.
[ ] 6. Multiple desktop support.
[ ] 7. Visual Option window using WinUI 3.
[x] 3. Tray icon with toggle control.
[x] 4. Add/Remove to Windows Startup support (without installer).
[ ] 5. Ensure single-instance only can run.
[ ] 6. Text-based option file.
[ ] 7. Multiple desktop support.
[ ] 8. (Planning: Visual Option window using WinUI 3.)
Binary file modified v4.0/src/MW4.rc
Binary file not shown.
5 changes: 3 additions & 2 deletions v4.0/src/MW4.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@
<LanguageStandard_C>stdc17</LanguageStandard_C>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
Expand All @@ -145,7 +145,8 @@
<ResourceCompile Include="MW4.rc" />
</ItemGroup>
<ItemGroup>
<Image Include="mw4-test.ico" />
<Image Include="mw4-disabled.ico" />
<Image Include="mw4.ico" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
5 changes: 4 additions & 1 deletion v4.0/src/MW4.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@
</ResourceCompile>
</ItemGroup>
<ItemGroup>
<Image Include="mw4-test.ico">
<Image Include="mw4.ico">
<Filter>Resource Files</Filter>
</Image>
<Image Include="mw4-disabled.ico">
<Filter>Resource Files</Filter>
</Image>
</ItemGroup>
Expand Down
11 changes: 6 additions & 5 deletions v4.0/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLin
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON_ENABLED));

RegisterClass(&wc);

Expand All @@ -32,7 +32,8 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLin
CreateTrayIcon(hwnd, hInstance);
CreateContextMenu();

SetTimer(hwnd, IDT_TIMER1, 100, NULL);
wrapEnabled = FALSE;
ToggleWrapEnabled(hwnd);

MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
Expand Down Expand Up @@ -63,14 +64,14 @@ LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
break;

case WM_TIMER:
if (wParam == IDT_TIMER1)
{
if (wParam == IDT_WRAP_TIMER)
WrapMouseWhileDragging();
}

break;

default:
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
return 0;
}

2 changes: 1 addition & 1 deletion v4.0/src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include "resource.h"
#include "mouse_wrap.h"
#include "taskbar.h"
#include "startup.h"

#define CLASS_NAME "Mouse Wrap"
#define WINDOW_TITLE "Mouse Wrap"
#define IDT_TIMER1 1

extern HINSTANCE hInst;

Expand Down
55 changes: 29 additions & 26 deletions v4.0/src/mouse_wrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,32 @@
#include "mouse_wrap.h"
#include <stdio.h>

BOOL wrapEnabled = TRUE;

typedef struct {
POINT pt;
BOOL isDragging;
} MouseData;

void ToggleWrapEnabled(HWND hwnd)
{
wrapEnabled = !wrapEnabled;
if (wrapEnabled)
SetTimer(hwnd, IDT_WRAP_TIMER, WRAP_DELAY, NULL);
else
KillTimer(hwnd, IDT_WRAP_TIMER);
}

void WrapMouseWhileDragging()
{
MouseData mouseData;
GetCursorPos(&mouseData.pt);
mouseData.isDragging = TRUE;

// Enumerate all monitors and check if we need to wrap the mouse
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&mouseData);
}

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
MouseData* mouseData = (MouseData*)dwData;
Expand All @@ -17,40 +38,31 @@ BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMoni
GetMonitorInfo(hMonitor, &mi);
workArea = mi.rcWork;

// Use OutputDebugString to output the WorkArea dimensions
char buffer[256];
sprintf_s(buffer, sizeof(buffer), "WorkArea: (%d, %d) - (%d, %d)\n", workArea.left, workArea.top, workArea.right, workArea.bottom);
OutputDebugStringA(buffer);
sprintf_s(buffer, sizeof(buffer), "Monitor: (%d, %d) - (%d, %d)\n", lprcMonitor->right, lprcMonitor->top, lprcMonitor->right, lprcMonitor->bottom);
OutputDebugStringA(buffer);


// Check if the mouse is exactly on the border of the screen
if (mouseData->pt.x <= lprcMonitor->left) {
mouseData->pt.x = lprcMonitor->right - 1;
mouseData->pt.x = lprcMonitor->right - 2;
SetCursorPos(mouseData->pt.x, mouseData->pt.y);
return FALSE;
}
else if (mouseData->pt.x >= lprcMonitor->right - 1) {
mouseData->pt.x = lprcMonitor->left + 1;
mouseData->pt.x = lprcMonitor->left + 2;
SetCursorPos(mouseData->pt.x, mouseData->pt.y);
return FALSE;
}
else if (mouseData->pt.y <= lprcMonitor->top) {
mouseData->pt.y = lprcMonitor->bottom - 1;
mouseData->pt.y = lprcMonitor->bottom - 2;
SetCursorPos(mouseData->pt.x, mouseData->pt.y);
return FALSE;
}
else if (mouseData->pt.y == lprcMonitor->bottom - 1) {
mouseData->pt.y = lprcMonitor->top;
mouseData->pt.y = lprcMonitor->top + 1;
SetCursorPos(mouseData->pt.x, mouseData->pt.y);
return FALSE;
}


if (!(GetAsyncKeyState(VK_LBUTTON) & 0x8000)) {
// Check workspace area only while dragging
if (!(GetAsyncKeyState(VK_LBUTTON) & 0x8000))
return;
}

// Check if the mouse is exactly on the border and wrap it to the opposite side within the work area
if (mouseData->pt.x == workArea.left) {
Expand All @@ -74,16 +86,7 @@ BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMoni
return FALSE;
}


return TRUE; // Continue enumeration
// Continue enumeration
return TRUE;
}

void WrapMouseWhileDragging()
{
MouseData mouseData;
GetCursorPos(&mouseData.pt);
mouseData.isDragging = TRUE;

// Enumerate all monitors and check if we need to wrap the mouse
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&mouseData);
}
8 changes: 7 additions & 1 deletion v4.0/src/mouse_wrap.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#ifndef MOUSE_WRAP_H
#define MOUSE_WRAP_H

void WrapMouse();
#define IDT_WRAP_TIMER 1
#define WRAP_DELAY 20

BOOL wrapEnabled;

void ToggleWrapEnabled(HWND hwnd);
void WrapMouseWhileDragging();
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData);

#endif // MOUSE_WRAP_H
Binary file added v4.0/src/mw4-disabled.ico
Binary file not shown.
Binary file removed v4.0/src/mw4-test.ico
Binary file not shown.
Binary file added v4.0/src/mw4.ico
Binary file not shown.
Binary file added v4.0/src/mw4.ico.kra
Binary file not shown.
7 changes: 4 additions & 3 deletions v4.0/src/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
// Microsoft Visual C++ generated include file.
// Used by MW4.rc
//
#define IDI_ICON1 101
#define IDI_ICON2 102
#define IDI_ICON_ENABLED 103
#define IDI_ICON1 104
#define IDI_ICON_DISABLED 104

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_RESOURCE_VALUE 105
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_SYMED_VALUE 101
Expand Down
18 changes: 15 additions & 3 deletions v4.0/src/taskbar.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#include "taskbar.h"
#include "startup.h"
#include "mouse_wrap.h"

#define WM_TRAYICON (WM_USER + 1)
#define IDM_EXITAPP 1001
#define IDM_BUYMEACOFFEE 1002
#define IDM_TOGGLE_STARTUP 1003

HINSTANCE hInstMain;

static NOTIFYICONDATA nid;
static HMENU hMenu;

void CreateTrayIcon(HWND hwnd, HINSTANCE hInst)
{
hInstMain = hInst;

memset(&nid, 0, sizeof(nid));
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hwnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_TRAYICON;
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
nid.hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON_ENABLED));
wcscpy_s(nid.szTip, sizeof(nid.szTip) / sizeof(wchar_t), L"Mouse Wrap");

Shell_NotifyIcon(NIM_ADD, &nid);
Expand Down Expand Up @@ -50,7 +54,7 @@ void TaskBarCheckClick(LPARAM lParam, HWND hwnd)
ShowContextMenu(hwnd);

else if (LOWORD(lParam) == WM_LBUTTONUP)
MessageBox(hwnd, L"Tray icon clicked!", L"Info", MB_OK | MB_ICONINFORMATION);
IconClicked(hwnd);
}

void TaskBarCheckCommand(WORD cmd)
Expand All @@ -75,6 +79,14 @@ void TaskBarCheckCommand(WORD cmd)
}
}

void IconClicked(HWND hwnd)
{
ToggleWrapEnabled(hwnd);
// Swap tray icon
nid.hIcon = LoadIcon(hInstMain, MAKEINTRESOURCE(wrapEnabled ? IDI_ICON_ENABLED : IDI_ICON_DISABLED));
Shell_NotifyIcon(NIM_MODIFY, &nid);
}

void CleanUpTrayIcon()
{
Shell_NotifyIcon(NIM_DELETE, &nid);
Expand Down
1 change: 1 addition & 0 deletions v4.0/src/taskbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ void ShowContextMenu(HWND hwnd);
void CreateContextMenu();
void TaskBarCheckCommand(WORD cmd);
void TaskBarCheckClick(LPARAM lParam, HWND hwnd);
void IconClicked();
void CleanUpTrayIcon();

#endif // TASKBAR_H

0 comments on commit e7fa298

Please sign in to comment.