Sign in to follow this  
orcfan32

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

Recommended Posts

ildave1    561
http://www.bluesfear.com/tutorials/C%2B%2Bwindow_p1.php

Try google.com next time. Its power are godlike.

C++ is a Computer Programming language that is compatible with a ton of things... Refer to the google master for more information.

Share this post


Link to post
Share on other sites
Amazed    122
Best way would be found right here at http://www.gamedev.net/reference/articles/article1076.asp.

As an aside, C++ is not a Windows language. It can be used on any platform that possess a C++ compiler, which means it works on much more than Windows. :)

Share this post


Link to post
Share on other sites
ildave1    561
Quote:
Original post by orcfan32
It seems that that was what I was trying....


.... 'that'..... You mean google? Its tough stuff, I understand, but when you can create some good searching analogies, you'll become a master at the googling.

Its in you, you've just got to dig deep inside yourself. ;]

Share this post


Link to post
Share on other sites
orcfan32    100
Well, what I was doing was functions that create a window for you! :) Problem is, it says:

C:/CPP/Main.cpp: In function `int main()':
C:/CPP/Main.cpp:8: error: `CreateWND' undeclared (first use this function)
C:/CPP/Main.cpp:8: error: (Each undeclared identifier is reported only once for
each function it appears in.)


Here's the code for Main.cpp:
#include <iostream>
#include "Functions.h"
#include "Functions.cpp"
using namespace std;

int WinMain()
{
CreateWND("TEST", 500, 500);
}





Functions.h:
int CreateWND(string Title, int Width, int Height)




And Functions.cpp:
#include <iostream>
#include <string>
using namespace std;

int CreateWND(string Title, int Width, int Height)
{
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)

{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
Title, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
Width, /* The programs width */
Height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}

}





[Edited by - orcfan32 on July 17, 2005 2:28:28 PM]

Share this post


Link to post
Share on other sites
Oberon_Command    6081
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 [ code ][ /code ], 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.

Share this post


Link to post
Share on other sites
orcfan32    100
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

Share this post


Link to post
Share on other sites
Oberon_Command    6081
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.

Share this post


Link to post
Share on other sites
Oberon_Command    6081
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.

Share this post


Link to post
Share on other sites
Oberon_Command    6081

#include <windows.h>

const char g_szClassName[] = "myWindowClass";

// Step 4: the Window Procedure
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;

//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.

Share this post


Link to post
Share on other sites
Oberon_Command    6081
*sigh* Alright, I will, but just this once:

functions.h

#ifndef __FUNCTIONS_H
#define __FUNCTIONS_H


int CreateWND(string Title, int Width, int Height);

#endif







main.cpp

#include <iostream>
#include <string>
#include <windows.h>
#include "functions.h"

int WINAPI WinMain (HINSTANCE hThisInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgument,
int nFunsterStil)
{
CreateWND("Title", 640, 480);
return 0;
}





Functions.cpp


#include <iostream>
#include <string>
#include <windows.h>
#include "functions.h"
using namespace std;

/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/* Make the class name into a global variable */
char szClassName[ ] = "WindowsApp";



int CreateWND(string Title, int Width, int Height)
{
HWND hwnd; /* This is the handle for our window */
MSG messages; /* Here messages to the application are saved */
WNDCLASSEX wincl; /* Data structure for the windowclass */

/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof (WNDCLASSEX);

/* Use default icon and mouse-pointer */
wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default color as the background of the window */
wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;

/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx (&wincl))
return 0;

/* The class is registered, let's create the program*/
hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
Title, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
Width, /* The programs width */
Height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);

/* Make the window visible on the screen */
ShowWindow (hwnd, nFunsterStil);

/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage (&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}

/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}


/* This function is called by the Windows function DispatchMessage() */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message) /* handle the messages */
{
case WM_DESTROY:
PostQuitMessage (0); /* send a WM_QUIT to the message queue */
break;
default: /* for messages that we don't deal with */
return DefWindowProc (hwnd, message, wParam, lParam);
}

return 0;
}


Share this post


Link to post
Share on other sites
orcfan32    100
All right! Oberon_Command is 73H R0X0RZ, 73H NUMB3R 0N3, H3 D4 1337 M0N!! Rating UP!!

Translated from 1337: Oberon_Command is TEH ROXORZ, TEH NUMBER ONE, HE DA LEET MON!!

EDIT: Just one problem, there is a compile error:

C:/CPP/Main.cpp:11: error: `CreateWND' undeclared (first use this function)

EDIT AGAIN: Ok, the above error is fixed, now I have these:

In file included from C:/CPP/Main.cpp:5:
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:22: error: `hThisInstance' undeclared (first use this
function)

C:/CPP/Functions.cpp:22: error: (Each undeclared identifier is reported only
once for each function it appears in.)

C:/CPP/Functions.cpp:56: error: cannot convert `std::string' to `const CHAR*'

for argument `3' to `HWND__* CreateWindowExA(long unsigned int, const CHAR*,
const CHAR*, long unsigned int, int, int, int, int, HWND__*, HMENU__*,
HINSTANCE__*, void*)'
C:/CPP/Functions.cpp:59: error: `nFunsterStil' undeclared (first use this
function)

[Edited by - orcfan32 on July 17, 2005 6:22:52 PM]

Share this post


Link to post
Share on other sites
Oberon_Command    6081
Alright, here's what you do:

Add in a HINSTANCE parameter to your CreateWND() function and use that instead of hThisInstance. Also add a hFunsterStil (whatever THAT is) variable to your CreateWND() functionw. So, your new CreateWnd() call should look like this:

// here is the call
CreateWND("Title", 640, 480, hThisInstance, nFunsterStil);
// here is the prototype
int CreateWND(string Title, int Width, int Height, HINSTANCE hThisInstance, int nFunsterStil);


Change this:

hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
Title, /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
Width, /* The programs width */
Height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);


to this

hwnd = CreateWindowEx (
0, /* Extended possibilites for variation */
szClassName, /* Classname */
Title.c_str(), /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
Width, /* The programs width */
Height, /* and height in pixels */
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);


That's one of the problems with working with the Win32 API, you have to remember to convert C++ strings to char *'s...

Share this post


Link to post
Share on other sites
orcfan32    100
Still has 2 more errors:
'hThisInstance' is undeclared.
'nFunsterStil' is undeclared.

EDIT: Oh, I'm dumb, didn't read the first part of your post...

EDIT AGAIN: Ok, after that it says:


C:/CPP/Functions.cpp: In function `int WinMain(HINSTANCE__*, HINSTANCE__*,
CHAR*, int)':
C:/CPP/Functions.cpp:16: error: too few arguments to function `int
CreateWND(std::basic_string<char, std::char_traits<char>,
std::allocator<char> >, int, int, HINSTANCE__*, int)'
C:/CPP/Main.cpp:12: error: at this point in file /* A.K.A. Line 12..pointing to the function in main..? */




I'm sorry to bug you with this..but it seems that I have few to arguments in where I call the function (Main). So, I need to add something to hThisInstance and nFunsterStil. What would I add? I'm not familiar with window programming - only console.

Share this post


Link to post
Share on other sites
orcfan32    100
Woohoo! It works! Now I just have to figure out how to get rid of the console app in the background....thanks a ton, Oberon_Command!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this