ShowWindow无效的窗口句柄

时间:2013-03-01 23:20:00

标签: c++ windows winapi getlasterror showwindow

我最近一直在尝试使用C ++中的Windows API创建一个窗口类。但是,每当我尝试调用ShowWindow时,该函数都会将最后一个错误设置为1400(ERROR_INVALID_WINDOW_HANDLE)。尝试了一段时间后,我偶然发现了以下示例: http://blogs.msdn.com/b/oldnewthing/archive/2005/04/22/410773.aspx#comments

即使创建一个新项目(我使用MSVC Express 2008)并完全复制代码(我讨厌做),我发现,当代码成功创建一个窗口时,ShowWindow函数仍然报告错误1400.这是以上链接中的代码摘录:

int PASCAL
WinMain(HINSTANCE hinst, HINSTANCE, LPSTR, int nShowCmd)
{
 g_hinst = hinst;

 if (SUCCEEDED(CoInitialize(NULL))) {
  InitCommonControls();

  RootWindow *prw = RootWindow::Create();
  if (prw) {
   ShowWindow(prw->GetHWND(), nShowCmd);
   int error = GetLastError(); //Line added by me, error gets set to 1400.
   MSG msg;
       while (GetMessage(&msg, NULL, 0, 0)) {
    TranslateMessage(&msg);
    DispatchMessage(&msg);
   }
  }
  CoUninitialize();
 }
 return 0;
}

(完整的代码可以在上面的链接中找到)

如果有人对如何将窗口句柄作为类的成员变量而没有在ShowWindow上收到错误1400有任何想法,我将非常感谢一些帮助。

2 个答案:

答案 0 :(得分:6)

   ShowWindow(prw->GetHWND(), nShowCmd);
   int error = GetLastError();

这不是正确的代码。调用GetLastError()的唯一时间是当winapi函数失败时。如果你在没有失败时使用GetLastError(),那么你将得到一个完全随机的数字。 ShowWindow()有点特别之处在于它根本不会产生错误代码,因此使用GetLastError()永远不会正确。

通用模式大致是:

if (!SomeWinapiFunction(...)) {
    int error = GetLastError();
    CrashAndBurn(error);
}

但请检查MSDN文档以查看返回值表示错误以及GetLastError()是否合适。例如,它通常不在GDI函数上。请务必在代码的其他部分更正此问题。当您使用原始api时,正确处理错误是非常。特别注意你的RootWindow :: Create()方法没有很好的方法来指示创建窗口失败。这需要修复。例外当然是一种非常好的方法。

答案 1 :(得分:0)

我有同样的问题。解决方案是将 DefWindowProc()默认移到 WndProc()的末尾。

之前:

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    static HBITMAP hBitMap;
    static int cxSizeBitMap;
    static int cySizeBitMap;
    static int cxClient;
    static int cyClient;
    HDC hdc;
    BITMAP bitMap;
    PAINTSTRUCT ps;
    HDC hMem;
    HINSTANCE      hInstance ;
    switch( message ) 
    {
        case WM_CREATE:
            hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
            hBitMap = LoadBitmap( hInstance, MAKEINTRESOURCE( IDB_BRICK ) );
            GetObject( hBitMap, sizeof(BITMAP), &bitMap );
            cxSizeBitMap = bitMap.bmWidth;
            cySizeBitMap = bitMap.bmHeight;
            break;
        case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
            break;
        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            hMem = CreateCompatibleDC( hdc );
            SelectObject( hMem,  hBitMap );
            for (int y = 0 ; y < cyClient ; y += cySizeBitMap)
            for (int x = 0 ; x < cxClient ; x += cxSizeBitMap)
            {
               BitBlt (hdc, x, y, cxSizeBitMap, cySizeBitMap, hMem, 0, 0, SRCCOPY) ;
            }
            DeleteDC( hMem );
            EndPaint( hWnd, &ps );
            break;
        case WM_DESTROY:
            DeleteObject( hBitMap );
            PostQuitMessage( 0 );
            break;
        default:
            // In this cast ShowWindow() will return 1400.
            DefWindowProc(hWnd, message, wParam, lParam);
    }

    return 0;
}

之后:

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
{
    static HBITMAP hBitMap;
    static int cxSizeBitMap;
    static int cySizeBitMap;
    static int cxClient;
    static int cyClient;
    HDC hdc;
    BITMAP bitMap;
    PAINTSTRUCT ps;
    HDC hMem;
    HINSTANCE      hInstance ;
    switch( message ) 
    {
        case WM_CREATE:
            hInstance = ((LPCREATESTRUCT) lParam)->hInstance ;
            hBitMap = LoadBitmap( hInstance, MAKEINTRESOURCE( IDB_BRICK ) );
            GetObject( hBitMap, sizeof(BITMAP), &bitMap );
            cxSizeBitMap = bitMap.bmWidth;
            cySizeBitMap = bitMap.bmHeight;
            break;
        case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
            break;

        case WM_PAINT:
            hdc = BeginPaint( hWnd, &ps );
            hMem = CreateCompatibleDC( hdc );

            // Было SelectObject( hdc,  hMem );
            SelectObject( hMem,  hBitMap );
            // Было BitBlt( hdc, 0, 0, cxSize, cySize, hMem, 0, 0, DIB_RGB_COLORS);
            for (int y = 0 ; y < cyClient ; y += cySizeBitMap)
            for (int x = 0 ; x < cxClient ; x += cxSizeBitMap)
            {
               BitBlt (hdc, x, y, cxSizeBitMap, cySizeBitMap, hMem, 0, 0, SRCCOPY) ;
            }


            DeleteDC( hMem );
            EndPaint( hWnd, &ps );
            break;
        case WM_DESTROY:
            DeleteObject( hBitMap );
            PostQuitMessage( 0 );
            break;
    }

    // In this case ShowWindow() will show the window.
    return DefWindowProc(hWnd, message, wParam, lParam);;
}