Advertisement
alien_fx_fiend

Mouse-Spotter (TrayMenu Is Working) *FINAL RELEASE*

Dec 4th, 2024 (edited)
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.06 KB | Source Code | 0 0
  1. ==++ Here's the full source code for (file 1/1) "source.cpp":: ++==
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <shellapi.h>
  6. #include "resource.h"
  7.  
  8. #define WINDOW_CLASS_NAME L"MouseSpotterClass"
  9. #define WINDOW_TITLE L"MouseSpotter"
  10. #define WM_TRAYICON (WM_USER + 1)
  11. #define ID_TRAYICON 1001
  12. #define ID_TRAY_EXIT 1002
  13. #define ID_TRAY_TOGGLE 1003
  14. #define ID_TRAY_ABOUT 1004
  15.  
  16. bool g_isEnabled = true;
  17. COLORREF g_spotterColor = RGB(255, 0, 0);
  18. std::vector<HWND> g_excludedWindows;
  19. int g_spotterSize = 64;
  20.  
  21. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
  22. void EnableDebugPrivileges();
  23. bool IsWindowExcluded(HWND windowHandle);
  24. void DrawSpotter(HWND hwnd, HDC hdc);
  25.  
  26. class MouseSpotter {
  27. private:
  28.    HWND m_hwnd;
  29.    HINSTANCE m_hInstance;
  30.    bool m_isRunning;
  31.    NOTIFYICONDATA m_nid;
  32.  
  33.    void InitializeTrayIcon() {
  34.        ZeroMemory(&m_nid, sizeof(NOTIFYICONDATA));
  35.        m_nid.cbSize = sizeof(NOTIFYICONDATA);
  36.        m_nid.hWnd = m_hwnd;
  37.        m_nid.uID = ID_TRAYICON;
  38.        m_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
  39.        m_nid.uCallbackMessage = WM_TRAYICON;
  40.        m_nid.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_ICON1));
  41.        wcscpy_s(m_nid.szTip, L"MouseSpotter");
  42.  
  43.        if (!Shell_NotifyIcon(NIM_ADD, &m_nid)) {
  44.            MessageBox(NULL, L"Failed to add tray icon!", L"Error", MB_ICONERROR | MB_OK);
  45.        }
  46.    }
  47.  
  48.    void ShowTrayMenu() {
  49.        if (!m_hwnd) {
  50.            MessageBox(NULL, L"Invalid window handle!", L"Error", MB_ICONERROR | MB_OK);
  51.            return;
  52.        }
  53.  
  54.        // Get the cursor position
  55.        POINT curPoint;
  56.        GetCursorPos(&curPoint);
  57.  
  58.        // Create the popup menu
  59.        HMENU hPopMenu = CreatePopupMenu();
  60.        if (!hPopMenu) {
  61.            MessageBox(NULL, L"Failed to create tray menu!", L"Error", MB_ICONERROR | MB_OK);
  62.            return;
  63.        }
  64.  
  65.        // Add menu items
  66.        InsertMenu(hPopMenu, 0, MF_BYPOSITION | MF_STRING, ID_TRAY_TOGGLE, g_isEnabled ? L"Disable" : L"Enable");
  67.        InsertMenu(hPopMenu, 1, MF_BYPOSITION | MF_STRING, ID_TRAY_ABOUT, L"About");
  68.        InsertMenu(hPopMenu, 2, MF_BYPOSITION | MF_SEPARATOR, 0, NULL);
  69.        InsertMenu(hPopMenu, 3, MF_BYPOSITION | MF_STRING, ID_TRAY_EXIT, L"Exit");
  70.  
  71.        // Ensure the window is in the foreground
  72.        SetForegroundWindow(m_hwnd);
  73.  
  74.        // Display the menu
  75.        TrackPopupMenu(hPopMenu, TPM_LEFTALIGN | TPM_RIGHTBUTTON, curPoint.x, curPoint.y, 0, m_hwnd, NULL);
  76.  
  77.        // Post a dummy message to ensure the menu works correctly
  78.        PostMessage(m_hwnd, WM_NULL, 0, 0);
  79.  
  80.        // Destroy the menu after use
  81.        DestroyMenu(hPopMenu);
  82.    }
  83.  
  84. public:
  85.    MouseSpotter(HINSTANCE hInstance) : m_hInstance(hInstance), m_isRunning(true) {
  86.        InitializeWindow();
  87.    }
  88.  
  89.    ~MouseSpotter() {
  90.        Shell_NotifyIcon(NIM_DELETE, &m_nid);
  91.        if (m_hwnd) {
  92.            DestroyWindow(m_hwnd);
  93.        }
  94.        UnregisterClass(WINDOW_CLASS_NAME, m_hInstance);
  95.    }
  96.  
  97.    void HandleTrayMessage(WPARAM wParam, LPARAM lParam) {
  98.        switch (lParam) {
  99.        case WM_RBUTTONUP:
  100.        case WM_CONTEXTMENU:
  101.            ShowTrayMenu();
  102.            break;
  103.        case WM_LBUTTONDBLCLK:
  104.            g_isEnabled = !g_isEnabled;
  105.            if (g_isEnabled) {
  106.                ShowWindow(m_hwnd, SW_SHOW);
  107.            }
  108.            else {
  109.                ShowWindow(m_hwnd, SW_HIDE);
  110.            }
  111.            break;
  112.        }
  113.    }
  114.  
  115.    bool InitializeWindow() {
  116.        WNDCLASSEX wc = { 0 };
  117.        wc.cbSize = sizeof(WNDCLASSEX);
  118.        wc.lpfnWndProc = WindowProc;
  119.        wc.hInstance = m_hInstance;
  120.        wc.lpszClassName = WINDOW_CLASS_NAME;
  121.        wc.hbrBackground = NULL;
  122.        wc.hCursor = LoadCursor(NULL, IDC_CROSS);
  123.        wc.hIcon = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_ICON1));
  124.        wc.hIconSm = LoadIcon(m_hInstance, MAKEINTRESOURCE(IDI_ICON1));
  125.  
  126.        if (!RegisterClassEx(&wc)) {
  127.            MessageBox(NULL, L"Window Registration Failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
  128.            return false;
  129.        }
  130.  
  131.        m_hwnd = CreateWindowEx(
  132.            WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_LAYERED | WS_EX_TOOLWINDOW,
  133.            WINDOW_CLASS_NAME,
  134.            WINDOW_TITLE,
  135.            WS_POPUP,
  136.            0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
  137.            NULL, NULL, m_hInstance, this
  138.        );
  139.  
  140.        if (!m_hwnd) {
  141.            MessageBox(NULL, L"Window Creation Failed!", L"Error", MB_ICONEXCLAMATION | MB_OK);
  142.            return false;
  143.        }
  144.  
  145.        SetLayeredWindowAttributes(m_hwnd, RGB(0, 0, 0), 255, LWA_COLORKEY);
  146.        InitializeTrayIcon();
  147.        return true;
  148.    }
  149.  
  150.    void Run() {
  151.        ShowWindow(m_hwnd, SW_SHOW);
  152.        UpdateWindow(m_hwnd);
  153.  
  154.        MSG msg;
  155.        POINT cursorPos;
  156.        RECT spotterRect;
  157.  
  158.        while (m_isRunning) {
  159.            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
  160.                if (msg.message == WM_QUIT) {
  161.                    m_isRunning = false;
  162.                    break;
  163.                }
  164.                TranslateMessage(&msg);
  165.                DispatchMessage(&msg);
  166.            }
  167.  
  168.            if (!g_isEnabled) {
  169.                Sleep(100);
  170.                continue;
  171.            }
  172.  
  173.            GetCursorPos(&cursorPos);
  174.            HWND foregroundWindow = GetForegroundWindow();
  175.            if (!IsWindowExcluded(foregroundWindow)) {
  176.                spotterRect.left = cursorPos.x - (g_spotterSize / 2);
  177.                spotterRect.top = cursorPos.y - (g_spotterSize / 2);
  178.                spotterRect.right = spotterRect.left + g_spotterSize;
  179.                spotterRect.bottom = spotterRect.top + g_spotterSize;
  180.  
  181.                SetWindowPos(m_hwnd, HWND_TOPMOST,
  182.                    spotterRect.left, spotterRect.top,
  183.                    g_spotterSize, g_spotterSize,
  184.                    SWP_NOACTIVATE);
  185.                InvalidateRect(m_hwnd, NULL, TRUE);
  186.            }
  187.            Sleep(1);
  188.        }
  189.    }
  190.  
  191.    HWND GetHWND() const { return m_hwnd; }
  192. };
  193.  
  194. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  195.    static MouseSpotter* pSpotter = nullptr;
  196.  
  197.    if (uMsg == WM_CREATE) {
  198.        CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
  199.        pSpotter = reinterpret_cast<MouseSpotter*>(pCreate->lpCreateParams);
  200.    }
  201.  
  202.    switch (uMsg) {
  203.    case WM_TRAYICON:
  204.        if (pSpotter) {
  205.            pSpotter->HandleTrayMessage(wParam, lParam);
  206.        }
  207.        return 0;
  208.  
  209.    case WM_COMMAND:
  210.        switch (LOWORD(wParam)) {
  211.        case ID_TRAY_EXIT:
  212.            PostQuitMessage(0);
  213.            return 0;
  214.        case ID_TRAY_TOGGLE:
  215.            g_isEnabled = !g_isEnabled;
  216.            if (g_isEnabled) {
  217.                ShowWindow(hwnd, SW_SHOW);
  218.            }
  219.            else {
  220.                ShowWindow(hwnd, SW_HIDE);
  221.            }
  222.            return 0;
  223.        case ID_TRAY_ABOUT:
  224.            MessageBox(NULL, L"Ctrl+Alt+C to Toggle\nAlt+F3 to Exit", L"Hotkey Info", MB_ICONINFORMATION | MB_OK);
  225.            return 0;
  226.        }
  227.        break;
  228.  
  229.  
  230.    case WM_PAINT: {
  231.        PAINTSTRUCT ps;
  232.        HDC hdc = BeginPaint(hwnd, &ps);
  233.        DrawSpotter(hwnd, hdc);
  234.        EndPaint(hwnd, &ps);
  235.        return 0;
  236.    }
  237.  
  238.    case WM_DESTROY:
  239.        UnregisterHotKey(hwnd, 1);
  240.        UnregisterHotKey(hwnd, 2);
  241.        UnregisterHotKey(hwnd, 3);
  242.        PostQuitMessage(0);
  243.        return 0;
  244.  
  245.    case WM_KEYDOWN:
  246.        switch (wParam) {
  247.        case VK_F1:
  248.            MessageBox(NULL, L"Ctrl+Alt+C to Toggle\nAlt+F3 to Exit", L"Hotkey Info", MB_ICONINFORMATION | MB_OK);
  249.            return 0;
  250.        }
  251.        break;
  252.  
  253.    case WM_CREATE: {
  254.        CREATESTRUCT* pCreate = reinterpret_cast<CREATESTRUCT*>(lParam);
  255.        pSpotter = reinterpret_cast<MouseSpotter*>(pCreate->lpCreateParams);
  256.        RegisterHotKey(hwnd, 1, MOD_CONTROL | MOD_ALT, 'C');
  257.        RegisterHotKey(hwnd, 2, MOD_ALT, VK_F3);
  258.        RegisterHotKey(hwnd, 3, 0, VK_F1);
  259.        return 0;
  260.    }
  261.  
  262.    case WM_HOTKEY:
  263.        if (wParam == 1) {
  264.            g_isEnabled = !g_isEnabled;
  265.            if (g_isEnabled) {
  266.                ShowWindow(hwnd, SW_SHOW);
  267.            }
  268.            else {
  269.                ShowWindow(hwnd, SW_HIDE);
  270.            }
  271.        }
  272.        else if (wParam == 2) {
  273.            PostQuitMessage(0);
  274.        }
  275.        else if (wParam == 3) {
  276.            MessageBox(NULL, L"Ctrl+Alt+C to Toggle\nAlt+F3 to Exit", L"Hotkey Info", MB_ICONINFORMATION | MB_OK);
  277.        }
  278.        return 0;
  279.    }
  280.    return DefWindowProc(hwnd, uMsg, wParam, lParam);
  281. }
  282.  
  283. //transparent crosshair
  284. void DrawSpotter(HWND hwnd, HDC hdc) {
  285.    SetBkMode(hdc, TRANSPARENT);
  286.  
  287.    // Create a pen for drawing
  288.    HPEN hPen = CreatePen(PS_SOLID, 2, g_spotterColor);
  289.    HPEN hOldPen = (HPEN)SelectObject(hdc, hPen);
  290.  
  291.    // Create a hollow brush for transparent circle
  292.    HBRUSH hBrush = (HBRUSH)GetStockObject(HOLLOW_BRUSH);
  293.    HBRUSH hOldBrush = (HBRUSH)SelectObject(hdc, hBrush);
  294.  
  295.    int centerX = g_spotterSize / 2;
  296.    int centerY = g_spotterSize / 2;
  297.    int radius = g_spotterSize / 4;
  298.  
  299.    // Draw the circle first
  300.    Ellipse(hdc, centerX - radius, centerY - radius,
  301.        centerX + radius, centerY + radius);
  302.  
  303.    // Draw crosshair over the circle
  304.    MoveToEx(hdc, centerX, 0, NULL);
  305.    LineTo(hdc, centerX, g_spotterSize);
  306.    MoveToEx(hdc, 0, centerY, NULL);
  307.    LineTo(hdc, g_spotterSize, centerY);
  308.  
  309.    // Clean up
  310.    SelectObject(hdc, hOldPen);
  311.    SelectObject(hdc, hOldBrush);
  312.    DeleteObject(hPen);
  313. }
  314.  
  315. //crosshair black
  316. /*void DrawSpotter(HWND hwnd, HDC hdc) {
  317.    RECT rect;
  318.    GetClientRect(hwnd, &rect);
  319.  
  320.    // Create pen for drawing
  321.    HPEN hPen = CreatePen(PS_SOLID, 2, g_spotterColor);
  322.    SelectObject(hdc, hPen);
  323.  
  324.    // Calculate center point
  325.    int centerX = g_spotterSize / 2;
  326.    int centerY = g_spotterSize / 2;
  327.  
  328.    // Draw vertical line
  329.    MoveToEx(hdc, centerX, 0, NULL);
  330.    LineTo(hdc, centerX, g_spotterSize);
  331.  
  332.    // Draw horizontal line
  333.    MoveToEx(hdc, 0, centerY, NULL);
  334.    LineTo(hdc, g_spotterSize, centerY);
  335.  
  336.    // Clean up
  337.    DeleteObject(hPen);
  338. }*/
  339.  
  340. //red circle
  341. /*void DrawSpotter(HWND hwnd, HDC hdc) {
  342.    RECT rect;
  343.    GetClientRect(hwnd, &rect);
  344.  
  345.    // Create pen and brush for drawing
  346.    HPEN hPen = CreatePen(PS_SOLID, 2, g_spotterColor);
  347.    HBRUSH hBrush = CreateSolidBrush(g_spotterColor);
  348.  
  349.    SelectObject(hdc, hPen);
  350.    SelectObject(hdc, hBrush);
  351.  
  352.    // Draw crosshair or circle
  353.    Ellipse(hdc, 0, 0, g_spotterSize, g_spotterSize);
  354.  
  355.    // Clean up
  356.    DeleteObject(hPen);
  357.    DeleteObject(hBrush);
  358. }*/
  359.  
  360. bool IsWindowExcluded(HWND windowHandle) {
  361.    return std::find(g_excludedWindows.begin(), g_excludedWindows.end(), windowHandle) != g_excludedWindows.end();
  362. }
  363.  
  364. int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR pCmdLine, int nCmdShow) {
  365.    MouseSpotter spotter(hInstance);
  366.    spotter.Run();
  367.    return 0;
  368. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement