Win32: Can't Show window after hide it?

1.4k views Asked by At

I was trying to show a window using ShowWindow in a Callback function that called is set by a SetTime after I have hided it, but it didn't worked. Please check the following code example.

#define _WIN32_WINNT 0x0500
#include<windows.h>
void CALLBACK f(HWND hwnd, UINT uMsg, UINT timerId, DWORD dwTime)
{
    MessageBoxA(NULL,"Test","test2",MB_OK);
    ShowWindow( hwnd, SW_SHOW );  //This will not show the window :(
    MessageBoxA(NULL,"Is it shown?","test2",MB_OK);
}
int main()
{
    MSG msg;
    ShowWindow( GetConsoleWindow(), SW_HIDE );
    SetTimer(NULL, 0, 1000*3, &f);
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

Thank you.

1

There are 1 answers

0
Mostafa Alayesh On

As @IInspectable suggested, it is the wrong handle that the call back function carries (which is the handle NULL which have been passed to SetTimer).

To correct the code above, you should use the same handle for both show and hide.

#define _WIN32_WINNT 0x0500
#include<windows.h>
HWND hwnd;
void CALLBACK f(HWND __hwnd__, UINT uMsg, UINT timerId, DWORD dwTime)
{
    MessageBoxA(NULL,"Test","test2",MB_OK);
    ShowWindow( hwnd, SW_SHOW );  //This will not show the window :(
    MessageBoxA(NULL,"Is it shown?","test2",MB_OK);
}
int main()
{
    MSG msg;
    hwnd=GetConsoleWindow();

    ShowWindow(hwnd , SW_HIDE );

    SetTimer(NULL, 0, 1000*3, &f);
    while(GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return 0;
}

Thank you.