What's the simplest way to create a window in CPP from scratch? (Console delete?)

Started by
26 comments, last by TDragon 18 years, 9 months ago
You do not use main() when you are programming for windows. You use WinMain, which looks like this:

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPCSTR lpcmdline, int nshowcmd)


You also need to include Windows.h, and when you're posting code, using the , remember to omit the spaces in the tags.

Oh, and to draw text inside a window, you use the TextOut() function. Look that up in MSDN.
Advertisement
Ok, I'm having errors after I found out that I have to compile BOTH cpp files. Here they are:

In file included from C:/CPP/Main.cpp:3:
C:/CPP/Functions.cpp: In function `int CreateWND(std::basic_string<char,
std::char_traits<char>, std::allocator<char> >, int, int)':
C:/CPP/Functions.cpp:8: error: `LRESULT' undeclared (first use this function)
C:/CPP/Functions.cpp:8: error: (Each undeclared identifier is reported only
once for each function it appears in.)
C:/CPP/Functions.cpp:8: error: syntax error before `WindowProcedure'
C:/CPP/Functions.cpp:13: error: syntax error before `(' token
C:/CPP/Functions.cpp:20: error: use of class template `template<class _CharT>
class std::messages' as expression
C:/CPP/Functions.cpp:21: error: `WNDCLASSEX' undeclared (first use this
function)
C:/CPP/Functions.cpp:24: error: `wincl' undeclared (first use this function)
C:/CPP/Functions.cpp:24: error: `hThisInstance' undeclared (first use this
function)
C:/CPP/Functions.cpp:26: error: `WindowProcedure' undeclared (first use this
function)
C:/CPP/Functions.cpp:27: error: `CS_DBLCLKS' undeclared (first use this
function)
C:/CPP/Functions.cpp:31: error: `IDI_APPLICATION' undeclared (first use this
function)
C:/CPP/Functions.cpp:31: error: `LoadIcon' undeclared (first use this function)

C:/CPP/Functions.cpp:33: error: `IDC_ARROW' undeclared (first use this
function)
C:/CPP/Functions.cpp:33: error: `LoadCursor' undeclared (first use this
function)
C:/CPP/Functions.cpp:38: error: `HBRUSH' undeclared (first use this function)
C:/CPP/Functions.cpp:38: error: syntax error before `;' token
C:/CPP/Functions.cpp:41: error: `RegisterClassEx' undeclared (first use this
function)
C:/CPP/Functions.cpp:45: error: `hwnd' undeclared (first use this function)
C:/CPP/Functions.cpp:49: error: `WS_OVERLAPPEDWINDOW' undeclared (first use
this function)
C:/CPP/Functions.cpp:50: error: `CW_USEDEFAULT' undeclared (first use this
function)
C:/CPP/Functions.cpp:54: error: `HWND_DESKTOP' undeclared (first use this
function)
C:/CPP/Functions.cpp:58: error: `CreateWindowEx' undeclared (first use this
function)
C:/CPP/Functions.cpp:61: error: `nFunsterStil' undeclared (first use this
function)
C:/CPP/Functions.cpp:61: error: `ShowWindow' undeclared (first use this
function)
C:/CPP/Functions.cpp:64: error: use of class template `template<class _CharT>
class std::messages' as expression
C:/CPP/Functions.cpp:64: error: `GetMessage' undeclared (first use this
function)
C:/CPP/Functions.cpp:67: error: use of class template `template<class _CharT>
class std::messages' as expression
C:/CPP/Functions.cpp:67: error: `TranslateMessage' undeclared (first use this
function)
C:/CPP/Functions.cpp:69: error: use of class template `template<class _CharT>
class std::messages' as expression
C:/CPP/Functions.cpp:69: error: `DispatchMessage' undeclared (first use this
function)
C:/CPP/Functions.cpp:73: error: use of class template `template<class _CharT>
class std::messages' as expression
C:/CPP/Functions.cpp: At global scope:
C:/CPP/Functions.cpp:79: error: syntax error before `(' token
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Uhhh, you can't declare functions inside of functions. [wink] If you're using separate source files, you will need to make a header file for the one that doesn't contain WinMain().

And post the source code you're using to get the errors.
Ok, I'M LOST!

EDIT: The source code is the Functions.cpp which it's source code is above.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
I reiterate: you cannot declare a function inside a function! WinMain() replaces main(), it doesn't go in a CreatWND() function like you seem to think. Replace int main() with the declaration to Winmain(), first of all, then put windowproc outside of CreateWND().

Also, cin.get() will not work if you're using a window.
Where are you getting windowproc?
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
Quote:Original post by orcfan32
Where are you getting windowproc?


You've named it WindowProcedure().
I still don't know how to move it around; any way I do it, I get compile errors.
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit
#include <windows.h>const char g_szClassName[] = "myWindowClass";// Step 4: the Window ProcedureLRESULT 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;    //Step 1: Registering the Window Class    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(&wc))    {        MessageBox(NULL, "Window Registration Failed!", "Error!",            MB_ICONEXCLAMATION | MB_OK);        return 0;    }    // Step 2: Creating the Window    hwnd = CreateWindowEx(        WS_EX_CLIENTEDGE,        g_szClassName,        "The title of my window",        WS_OVERLAPPEDWINDOW,        CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,        NULL, NULL, hInstance, NULL);    if(hwnd == NULL)    {        MessageBox(NULL, "Window Creation Failed!", "Error!",            MB_ICONEXCLAMATION | MB_OK);        return 0;    }    ShowWindow(hwnd, nCmdShow);    UpdateWindow(hwnd);    // Step 3: The Message Loop    while(GetMessage(&Msg, NULL, 0, 0) > 0)    {        TranslateMessage(&Msg);        DispatchMessage(&Msg);    }    return Msg.wParam;}


That is guaranteed to compile. I took it from here.
Hehehehehehe..not to be a pain..but could you take my Functions.cpp and arrange it to work? Please??
The best thing to do is just choose whatever you think you'd prefer, and go for it. -Promit

This topic is closed to new replies.

Advertisement