0

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.

Mostafa Alayesh
  • 111
  • 1
  • 9
  • Why did you cast `f`? Remove that cast. You don't check for errors. Why not? – David Heffernan Apr 13 '16 at 07:04
  • Pretty impossible to tell, what *"doesn't work"*. The text says, you cannot show a window, after it was hidden. The code says something else (`ShowWindow(hwnd, SW_SHOW); //won't hide the window`). I don't know, why you opted for the wrong [TimerProc](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644907.aspx) signature either. – IInspectable Apr 13 '16 at 08:02
  • 1
    Why don't you set a breakpoint on your `ShowWindow`-call (inside `f`), and observe the value of `hwnd`? Since the timer is not associated with a window, I'd assume it is `NULL`. – IInspectable Apr 13 '16 at 10:09

1 Answers1

0

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.

Mostafa Alayesh
  • 111
  • 1
  • 9