Search code examples
c++windowswinapi

C++ Window application opening with an additional window. How do I get rid of the second window?


I have recently started C++ programming and when I had finished my first program I compiled and ran the program which worked however when ran it always opens a second window with the title \bin\Debug\C++ 2d Games tutorial. Here is a image of what is happening.

https://i.sstatic.net/0E1F0.png

What have I done wrong for this to happen? I just want my application window to run without the other one in the background. The code that I have written is also provided here.

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

Any help fixing this would be greatly appreciated. As I said before I am new to this and I don't know what I have done wrong. If it helps any this was compiled in code blocks.


Solution

  • Try changing your application to GUI Application in the project properties / build targets. It's probably set to Console.