DaveTheMinion
DaveTheMinion

Reputation: 664

CreateWindow(): Type name is not allowed

I am writing a C++ program in which I would like to create a window using the CreateWindow() function, but I cannot get it to work. I am unable to to compile the program, and the only information Visual Studio gives me in the Error List is "type name is not allowed." How can I resolve this? I haven't been able to determine how to fix it on my own. Here is the code for the program:

#include "stdafx.h"
#include <Windows.h>

int main()
{
    int screenWidth = GetSystemMetrics(SM_CXSCREEN);
    int screenHeight = GetSystemMetrics(SM_CYSCREEN);
    HWND window = CreateWindow("Melter", NULL, WS_POPUP, 0, 0, screenWidth, screenHeight, HWND_DESKTOP, NULL, HINSTANCE, NULL);
    return 0;
}

Upvotes: 0

Views: 1112

Answers (1)

Rabbid76
Rabbid76

Reputation: 210928

To create a window from a console application you have saveral things to do. First of all you have to register your own window class by RegisterClass with style paramters, a module handle and most important a window procedure. The module handle you can get by GetModuleHandle(0), what returns a handle to the file used to create the calling process. The window procedure you have to define yourslef. This is a function that processes messages sent to a window. With this window class and the module handle you can create your window with CreateWindow. After your window is created you have to show it with ShowWindow. Finally you need a message loop for your window:

#include <Windows.h>

// Window procedure which processes messages sent to the window
LRESULT CALLBACK WindowProcedure( HWND window, unsigned int msg, WPARAM wp, LPARAM lp )
{
    switch(msg)
    {
        case WM_DESTROY: PostQuitMessage(0); return 0;
        default: return DefWindowProc( window, msg, wp, lp );
    }
}

int main()
{
    // Get module handle
    HMODULE hModule = GetModuleHandle( 0 );
    if (!hModule)
        return 0;

    // Register window class
    const char* const myWindow = "MyWindow" ;
    //const wchar_t* const myWindow = L"MyWindow"; // unicode
    WNDCLASS myWndClass = { 
        CS_DBLCLKS, WindowProcedure, 0, 0, hModule,
        LoadIcon(0,IDI_APPLICATION), LoadCursor(0,IDC_ARROW),
        CreateSolidBrush(COLOR_WINDOW+1), 0, myWindow };
    if ( !RegisterClass( &myWndClass ) )
        return 0;

    // Create window
    int screenWidth = GetSystemMetrics(SM_CXSCREEN)/2;
    int screenHeight = GetSystemMetrics(SM_CYSCREEN)/2;
    HWND window = CreateWindow( myWindow, NULL, WS_OVERLAPPEDWINDOW, 0, 0, screenWidth, screenHeight, HWND_DESKTOP, NULL, hModule, NULL);
    if( !window )
        return 0;

    // Show window
    ShowWindow( window, SW_SHOWDEFAULT );

    // Message loop
    MSG msg ;
    while( GetMessage( &msg, 0, 0, 0 ) )
        DispatchMessage(&msg);

    return 0;
}

Upvotes: 1

Related Questions