2011-11-24 62 views
2

我正在通過一本書向我展示如何創建一個Windows應用程序。我編寫了代碼,但是當我編譯並運行它時,它說它已經成功構建,但它沒有顯示應該寫入「Hello World」的窗口。我正在使用Visual Studio 2010與C++,可能是什麼問題?看不到我的第一個窗口應用程序

謝謝

這是代碼;

//Header Files 
#include <windows.h> 
#include <stdlib.h> 
#include <time.h> 

//Application Title 
#define APPTITLE L"Hello World" 

//function prototypes (forward declarations) 
BOOL InitInstance(HINSTANCE, int); 
ATOM MyRegisterClass(HINSTANCE); 
LRESULT CALLBACK WinProc(HWND, UINT, WPARAM, LPARAM); 

//The window event callback function 
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{ 
    PAINTSTRUCT ps; 
    HDC hdc; 
    //char *szHello = "Hello World!"; 
    RECT rt; 
    int x, y, n; 
    COLORREF c; 

    switch(message) 
    { 
     case WM_PAINT: 
      //get the dimensions of the window 
      GetClientRect(hWnd, &rt); 

      //Start drawing on device context 
      hdc = BeginPaint(hWnd, &ps); 

      //Draw some text 
      DrawText(hdc, L"Hello World!", strlen("Hello World!"), &rt, DT_CENTER); 

      //Draw 1000 random pixels 
      for(n=0; n < 3000; n++) 
      { 
       x = rand() % (rt.right - rt.left); 
       y = rand() % (rt.bottom - rt.top); 
       c = RGB(rand()%256, rand()%256, rand()%256); 
       SetPixel(hdc, x, y, c); 
      } 

      //Stop drawing 
      EndPaint(hWnd, &ps); 
      break; 

     case WM_DESTROY: 
      PostQuitMessage(0); 
      break; 
    } 
    return DefWindowProc(hWnd, message, wParam, lParam); 

} 

//helper function to set up the window properties 
ATOM MyRegisterClass(HINSTANCE hInstance) 
{ 
    //create the window class structure 
    WNDCLASSEX wc; 
    wc.cbSize = sizeof(WNDCLASSEX); 

    //FILL THE STRUCT WITH INGO 
    wc.cbSize = CS_HREDRAW | CS_VREDRAW; 
    wc.lpfnWndProc = (WNDPROC)WinProc; 
    wc.cbClsExtra = 0; 
    wc.cbWndExtra = 0; 
    wc.hInstance = hInstance; 
    wc.hIcon = NULL; 
    wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName = APPTITLE; 
    wc.hIconSm = NULL; 

    //set up the window with the class info 
    return RegisterClassEx(&wc); 

} 

BOOL InitInstance(HINSTANCE hInstance, int nCmdShow) 
{ 
    HWND hWnd; 

    //create a new window 
    hWnd = CreateWindow(
     APPTITLE,    //Window class 
     APPTITLE,    //title bar 
     WS_OVERLAPPEDWINDOW, //window Style 
     CW_USEDEFAULT,   //x position of window 
     CW_USEDEFAULT,   //y postion of window 
     500,     //width of the window 
     400,     //height of the window 
     NULL,     //parent window 
     NULL,     //menu 
     hInstance,    //application instance 
     NULL);     //window parameters 

    //was there an error creating the window? 
    if(!hWnd) 
     return FALSE; 

    //Display the window 
    ShowWindow(hWnd, nCmdShow); 
    UpdateWindow(hWnd); 

    return TRUE; 

} 

//Entry point for a Windows program 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{ 
    //declare varuables 
    MSG msg; 

    //register the class 
    MyRegisterClass(hInstance); 

    //Initialize application 
    if(!InitInstance(hInstance, nCmdShow)) 
     return FALSE; 

    //set random number seed 
    srand(time(NULL)); 

    //Main message loop 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 


    return msg.wParam; 

} 

回答

3

此行是錯誤的:

wc.cbSize = CS_HREDRAW | CS_VREDRAW; 

你的意思是

wc.style = CS_HREDRAW | CS_VREDRAW; 

事實上,這樣你讓它在很清楚我會改變窗口類的初始化代碼整個結構被初始化的代碼。

WNDCLASSEX wc = { 0 };//initialise struct to 0 
wc.cbSize = sizeof(WNDCLASSEX); 
//FILL THE STRUCT WITH INGO 
wc.style = CS_HREDRAW | CS_VREDRAW; 
wc.lpfnWndProc = (WNDPROC)WinProc; 
wc.hInstance = hInstance; 
wc.hCursor = LoadCursor(NULL, IDC_ARROW); 
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); 
wc.lpszClassName = APPTITLE; 

你失蹤了一些錯誤檢查:

if (!MyRegisterClass(hInstance)) 
    return FALSE; 

下調試通過步進將讓你看到在這個過程中事情會出錯。

+0

非常感謝。我不知道我是如何錯過的,但爲什麼如果我給wc.cbSize提供了錯誤的數據,它沒有給出錯誤信息? – Perex19

+0

好的!我注意到窗口類沒有註冊(CreateWindow將最後一個錯誤設置爲1407),但幾分鐘盯着MyRegisterClass沒有發現錯誤。 –

+0

它確實出現錯誤。對'RegisterClassEx'的調用,你可以通過調用'GetLastError'找到原因。你只是沒有檢查。看到我最新的更新。 –

相關問題