2017-01-25 2 views
0

최근에 C++에 대한 소개 수업을 마친 후 Visual C++로 이동하여 작은 응용 프로그램을 만들려고했습니다.Visual C++ 창 만들기가 실패했습니다.

지금까지 내가 원했던 것은 적어도 창을 만들고 표시하는 것입니다. 더 이상 가지 않았습니다. 많은 코드를 사용하기 위해 많은 Microsoft 자습서를 사용했습니다.

내가하는 일을 이해하는 데 도움이되는 의견이 많습니다. 마이크로 소프트 비주얼 스튜디오 2015

#include <tchar.h> //A Win32 character string that can be used to describe ANSI, DBCS, or Unicode strings 
        //Enclose the strings in _T() if an incompatibility error occures or (LPCWSTR)L"" 
#include <windows.h> 

// Global variable 
HINSTANCE hinst; 

//Function prototype for the window procedure so the inside of the program knows wazup 
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); 
BOOL InitApplication(HINSTANCE); 
BOOL InitInstance(HINSTANCE, int); 

//Instead of your usual console program, you get WinMain() instead of int main(int argc,char *argv[]) 
INT WINAPI wWinMain(HINSTANCE hInst, //Instance of the program being executed 
    HINSTANCE hPrevInst,    //Previous instance, not used much anymore 
    LPWSTR lpCmdLine,    //Holds command line arguments 
    INT nShowCmd)     //Tells how the program should start(minumised, maximised...) 
{ 
    /*Steps to create a window: 
     - Register a window class 
     - Create the window 
     - Show the window 
     - Setup a callback procedure 
    */ 

    //We can tell if the window class has failed if RegisterClassEx() returns 0. 
    //Fortunatley, there is another handy function, namely GetLastError(). 
    //GetLastError() will return an error code, which can then be used to track down the cause of the error. 
    //https://msdn.microsoft.com/en-us/library/ms681381.aspx 
    if (!InitApplication(hInst)) 
    { 
     int nResult = GetLastError(); 
     MessageBox(NULL, 
      _T("Window class creation failed"), 
      _T("Window Class Failed"), 
      MB_ICONERROR); 
     return 1; 
    } 
    //Window creation & display: 
    if (!InitInstance(hInst, nShowCmd)){ 
     int nResult = GetLastError(); 
     MessageBox(NULL, 
      _T("Window creation failed"), 
      _T("Window Creation Failed"), 
      MB_ICONERROR); 
     return 1; 
    } 

    //The windows 'callback procedure' is necessary to continue 
    MSG msg; //Message handler 
    ZeroMemory(&msg, sizeof(MSG)); 
    //Main loop for the program 
    while (GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg);//translates virtual key messages to character messages 
     DispatchMessage(&msg); //Sends any messages to the callvack procedure, so it can be handled 
    } 
    //Still returns an int! 
    return 0; 
} 

BOOL InitApplication(HINSTANCE hinstance) 
{ 
    //The window class defines the overall 'look and feel' of the window that we want to create. 
    //First we create an instance of a window class 
    WNDCLASSEX wClass; 
    //For more about WNDCLASSEX: https://msdn.microsoft.com/en-us/library/ms633577(VS.85).aspx 

    //Clearing all the parameters of the WNDCLASSEX instance wClass to zero 
    ZeroMemory(&wClass, sizeof(WNDCLASSEX)); 
    //Now we are setting up all sotrts of parameters for our instance: 
    wClass.cbClsExtra = NULL;      // cbClsExtra   Additional parameters 
    wClass.cbSize = sizeof(WNDCLASSEX);    // cbSize    Specifies the size of the window class 
    wClass.cbWndExtra = NULL;      // cbWndExtra   Additional parameters 
    wClass.hbrBackground = (HBRUSH)COLOR_WINDOW; // hbrBackground  Sets background color for the window 
    wClass.hCursor = LoadCursor(NULL, IDC_ARROW); // hCursor    The cursor that will appear in the window 
    wClass.hIcon = NULL;       // hIcon    Icon for the window 
    wClass.hIconSm = NULL;       // hIconSm    Small icon for the window 
    wClass.hInstance = hinstance;     // hInstance   Handle to the application instance 
    wClass.lpfnWndProc = (WNDPROC)WinProc;   // lpfnWndProc   The callback procedure (more on that later) 
    wClass.lpszClassName = _T("Window Class");  // lpszClassName  The unique name of the window class 
    wClass.lpszMenuName = NULL;      // lpszMenuName  Used for menus 
    wClass.style = CS_HREDRAW | CS_VREDRAW;   // style    The look and feel of the window 

    // Register the window class. 
    return RegisterClassEx(&wClass); 
} 

