2016-07-01 8 views
0

はコードです:ここでは私のWin32(C++、visual studio 2013)ウィンドウが表示されないのはなぜですか?ここ

#include <windows.h> 
#include <windowsx.h> 


// the WindowProc callback function prototype 
LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam); 

// win32 entry point 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){ 
    ///* 
    // window handler 
    HWND hwnd; 

    // struct for window information 
    WNDCLASSEX wc; 

    // clear out the window class for use 
    ZeroMemory(&wc, sizeof(WNDCLASSEX)); 

    // setting wc struct with window values/properties 
    wc.cbSize = sizeof(WNDCLASSEX); 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.hInstance = hInstance; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.lpszClassName = "TestWindow"; 
    wc.lpfnWndProc = WindowProc; 

    // register window before creation/use 
    RegisterClassEx(&wc); 

    // creating the window class 
    hwnd = CreateWindowEx(NULL, "TestWindow", "First Win32 Program", WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL); 

    // show the window 
    ShowWindow(hwnd, nCmdShow);//*/ 

} 

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam){ 
    return 0; 
} 

は、コンパイラ/デバッグ/実行時のコンソール出力です:

'wintest.exe' (Win32): Loaded 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\user32.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\gdi32.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\lpk.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\usp10.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcr120d.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\imm32.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file. 
'wintest.exe' (Win32): Loaded 'C:\Windows\SysWOW64\uxtheme.dll'. Cannot find or open the PDB file. 
The program '[3484] wintest.exe' has exited with code 0 (0x0). 

それは非常に基本的な簡単なプログラムだし、それがない、すべてがのWin32をオープンポップすると想定されます左上隅から始まるサイズ1000x1000のウィンドウ。

+1

...ウィンドウを表示しようとすると、すぐに終了します。 [ループする方法はこちら](https://msdn.microsoft.com/en-us/library/windows/desktop/ff381409(v = vs.85).aspx)を参照してください。 –

+0

文字セットのデフォルト設定をUnicodeからMBCSに変更しないでください。 Unicodeを使用していない場合は、限られた機能を持つアプリケーションを実装するためのリソースを無駄に消費します。 – IInspectable

+1

**ウィンドウを作成する**サンプルプログラムは、メッセージループを含みます。どのようにこれを見逃すことができましたか? –

答えて

5

私が見ることができる最初の間違いは、あなたのウィンドウの手順です。明示的に処理しないメッセージは、DefWindowProcに渡されます。

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) 
{ 
    return DefWindowProc(hwnd, message, wparam, lparam); 
} 

これは、ウィンドウプロシージャに必要な最小限です。実際には、少なくともWM_CLOSEWM_DESTROYを処理し、それ以上の機能を追加したいと考えています。

作成中にウィンドウにWM_NCCREATEメッセージを送信すると、壊れたウィンドウプロシージャによって、CreateWindowExが失敗します。

もう1つの目立った問題は、エラーチェックをまったく実行しないことです。作成したすべてのAPI呼び出しの戻り値を無視することで、プログラムがどこで失敗しているかを診断することはできません。

最後に、メッセージループを含めません。したがって、たとえウィンドウが表示されても、プロセスは直ちに終了します。

このプログラムはウィンドウを表示します。明らかに、もっと多くの作業が必要ですが、それはスタートです。

#include <windows.h> 

LRESULT CALLBACK WindowProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam) 
{ 
    switch(message) 
    { 
     case WM_CLOSE: 
      DestroyWindow(hwnd); 
      return 0; 
     case WM_DESTROY: 
      PostQuitMessage(0); 
      return 0; 
    } 
    return DefWindowProc(hwnd, message, wparam, lparam); 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, 
    int nCmdShow) 
{ 
    WNDCLASSEX wc = { sizeof(WNDCLASSEX) }; 
    wc.style = CS_HREDRAW | CS_VREDRAW; 
    wc.hInstance = hInstance; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.lpszClassName = "TestWindow"; 
    wc.lpfnWndProc = WindowProc; 

    if (!RegisterClassEx(&wc)) 
     return 1; 

    HWND hwnd = CreateWindowEx(0, "TestWindow", "First Win32 Program", 
     WS_OVERLAPPEDWINDOW, 100, 100, 1000, 1000, NULL, NULL, hInstance, NULL); 
    if (!hwnd) 
     return 1; 

    ShowWindow(hwnd, nCmdShow); 

    MSG msg; 
    while (GetMessage(&msg, 0, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 

    return 0; 
} 
+1

誤ったウィンドウプロシージャは、[WM_NCCREATE]に「0」を返します(https://msdn.microsoft.com/en-us/library/)。 windows/desktop/ms632635.aspx)、 'CreateWindowEx'を呼び出して' NULL'を返します。これは、あなたが示唆するように、エラーをチェックするときに容易に診断することができます。それは決して地面から降りないので、窓は現れません。 – IInspectable

関連する問題