Visual studio wont work with “MoveTo” (win32api)

不想你离开。 提交于 2021-02-17 07:11:23

问题


Here's the code below, VS said that on line 109 ( MoveTo(hdc, x, y);
"MoveTo" is undefined.

Can someone explain me what's wrong ? This is a win32api application.

// Win32Project.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "Win32Project4.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

LRESULT CALLBACK ChildWndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int pNum = 0;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    const WCHAR szAppName[] = L"Third_LAB";
    const WCHAR szChildClass[] = L"Child";

    HWND hwnd;
    MSG msg;
    WNDCLASSEX wndclass;
    wndclass.cbSize = sizeof(wndclass);
    wndclass.style = CS_HREDRAW | CS_VREDRAW;
    wndclass.lpfnWndProc = WndProc;
    wndclass.cbClsExtra = 0;
    wndclass.cbWndExtra = 0;
    wndclass.hInstance = hInstance;
    wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wndclass.hCursor = LoadCursor(NULL, IDC_CROSS);
    wndclass.hbrBackground = (HBRUSH)(NULL_BRUSH);
    wndclass.lpszMenuName = NULL;
    wndclass.lpszClassName = szAppName;
    wndclass.hIconSm = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_SMALL));
    RegisterClassEx(&wndclass);
    wndclass.lpfnWndProc = ChildWndProc;
    wndclass.cbWndExtra = sizeof(WORD);
    wndclass.hIcon = NULL;
    wndclass.lpszClassName = szChildClass;
    wndclass.hIconSm = NULL;
    RegisterClassEx(&wndclass);
    hwnd = CreateWindowEx(
        0,
        szAppName, // window class name
        L"Third_LAB", // window caption
        WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL, // window style
        200, // initial x position
        150, // initial y position
        800, // initial x size
        500, // initial y size
        NULL, // parent window handle
        NULL, // window menu handle
        hInstance, // program instance handle
        NULL
    ); // creation parameters
    ShowWindow(hwnd, iCmdShow);
    UpdateWindow(hwnd);

    while (GetMessage(&msg, 0, 0, 0))
        DispatchMessage(&msg);


    return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    {
        const WCHAR szChildClass[] = L"Child";
        static int cxBlock, cyBlock;
        static HDC hdc;
        static HPEN hPen;
        static BOOL track = FALSE;
        static HWND hwndCH;
        static  int x, y;

        switch (iMsg) {
        case WM_CREATE:
            hdc = GetDC(hwnd);

            hwndCH = CreateWindow(szChildClass, NULL,
                WS_CHILDWINDOW | WS_VISIBLE,
                0, 0, cxBlock / 2 + 3, cyBlock + 3,
                hwnd, NULL,
                (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE),
                NULL);
            MoveWindow(hwndCH, 0, 0, cxBlock / 2 + 3, cyBlock + 3, NULL);
            return 0;

        case WM_SIZE:

            cxBlock = LOWORD(lParam);
            cyBlock = HIWORD(lParam);
            MoveWindow(hwndCH, 0, 0, cxBlock / 2 + 3, cyBlock + 3, NULL);
            return 0;

        case WM_KEYDOWN:
            switch (wParam)
            {
            case (0x51):
                PostQuitMessage(0);
                break;
            }
            return 0;
        case WM_LBUTTONDOWN:
            track = TRUE;
            x = LOWORD(lParam);
            y = HIWORD(lParam);
            MoveTo(hdc, x, y);
            switch (pNum)
            {
            case (1):
                hPen = CreatePen(PS_SOLID, 5, 0);
                SelectObject(hdc, hPen); break;
            case (2):
                hPen = CreatePen(PS_SOLID, 15, 0);
                SelectObject(hdc, hPen); break;
            case (3):
                hPen = CreatePen(PS_SOLID, 25, 0);
                SelectObject(hdc, hPen); break;
            }
            return 0;
        case WM_LBUTTONUP:
            track = FALSE;
            DeleteObject(hPen);
            return 0;
        case WM_MOUSEMOVE:
            if (track)
            {
                x = LOWORD(lParam);
                y = HIWORD(lParam);
                LineTo(hdc, x, y);
                MoveTo(hdc, x, y);
            }
            return 0;
        case WM_DESTROY:
            ReleaseDC(hwnd, hdc);

            PostQuitMessage(0);
            return 0;
        }
        return DefWindowProc(hwnd, iMsg, wParam, lParam);
    }
}
LRESULT CALLBACK ChildWndProc(HWND hwnd, UINT iMsg, WPARAM wParam, LPARAM lParam)
{
    static HDC hdc;
    PAINTSTRUCT ps;
    static int cxBlock, cyBlock;
    static int x, y;
    static HPEN hPen;
    switch (iMsg)
    {

    case WM_SIZE:
        hdc = GetDC(hwnd);
        cxBlock = LOWORD(lParam);
        cyBlock = HIWORD(lParam);
        InvalidateRect(hwnd, NULL, TRUE);
        ReleaseDC(hwnd, hdc);
        return 0;

    case WM_PAINT:
        hdc = BeginPaint(hwnd, &ps);
        SetROP2(hdc, R2_BLACK);
        Rectangle(hdc, 0, cyBlock / 2 - 100, cxBlock, cyBlock / 2 - 105);
        Rectangle(hdc, 0, cyBlock / 2, cxBlock, cyBlock / 2 - 15);
        Rectangle(hdc, 0, cyBlock / 2 + 100, cxBlock, cyBlock / 2 + 125);
        InvalidateRect(hwnd, NULL, FALSE);
        break;
    case WM_RBUTTONDOWN:
        x = LOWORD(lParam);
        y = HIWORD(lParam);

        if ((y <= (cyBlock / 2 - 100)) && (y >= (cyBlock / 2 - 105)))
            pNum = 1;
        else if ((y <= (cyBlock / 2)) && (y >= (cyBlock / 2 - 15)))
            pNum = 2;
        else if ((y >= (cyBlock / 2 + 100)) && (y <= (cyBlock / 2 + 125)))
            pNum = 3;
        return 0;
    case WM_DESTROY:
        DeleteObject(hPen);
        ReleaseDC(hwnd, hdc);
        DestroyWindow(hwnd);
        return 0;
    }
    return DefWindowProc(hwnd, iMsg, wParam, lParam);

Waiting for a fast response, I would be very glad.


回答1:


MoveTo() is a Windows 3.x function that is no longer supported. Use MoveToEx() instead, setting the last argument to NULL. You can define your own MoveTo() to do that, eg:

#define MoveTo(hdc, x, y) ((void)MoveToEx(hdc, x, y, NULL))

Or:

inline void MoveTo(HDC hdc, int X, int Y) { MoveToEx(hdc, X, Y, NULL); }


来源:https://stackoverflow.com/questions/43078884/visual-studio-wont-work-with-moveto-win32api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!