BOOL InitInstance(HINSTANCE hinstance, int nCmdShow) 
{ 
    // Save the application-instance handle. 
    hinst = hinstance; 
    // Create the main window. 
    //CreateWindowEx returns a HWND 
    HWND hwnd = CreateWindowEx( // A "handle" is a generic identifier + like "new" 
     0,      // DWORD, extended window style of the window being created https://msdn.microsoft.com/en-us/library/windows/desktop/ff700543(v=vs.85).aspx 
     _T("MainsWClass"),  // LPCTSTR, A null-terminated string or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. 
     _T("Sample"),   // LPCTSTR, The window name. 
     WS_OVERLAPPEDWINDOW,  // DWORD, Style of the window created 

     CW_USEDEFAULT,   // int, X - default horizontal position 
     CW_USEDEFAULT,   // int, Y - default vertical position 
     CW_USEDEFAULT,   // int, nWidth - default width 
     CW_USEDEFAULT,   // int, nHeight - default height 

     (HWND)NULL,    // HWND, hWndParent - handle to the parrent or owner window of the window being created (opptional for pop-up) 
     (HMENU)NULL,    // HMENU, hMenu 
     hinstance,    // HINSTANCE, hInstance - A handle to the instance of the module to be associated with the window. 
     (LPVOID)NULL);   // LPVOID, Pointer to a value to be passed to the window through the CREATESTRUCT structure 

    //If the creation fails, returns FALSE 
    if (!hwnd) 
     return FALSE; 
    else{ 
     // Show the window and send a WM_PAINT message to the window procedure. 
     ShowWindow(hwnd, nCmdShow); 
     UpdateWindow(hwnd); 
     return TRUE; 
    } 
} 

//callback procedure 
LRESULT CALLBACK WinProc(
    HWND hWnd,  //Handle of the window that we want to monitor 
    UINT msg,  //Message we are receiving 
    WPARAM wParam, //Additionnal info for the received message 
    LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_DESTROY: //Message sent to the application if the program was closed using the X on the top of the window 
    { 
     PostQuitMessage(0); 
     return 0; 
    } 
    break; 
    } 

    return DefWindowProc(hWnd, msg, wParam, lParam); 
} 
+1

* * 프로그램이 실패한 곳은 어디입니까? GetLastError에 의해 반환 된 [오류 코드] (https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85) .aspx)는 무엇입니까? –

+1

You * really * 이렇게 UI 프로그래밍을 시작하고 싶지는 않습니다. 더 나은 경로를 보려면 https://msdn.microsoft.com/en-us/windows/uwp/get-started/create-a-basic-windows-10-app-in-cpp를 참조하십시오. –

+0

@Someprogrammerdude에 명시된 것 외에도 디버거를 사용하여 프로그램이 실패하는 위치를 파악할 수 있습니다. –

답변

2

당신은 "Window Class"라는 이름의 창 클래스를 등록하지만, 클래스 "MainsWClass"의 창을 만들려고하고 있습니다를 사용하여 난`. 그 계급은 알려지지 않았습니다. 이전에 등록한 창 클래스의 창을 만들어야합니다 (직접 또는 "BUTTON"과 같은 미리 정의 된 창 클래스 중 하나).

+0

정말 고마워요. 그것이 거기에 가야하는 것이 무엇인지 알지 못했습니다! – Artur

+0

@Artur : 그것은 당신이 언급 한 내용입니다.'// LPCTSTR, 널 종료 문자열이나 RegisterClass 또는 RegisterClassEx 함수에 대한 이전 호출에 의해 생성 된 클래스 아톰 .' – IInspectable

관련 문제