Win32:隐藏后无法显示窗口?

时间:2016-04-13 06:55:58

标签: winapi hide showwindow

我试图在Callback函数中使用ShowWindow显示一个窗口,该函数在我隐藏后由SetTime设置,但它没有用。 请检查以下代码示例。

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

谢谢。

1 个答案:

答案 0 :(得分:0)

正如@IInspectable建议的那样,回调函数携带的是错误的句柄(已经传递给NULL的句柄SetTimer。)

要更正上述代码,您应该对显示隐藏使用相同的句柄。

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

谢谢。

相关问题