带有附加窗口的C ++ Window应用程序打开。如何摆脱第二个窗口?

时间:2014-03-22 21:38:22

标签: c++ windows winapi compiler-construction

我最近开始使用C ++编程,当我完成第一个程序时,我编译并运行了该程序,但是当它运行时它总是打开第二个窗口,标题为\ bin \ Debug \ C ++ 2d Games教程。这是正在发生的事情的图像。

http://i.imgur.com/e9p9hT0.png

我为此做错了什么?我只是希望我的应用程序窗口在后台运行时没有其他窗口。我在这里提供了我编写的代码。

    // winmain.cpp
    #define DEFINE_LEAN_AND_MEAN
    #include <iostream>
    #include <windows.h>
    #include "StdAfx.h
    // Function prototypes
    int WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int);
    bool CreateMainWindow(HINSTANCE, int);
    LRESULT WINAPI WinProc(HWND, UINT, WPARAM, LPARAM);
    // Global variable
    HINSTANCE hinst;
    // Constants
    const char CLASS_NAME[] = "WinMain";
    const char APP_TITLE[] = "Hello World";  // title bar text
    const int WINDOW_WIDTH = 400;
    const int WINDOW_HEIGHT = 400;
    //=================
    // Starting point for a windows application
    // parameters are
    //       hInstance. Handle to the current instance of the application
    // hPrevInstance. Always NULL, obsolete parameter
    // lpCmdLine. Pointer to null-terminated string of command line arguements
    // nCmdShow. Specifies how the window is to be shown
    //=======================
    int WINAPI WinMain(HINSTANCE hInstance,
                                            HINSTANCE hPrevInstance,
                                            LPSTR        lpCmdLine,
                                            int                   nCmdShow)
    {
        MSG msg;
        // Create the window
        if(!CreateMainWindow(hInstance, nCmdShow) )
                    return false;
             // Main message loop
             int done = 0;
             while (!done)
                        {
                                 //PeekMessage is non blocking test for Windows messages
                         if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE) )
                            {

                                    // Look for quit message
                                    if (msg.message == WM_QUIT)
                                               done = 1;
                                    // Decode and pass messages on to WinProc
                                    TranslateMessage(&msg);
                                    DispatchMessage(&msg);

                                                            }

        }
        return msg.wParam;
    }
    //==================
    // Window event callback function
    //===================
    LRESULT WINAPI WinProc ( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
    {
          switch( msg )
          {
                  case WM_DESTROY:
                  // Tell windows to kill the program
                  PostQuitMessage(0);
                  return 0;

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

    }
    //==========
    // Create the window
    // returns: false on error
    //=============
    bool CreateMainWindow(HINSTANCE hInstance, int nCmdShow)
    {
             WNDCLASSEX wcx;
             HWND hwnd;

           // Fill in the window clasa structure with parameters
           // that describes the main window
   wcx.cbSize = sizeof(wcx);          // size of structure
   wcx.style = CS_HREDRAW | CS_VREDRAW;  // Redraw if size changes
   wcx.lpfnWndProc =WinProc;                // Points to the window Procedure
   wcx.cbClsExtra = 0;                               // No extra class memory
   wcx.cbWndExtra = 0;      // No extra window memory
   wcx.hInstance = hInstance;   // Handle to instance
   wcx.hIcon = NULL;
   wcx.hCursor =LoadCursor(NULL, IDC_ARROW);    // Predefined arrow
   // Background brush
   wcx.hbrBackground = (HBRUSH)GetStockObject (BLACK_BRUSH);
   wcx.lpszMenuName =NULL; // Name of the menu resource
   wcx.lpszClassName = CLASS_NAME;   // Name of the window class
   wcx.hIconSm = NULL;                         // Small class icon


    // Register the window class
    // RegisterClassEx return 0 on error
    if (RegisterClassEx(&wcx) == 0)                // If error
           return false;
    // Create Window
    hwnd = CreateWindow(
       CLASS_NAME,             // Name of the window class
       APP_TITLE,                   // Title bar text
       WS_OVERLAPPEDWINDOW,          // Window style
       CW_USEDEFAULT,            // Default horizontal position of the window
       CW_USEDEFAULT,         //  Default horizontal positin of the window
       WINDOW_WIDTH,     // Width of window
       WINDOW_HEIGHT,      // Height of the window
       (HWND) NULL,          // No parent menu
       (HMENU) NULL,     // No menu
       hInstance,      // Handle to application instance
       (LPVOID) NULL);      // No window parameters
       // If there was an error creating the window
       if (!hwnd)
                    return false;
        // Show the window
        ShowWindow(hwnd, nCmdShow);
        // Send a WM_PAINT message to the window procedure
        UpdateWindow(hwnd);
        return true;
        }

任何帮助解决这个问题都将非常感激。正如我之前所说,我是新手,我不知道自己做错了什么。如果它有助于任何这是在代码块中编译。

亲切的问候

2 个答案:

答案 0 :(得分:1)

另一个窗口是控制台窗口,您正在将其编译为控制台应用程序。因此,您需要将应用程序编译为GUI应用程序。

答案 1 :(得分:1)

尝试将您的申请更改为GUI Application中的project properties / build targets。它可能设置为Console