我的代码是否存在阻止 VK_UP 事件发生的问题?

问题描述 投票:0回答:1

所以我一直在编写一个函数,当按下UP键时,将hEdit中的文本设置为“哎呀,它起来了”,但它根本不起作用。就像当我按下这个键时,没有任何反应,我什至尝试使用 MessageBox() 函数而不是 SetWindowtextW(),但它仍然没有做任何事情。 smn 可以帮我找出问题吗?

 case WM_KEYDOWN:
{
      switch(wp)
     {
      case VK_UP:
      
        SetWindowTextW(hEdit, L"Oops it's up");
      
    break;
    }
break;
}

这是完整的代码:

#include <windows.h>
#include <algorithm>

#define VK_UP    0x26



using namespace std;

LRESULT CALLBACK MyWindowProcedure(HWND, UINT, WPARAM, LPARAM);
void AddControls(HWND hWnd);

HWND hEdit;

int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nCmdShow)
{
    MSG msg = { 0 };
    WNDCLASSW wc = { 0 };

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_HAND);
    wc.hInstance = hInst;
    wc.lpszClassName = L"myWindowClass";
    wc.lpfnWndProc = MyWindowProcedure;

    if (!RegisterClassW(&wc))
        return -1;

    CreateWindowW(L"myWindowClass", L"my Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500,
        NULL, NULL, NULL, NULL);

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

    return 0;
}

LRESULT CALLBACK MyWindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    static bool timerActive = false;

    switch (msg)
    {
    case WM_KEYDOWN:
        switch (wp)
        {
        case VK_UP:
            MessageBox(NULL, L"key up", L"", MB_OK);
            break;
        }

        break;
    case WM_LBUTTONDOWN:
    {
        if (!timerActive)
        {
            wchar_t user_input[100];
            GetWindowTextW(hEdit, user_input, 100);

            POINT mousePos;
            GetCursorPos(&mousePos);
            ScreenToClient(hWnd, &mousePos);

            HDC hdc = GetDC(hWnd);
            TextOutW(hdc, mousePos.x, mousePos.y, user_input, wcslen(user_input));
            ReleaseDC(hWnd, hdc);

            // Set a timer to erase the text after 500 milliseconds
            SetTimer(hWnd, 1, 400, NULL);
            timerActive = true;
        }
        break;
    }

    case WM_RBUTTONDOWN:
    {
        if (!timerActive)
        {
            wchar_t user_reverse_input[100];
            GetWindowTextW(hEdit, user_reverse_input, 100);
            reverse(user_reverse_input, user_reverse_input + wcslen(user_reverse_input));

            POINT mousePos;
            GetCursorPos(&mousePos);
            ScreenToClient(hWnd, &mousePos);

            HDC hdc = GetDC(hWnd);
            TextOutW(hdc, mousePos.x, mousePos.y, user_reverse_input, wcslen(user_reverse_input));
            ReleaseDC(hWnd, hdc);

            // Set a timer to erase the reversed text after 500 milliseconds
            SetTimer(hWnd, 1, 400, NULL);
            break;
        }
    }

    case WM_TIMER:
    {
        if (wp == 1)
        {
            KillTimer(hWnd, 1);
            InvalidateRect(hWnd, NULL, TRUE);
            timerActive = false;
        }
        break;
    }



    case WM_CREATE:
        AddControls(hWnd);
        SetFocus(hEdit);
        break;

    case WM_DESTROY:
        PostQuitMessage(0);
        break;

    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }

    return 0;
}

void AddControls(HWND hWnd)
{
    HWND hStatic = CreateWindowW(L"Static", L"Enter text here:", WS_VISIBLE | WS_CHILD, 10, 10, 220, 30, hWnd, NULL, NULL, NULL);

    if (hStatic == NULL)
    {
        MessageBoxW(NULL, L"Static control creation failed!", L"Error", MB_ICONERROR);
        return;
    }

    hEdit = CreateWindowW(L"Edit", L"", WS_VISIBLE | WS_CHILD | WS_BORDER | ES_MULTILINE | ES_AUTOVSCROLL, 10, 45, 170, 100, hWnd, NULL, NULL, NULL);

    if (hEdit == NULL)
    {
        MessageBoxW(NULL, L"Edit control creation failed!", L"Error", MB_ICONERROR);
        return;
    }
}


c++ windows winapi
1个回答
-1
投票

我会从代码中删除“#define VK_UP 0x26”行,因为 VK_UP 应该已经在您的系统中定义。 如果这没有帮助,请尝试在“switch (wp)”处放置一个断点,并检查您获得的向上箭头按键的实际参数 (wp)。

© www.soinside.com 2019 - 2024. All rights reserved.