Jump to content
  • Advertisement
Carlos Hdez Barbera

C++ GetDlgCtrlID and GetLastError returning 0 - Win32 Game

Recommended Posts

I'm doing a WIN32 application (kind of a table game like Tic-Tac-Toe) and I need to keep track (or get in someway) the ID of the different statics elements to change the colors.

The problem comes because the function GetDlgCtrlID is returning 0 and GetLastError is returning also a non error value.

This is the class where I'm trying to get the ID and save it

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;
}

And this is the code that create the element and the "Casilla" to keep track of the 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);
}

Hope you can help me!

Share this post


Link to post
Share on other sites
Advertisement

Microsoft Docs (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-getdlgctrlid) say:

Quote

An application sets the identifier for a child window when it creates the window by assigning the identifier value to the hmenu parameter when calling the CreateWindow or CreateWindowEx function.

You call CreateWindow (https://docs.microsoft.com/en-us/windows/desktop/api/winuser/nf-winuser-createwindowa) with NULL as hMenu, so it has the ID 0 and this is what the function returns.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!