c++ open a Window

211 views Asked by At

İ am new at c++ programming.When i compile this code it dont giving any error,but while its still running ,I cant see any Window.I copied this code from a book to my visual c++.What i am doing wrong. thanks for everything

// How to print text in Win32 C/C++
// Auther: -LeetGamer-

#include <Windows.h>
#include <WindowsX.h>

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX ps;
    HWND hWnd;

    ZeroMemory(&ps,sizeof(WNDCLASSEX));

    ps.cbSize=sizeof(WNDCLASSEX);
    ps.style=CS_HREDRAW | CS_VREDRAW ;
    ps.lpfnWndProc=(WNDPROC)WindowProc;
    ps.hInstance=hInstance;
    ps.hCursor=LoadCursor(NULL,IDC_ARROW);
    ps.hbrBackground=(HBRUSH)COLOR_WINDOW;
    ps.lpszClassName="asdasdasd";

    RegisterClassEx(&ps);

    hWnd=CreateWindowEx(NULL,"ASDASDDA","dsfsdfsdf",WS_OVERLAPPEDWINDOW,300,300,500,400,NULL,NULL,hInstance,NULL);

    ShowWindow(hWnd,nCmdShow);

    MSG mesaj;

    while(GetMessage(&mesaj,NULL,0,0))
    {
        TranslateMessage(&mesaj);
        DispatchMessage(&mesaj);
    }


    return mesaj.wParam;
}

LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            }
            break;

    }

    return DefWindowProc(hWnd,message,wParam,lParam);
}
1

There are 1 answers

0
manuell On

The class name must be the same in RegisterClassEx and CreateWindowEx

use:

const char * pszClassName = "MyClassName";

[...]

ps.lpszClassName = pszClassName;

[...]

hWnd = CreateWindowEx( NULL, pszClassName, [...]

Also, take the habit of checking results from APIs. Here, CreateWindowEx returned NULL.