坐标计算程序

时间:2018-10-18 16:58:53

标签: windows visual-c++ win32gui

我想为Windows创建一个简单的坐标计算程序: application

可以通过鼠标左键移动X_Line(红色)和Y_Line(蓝色)并显示坐标。

但是在这种情况下,我需要帮助来使坐标零件和功能生效。

请帮助我完成程序的任何部分!?!

感谢您的帮助,

1 个答案:

答案 0 :(得分:0)

处理WM_LBUTTONDOWN并记录鼠标位置,然后发送绘制消息以在该坐标处绘制。

#include <Windows.h>

LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    static int xcoord = -1;
    static int ycoord = -1;
    switch(msg)
    {
    case WM_PAINT:
    {
        PAINTSTRUCT ps;
        HDC hdc = BeginPaint(hWnd, &ps);

        if(xcoord >= 0 && ycoord >= 0)
        {
            RECT rc;
            GetClientRect(hWnd, &rc);

            //draw horizontal line
            MoveToEx(hdc, 0, ycoord, NULL);
            LineTo(hdc, rc.right, ycoord);

            //draw vertical line
            MoveToEx(hdc, xcoord, 0, NULL);
            LineTo(hdc, xcoord, rc.bottom);
        }

        EndPaint(hWnd, &ps);
        return 0;
    }

    case WM_LBUTTONDOWN:
    {
        xcoord = ((int)(short)LOWORD(lparam));
        ycoord = ((int)(short)HIWORD(lparam));
        InvalidateRect(hWnd, NULL, TRUE);
        return 0;
    }

    case WM_DESTROY:

        PostQuitMessage(0);
        return 0;
    }

    return DefWindowProc(hWnd, msg, wparam, lparam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int)
{
    WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
    wcex.lpfnWndProc = WndProc;
    wcex.hInstance = hInstance;
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszClassName = TEXT("classname");
    RegisterClassEx(&wcex);

    CreateWindow(wcex.lpszClassName, NULL, WS_VISIBLE | WS_OVERLAPPEDWINDOW,
        0, 0, 600, 400, 0, 0, hInstance, 0);

    MSG msg;
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return (int)msg.wParam;
}