2011-11-24 2 views
2

Windows 응용 프로그램을 만드는 방법을 보여주는 책을 통해 작업하고있었습니다. 코드를 작성했지만 컴파일하고 실행할 때 successfuly 빌드되었다고하지만 "Hello World"를 작성해야하는 창은 표시되지 않습니다. C++에서 Visual Studio 2010을 사용하고 있는데 문제가 있습니까?첫 번째 창 응용 프로그램을 볼 수 없습니다.

감사합니다.

여기에 코드가 나와 있습니다.

//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; 

전체 struct가 초기화되는 코드.

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' 호출을 통해 이유를 알 수 있습니다. 너는 단지 그것을 확인하지 않고 있었다. 최신 업데이트를 확인하십시오. –

관련 문제