Click to See Complete Forum and Search --> : Can Someone Explain To Me What All This Means?


Mythic Fr0st
February 11th, 2007, 08:25 PM
#include <windows.h>

/* 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)GetStockObject(BLACK_BRUSH);

/* 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 */
"Windows App", /* Title Text */
WS_OVERLAPPEDWINDOW, /* default window */
CW_USEDEFAULT, /* Windows decides the position */
CW_USEDEFAULT, /* where the window ends up on the screen */
544, /* The programs width */
375, /* 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;
}

Now, I see this, I see just a clump of text, I dont recognise what any of it does, except for stuff like switch() and case etc..

I learnt to program in C++ in ms-dos, but I dont get this, so can someone please explain to me what the main things to do are

And questions

1: How do I create a window

2: What does this do

LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

3: whats this for char szClassName[ ] = "WindowsApp";

I know its a global variable, but thats all, whats the ClassName For??

4: what the heck is this? the name of a function?


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

I dont get it, its like int WINAPI WinMain is the function name, but whats the stuff in the brackets ( )

5: How do I create a window

6: Where is a tutorial for this kind of thing?

I've tried looking, and they're all very "bad", tutorials so far,

any help appreciated thanks

Notsosuperhero
February 11th, 2007, 09:04 PM
1) You create a window by calling CreateWindow (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/createwindow.asp) or CreateWindowEx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windows/windowreference/windowfunctions/createwindowex.asp)

2) The window procedure handles all the messages that Windows OS will send to the window(i.e. resizing, moving, closing, minimize, etc...)

3) The class name is used in creating the window, basically you have a WNDCLASS (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windowclasses/windowclassreference/windowclassstructures/wndclass.asp) or WNDCLASSEX (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windowclasses/windowclassreference/windowclassstructures/wndclassex.asp) structure to fill out before creating the main window, this class is like an ID of sorts, so the window knows what Windows procedure to use. You also, depending on what structure you use, must call RegisterClass (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windowclasses/windowclassreference/windowclassfunctions/registerclass.asp) or RegisterClassEx (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/windowing/windowclasses/windowclassreference/windowclassfunctions/registerclassex.asp), respectively.

4) WinMain, is the main function for Windows, like in console apps you use main(), in Windows you WinMain, the WINMAIN is just a define for __stdcall calling convention. HINSTANCE is the instance of the application, the second parameter is now deprecated, but is just needed for it to compile correctly. And the other ones are the command line stuff.

5) See answer #1.

6) FunctionX (http://www.functionx.com/win32/index.htm) helped me out a lot. And if you've a little money to spend I highly suggest Programming Windows, 5th Edition (http://www.amazon.com/Programming-Windows-Fifth-Charles-Petzold/dp/157231995X) by Charles Petzold, its a bit dated but still is the best book on WinAPI, hardcover from Amazon.com was only $35 USD for me.

Cheers NSSH

Mythic Fr0st
February 11th, 2007, 09:49 PM
Thanks alot, i've been working on something, but it isnt registering


if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
is activating

#include <windows.h>

const char g_szClassName[] = "Window"; //the name of my window?

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) //a function?
{
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0); //errm what message? whats the 0 for?
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI
WinMain
(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc; //whats this, A window?
HWND hwnd; //Whats this?
MSG Msg; //Obviously a message variable?
//perhaps HWND means a handle window variable, called "hwnd"
//MSG declares a MSG variable called "Msg"
//and WNDCLASSEX declares a WNDCLASSEX variable "wc"
//register window class, whats this for??, whats a window class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc; // whats this?
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
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;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "I PWN U", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 400, 400, NULL, NULL, hInstance, NULL);
if (hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);

while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
I was copying off a tutorial any idea's thanks

Calculator
February 11th, 2007, 10:04 PM
It's funny really, the tutorial that Notsosuperhero linked you to explains all of that. In detail. Also, do you like to appear helpless? It strikes me as a bit odd.

Mythic Fr0st
February 21st, 2007, 06:43 PM
When I am helpless, I do like to appear helpless, as I want help lol