GetDlgCtrlID和GetLastError返回0

时间:2018-10-27 10:31:05

标签: c++ winapi error-handling desktop

我正在做WIN32应用程序,需要跟踪(或以某种方式获取)不同statics元素的ID才能更改颜色。

出现此问题是因为函数GetDlgCtrlID返回0,而GetLastError也返回非错误值。

这是我试图获取ID并将其保存的课程。

Casilla::Casilla(HWND* window, std::set<int>* stackToEnable, std::set<int>* stackToDisable)
{
    m_window = window;
    m_wID = GetDlgCtrlID(*window);
    std::string str = GetLastErrorAsString();
    m_setToEnable = stackToEnable;
    m_setToDisable = stackToDisable;
}

DWORD WINAPI Casilla::Enable()
{
    InvalidateRect(*m_window, NULL, TRUE);
    m_setToEnable->insert(m_wID);
    return 0;
}

DWORD WINAPI Casilla::Disable()
{
    InvalidateRect(*m_window, NULL, TRUE);
    m_setToDisable->insert(m_wID);
    return 0;
}

//Returns the last Win32 error, in string format. Returns an empty string if there is no error.
std::string Casilla::GetLastErrorAsString()
{
    //Get the error message, if any.
    DWORD errorMessageID = ::GetLastError();
    if (errorMessageID == 0)
        return std::string(); //No error message has been recorded

    LPSTR messageBuffer = nullptr;
    size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);

    std::string message(messageBuffer, size);

    //Free the buffer.
    LocalFree(messageBuffer);

    return message;
}

这是创建元素和“ Casilla”以跟踪ID的代码。

void AddSlot(HWND hWnd)
{   
    HWND tmp = CreateWindowW(L"static", L"TO CHANGE COLOR", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER, 500, 500, 100, 100, hWnd, NULL, NULL, NULL);
    Casilla* cas = new Casilla(&tmp, windowsToChangeColorToEnable, windowsToChangeColorToDisable);
}

0 个答案:

没有答案
相关问题