Menu

Console window popup with win 32 project

2009-07-06
2012-09-26
  • George Kotula

    George Kotula - 2009-07-06

    I am just getting into programming, and i just recently switched to Dev because Visual C++ sucks. Anyway, im just creating a basic window on an empty project and when i compile and run it, a console window pops up behind the window i created. it goes when i X out of it, but my window closes as well. If there is any way to fix this, please let me know. Here is my source.

    //.cpp file:

    include <windows.h>

    const char g_szClassName[] = "myWindowClass";

    WNDCLASSEX wc;

    LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    switch(msg)
    {
    case WM_CLOSE:
    DestroyWindow(hwnd);
    break;
    case WM_DESTROY:
    PostQuitMessage(0);
    break;
    default:
    return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
    }

    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

    {
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    wc.cbSize                = sizeof(WNDCLASSEX);
    wc.style                 = 0;
    wc.lpfnWndProc           = WndProc;
    wc.cbClsExtra            = 0;
    wc.cbWndExtra            = 0;
    wc.hInstance             = hInstance;
    wc.hIcon                 = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor               = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground         = (HBRUSH)(COLOR_WINDOW+1);
    wc.lpszMenuName          = NULL;
    wc.lpszClassName         = g_szClassName;
    wc.hIconSm                = LoadIcon(NULL, IDI_APPLICATION);
    
    if(!RegisterClassEx(&amp;wc))
    {
        MessageBox(NULL, &quot;Window Registration Failed!&quot;, &quot;Error&quot;, MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    
    hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, &quot;The Title of my  Window&quot;, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 300, NULL, NULL, hInstance, NULL);
    
    if(hwnd == NULL)
    {
        MessageBox(NULL, &quot;Window Creation Failed!&quot;, &quot;Error&quot;, MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }
    
    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);
    
    while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)
    {
        TranslateMessage(&amp;Msg);
        DispatchMessage(&amp;Msg);
    }
    return Msg.wParam;
    

    }

     
    • George Kotula

      George Kotula - 2009-07-06

      Thanks Clifford. First off, I didn't accurately describe my beef with VC++: i didn't want the new projects to have unicode automatically linked in. Dev C++ does not do this and it makes my life easier. Second, that was my bad with the error log vs. the entire log thing. I took a look at the whole log with a Win 32 GUI project and found the -mwindows link in it. That definitely was my only problem. Thanks again for the support.

      -George

       
    • George Kotula

      George Kotula - 2009-07-06

      I forgot some things like
      version: 4.9.9.2
      OS: Window XP SP3
      There are no errors.

      I guess i didnt read the "Please read this before posting a question"
      oops.

       
    • cpns

      cpns - 2009-07-06

      > i just recently switched to Dev because Visual C++ sucks.

      You must have been using it in some very strange way if you believe that it sucks more than the long unmaintained Dev-C++. It has annoyances, but none more annoying than the flaky debugger that Dev possesses!

      > If there is any way to fix this, please let me know.

      Set your project type as Win32 GUI. You must be using the project facility to do this, and it is recommended in any case. However if you insist on not using the project tool, use the linker -mwindows switch. Had you created your project from the Win32 project template (File->New->New Project, Win32 GUI Application), all necessary options will be set for you, as well as all of the basic Win32 message loop framework code.

      > I guess i didnt read the "Please read this before posting a question"

      It seems that you still have not, because you did not post the log! There may be no errors, but error messages are not the only useful information in the log. It also describes how your project is configured, and how it was built. Specifically in this case it would have shown whether you used the project tool or compiled directly.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.