Mizuna
Mizuna

Reputation: 53

win32 SetDlgItemText not working from within WM_INITDIALOG

I'm just starting to experiment with win32 and I've run into a problem.

BOOL CALLBACK UnsavedChangesProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HWND dHandle = GetActiveWindow();

switch (msg)
{
case WM_INITDIALOG:
    MessageBox(NULL, "In InitDialog", 0, 0);
    SetDlgItemText(dHandle, 1004, ("There are unsaved changes to \""));
    char error[10];
    sprintf_s(error, "%d", GetLastError());
    MessageBox(NULL, error, 0, 0);
    return TRUE;
case WM_COMMAND:
    switch (LOWORD(wParam))
    {
    case IDSAVE:
        DoFileSave(hwnd);
        EndDialog(hwnd, TRUE);
        PostQuitMessage(0);
        break;
    case IDEXIT:
        EndDialog(hwnd, TRUE);
        PostQuitMessage(0);
        break;
    }
    break;
case WM_CLOSE:
    EndDialog(hwnd, FALSE);
    break;
default:
    return FALSE;
}
return TRUE;
}

The GetLastError() returns 1421, control ID not found, but the ID (1004) definitely corresponds to the static control I'm trying to alter. I've also tried calling the function with the control name (IDC_STATIC_UNSAVED) with no luck. The strange part is that if I move the function call to where dHandle is declared (or get rid of dHandle and just call GetActiveWindow() inside the function there) the text is changed but it flickers because the function is being called every time the message loop iterates.

Is there a simple reason that this shouldn't work that I'm missing? Any help will be appreciated.

Edit: Here is an image of the Resource Symbols: Resource Symbols And here is an image of the Dialog Template: Dialog Template Note that all of the other controls work as expected.

Upvotes: 4

Views: 1465

Answers (2)

Patrick
Patrick

Reputation: 77

I agree with Sid S.
Besides changing the first parameter of SetDlgItemText from dHandle to hwnd, I would also suggest using IDC_STATIC_UNSAVED instead of the hardcoded value 1004. So, the SetDlgItemText() call becomes:
SetDlgItemText(hwnd, IDC_STATIC_UNSAVED, ("There are unsaved changes to \""));

Upvotes: 1

Sid S
Sid S

Reputation: 6125

The dialog window is passed to your handler in the hwnd parameter. There is no need to call GetActiveWindow() - in fact, that will give you the HWND of another window when the dialog is not the active window.

So, replace

HWND dHandle = GetActiveWindow();
SetDlgItemText(dHandle, 1004, ("There are unsaved changes to \""));

with

SetDlgItemText(hwnd, 1004, "There are unsaved changes to \"");

Upvotes: 5

Related Questions