从Win32编辑框获取输入[C ++]

时间:2014-09-10 00:00:17

标签: c++ winapi

大家好,我一直试图从Win32文本编辑框中获取输入,如下所示:

    char IP_ADDR[200];
    char NICK[200];
    #define Print(msg) print_line(*hwnd, msg)
    #define Println() Print("\r\n")


        Print("Welcome to Basic Client 0.01 Alpha!");
        Println();
        Print("Please enter your IP address");
        Println();
        for (;; Sleep(500))
        {
            if (IsKeyDown(VK_RETURN))
            {
                GetDlgItemText(*hwnd, IDC_MAIN_EDIT2, IP_ADDR, sizeof(IP_ADDR));
                SetDlgItemText(*hwnd, IDC_MAIN_EDIT2, "");
                break;
            }
        }
        Print("Please enter a nickname");
        Println();

        for (;; Sleep(500))
        {
            if (IsKeyDown(VK_RETURN))
            {
                GetDlgItemText(*hwnd, IDC_MAIN_EDIT2, NICK, sizeof(NICK));
                SetDlgItemText(*hwnd, IDC_MAIN_EDIT2, "");
                break;
            }
        }

但这不会进入循环或停止它直到键被按下它只是继续前进如果它有助于我有一个运行函数,这是代码来自一个新的线程,以便它可以做到这一点,同时管理这里的窗口消息是这个代码:

Client * c = new Client;
.....
int Run(HWND *hwnd)
{
    MSG msg = { 0 };
    while (msg.message != WM_QUIT)
    {
        if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {

            TranslateMessage(&msg);
            DispatchMessage(&msg);
            _beginthreadex(0, 0, ClientThread, (void*)hwnd, 0, 0);

        }


    }

    return 1;
}

unsigned int __stdcall ClientThread(void*data)
{

    HWND *hwnd = (HWND*)data;


    c->Run(hwnd);
    return 0;
}

这是IsKeyDown函数:

bool Client::IsKeyDown(int vk)
{
    return ((GetAsyncKeyState(vk) & 0x1) != 0)? true:false;
}

1 个答案:

答案 0 :(得分:0)

哇我的坏人我把missThread放在Message循环中,就像Retired Ninja指出的那样,这就是造成所有问题的原因。