📜  显示鼠标 c++ (1)

📅  最后修改于: 2023-12-03 15:40:10.771000             🧑  作者: Mango

用C++显示鼠标

当我们在开发软件时,有时候需要在屏幕上显示鼠标位置,这时我们就需要了解如何去获取鼠标位置。在C++中,我们可以通过WinAPI中的GetCursorPos()函数来获取当前鼠标位置。

获取鼠标位置

要使用GetCursorPos()函数,我们需要引入Windows.h头文件。下面是一个简单的示例代码,演示了如何使用GetCursorPos()函数获取鼠标位置。

#include <iostream>
#include <Windows.h>

int main()
{
    POINT pt;
    if (GetCursorPos(&pt)) {
        std::cout << "Cursor position: (" << pt.x << ", " << pt.y << ")" << std::endl;
    }
    else {
        std::cerr << "Failed to get cursor position." << std::endl;
    }
    return 0;
}

输出:

Cursor position: (453, 234)
显示鼠标位置

一旦我们获取了鼠标位置,我们就可以将其显示在屏幕上。在Windows上,我们可以使用GDI(图形设备接口)来绘制图形。下面是一个示例代码,使用了GDI绘制了一个白色的十字形,并在其中心显示了鼠标位置。

#include <iostream>
#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        // 绘制白色的十字形
        HPEN pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
        HPEN oldPen = (HPEN)SelectObject(hdc, pen);
        HBRUSH brush = (HBRUSH)GetStockObject(NULL_BRUSH);
        HBRUSH oldBrush = (HBRUSH)SelectObject(hdc, brush);
        int x = GetSystemMetrics(SM_CXSCREEN);
        int y = GetSystemMetrics(SM_CYSCREEN);
        MoveToEx(hdc, x / 2, 0, NULL);
        LineTo(hdc, x / 2, y);
        MoveToEx(hdc, 0, y / 2, NULL);
        LineTo(hdc, x, y / 2);
        SelectObject(hdc, oldPen);
        DeleteObject(pen);
        SelectObject(hdc, oldBrush);
        DeleteObject(brush);

        // 获取鼠标位置并显示
        POINT pt;
        if (GetCursorPos(&pt)) {
            SetTextColor(hdc, RGB(255, 255, 255));
            SetBkMode(hdc, TRANSPARENT);
            TCHAR buf[256];
            wsprintf(buf, _T("(%d, %d)"), pt.x, pt.y);
            TextOut(hdc, pt.x, pt.y, buf, _tcslen(buf));
        }

        EndPaint(hWnd, &ps);
        break;
    }
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    // 注册窗口类
    WNDCLASS wc;
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wc.lpszMenuName = NULL;
    wc.lpszClassName = _T("MyWindowClass");
    if (!RegisterClass(&wc)) {
        MessageBox(NULL, _T("Failed to register window class."), _T("Error"), MB_OK | MB_ICONERROR);
        return 1;
    }

    // 创建窗口并显示
    HWND hWnd = CreateWindow(_T("MyWindowClass"), _T("Mouse Position"),
        WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
        NULL, NULL, hInstance, NULL);
    if (hWnd == NULL) {
        MessageBox(NULL, _T("Failed to create window."), _T("Error"), MB_OK | MB_ICONERROR);
        return 1;
    }
    ShowWindow(hWnd, nCmdShow);

    // 消息循环
    MSG msg;
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}

运行程序后,我们将在屏幕上看到一个显示鼠标位置的窗口。

显示鼠标位置