# Win32 Programming Question

This topic is 4167 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I've been programming for 5 months now, and Im starting to dive into some basic graphics programming. [smile] I'm going to be starting to learn Directx 9.0, and I've been looking at a myriad of tutorials on Directx 9, but In every example I find, they create a Window using Win32. I've recently started to learn SDL as well, overall, so far its been pretty easy to pick up. My question(s) is, when you guys create an OpenGL application or a Directx application, do you guys code making the window everytime? Do you guys remember every single little this that it takes to create a basic window with a white background, or do you copy and paste? or, do you use SDL to create the window? How did you guys learn/remember how to create a window? It seems like a plethora to remember! [sad] example:
#include <windows.h>
#include "resource.h"

const char ClassName[] = "MainWindowClass";

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;

wc.cbSize           = sizeof(WNDCLASSEX);
wc.style            = 0;
wc.lpfnWndProc      = (WNDPROC)WndProc;
wc.cbClsExtra       = 0;
wc.cbWndExtra       = 0;
wc.hInstance        = hInstance;
wc.hIcon            = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hIconSm          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON));
wc.hCursor          = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground    = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName    = ClassName;

if (!RegisterClassEx(&wc))
{
MessageBox(NULL, "Failed To Register The Window Class.", "Error", MB_OK | MB_ICONERROR);
return 0;
}

HWND    hWnd;

hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
ClassName,
"Win32 Tutorial No.1",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
240,
120,
NULL,
NULL,
hInstance,
NULL);

if (!hWnd)
{
MessageBox(NULL, "Window Creation Failed.", "Error", MB_OK | MB_ICONERROR);
return 0;
}

ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);

MSG    Msg;

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

return Msg.wParam;
}



##### Share on other sites
Don't retype code every time you start a project. Tedious tasks are what computers were designed for. Visual Studio has options to create templates so that all of those things will be done for you when you start a new project.

I actually use a pre-built engine, Irrlicht, because I got tired of debugging my bad OpenGL. I never did finish learning it OGL, but I have a lot more done now.

SDL was pretty good, but I even made a template for SDL projects in my IDE.

If you must use the Win32API, try building a class library for it.

##### Share on other sites
God, my brain doesnt have room to memorize 1/100th of what I need to program. I keep my references close at hand, bookmark sites that I find incredibly handy, keep all my previous code in directories so I can go back and cut and paste.

Plus, if I find myself doing something more then a couple times, I generally make my own class framework to handle it. That or adopt someone elses.

##### Share on other sites
Similar to what templewulf stated. I have a series of source files that I start out with now, that have the majority of the functionality that I need.

But honestly, it only seems like a lot because you haven't done it often. When you work with it a lot, and understand what you're doing, creating a window will become second nature to you.

For me, I'm struggling to create stuff in Obj C & the whole NS architecture. Trying to get even a simple button on the screen programatically seems like a daunting task. On the other hand, I can get a button on a window in a matter of minutes using Win32. One I'm a complete beginner at, the other, I've had years of experience using.

*EDIT*

Although, I will admit, I have difficulty understanding some of the choices MS made in their API creation, such as cbSize. While I understand what its theoretically for, I've NEVER, not once, seen it initialized as anything other than the size of the structure.

##### Share on other sites
Remember, any code you need to use again can be: A) Put in a library to be reusesd, B) Copied / pasted from your previous project, C) Put in a template / macro in the editor / IDE of your choice. I don't actually use custom templates / macros personally, but I do have a folder of various pieces of common starter code that I copy files out of when I need this skeleton stuff for each new project.

The beauty of computers, they perfectly remember everything you've ever done ... if you can jut remember where you put it :)

I highly recommend you grab something like subversion (svn) to just store little things you think you might want later. Its a lot cooler than using zipped up backups from a hundred different past experiences. I keep a sandbox area in my repository just for such little things, and I just go through and delete them when I'm certain they have no use for me anymore (just like email).

##### Share on other sites
So I should create .cpp file, and everytime I need a window, just add that file to my project? Is that what you mean by template?

##### Share on other sites
Raymond Chen on structure sizes

http://blogs.msdn.com/oldnewthing/archive/2003/12/12/56061.aspx

##### Share on other sites
You could do that yes. There are various levels of sophistication and automation you could apply but that's more or less what it comes down to.

Another option is old fashioned copy-and-paste. I have a minimal Win32 app that does nothing except throw up a window and provide a menu with an exit option. When I start a new app I just copy my simple app and start building from there.

Of course experience counts for a lot. After you've been creating Win32 apps for 15 years you'll probably be able to write the skeleton code from memory too.

##### Share on other sites
I've recently been fiddling around with Win32 myself, before I start to seriously learn DirectX. It seems that no matter what you're going to do in DirectX (or OpenGL), you still need to set up a window. I've seen on the web several different ways to do this, usually in a reusable library as has been suggested previously.

That said, I'd be curious to see the reusable code other people use, just to learn about the different ways you can do the same thing (set up a window). Could be interesting to see how different people structure their basic window system.

##### Share on other sites
Just to let you know, you can use Direct3D with SDL.

##### Share on other sites
When you're planning to reuse code, it's a good idea to document it. Some simple comments explaining what exactly a function does can make life easier later on. I see no comments in your code, which means you and I will have to read the low-level code itself to get an understanding of it's purpose.

What I did was commenting every function argument of those Win32 API calls. It might be overkill, but every time I need to get back to that sort of code, it's easy to pick it up again. I don't remember it myself - I let the comments do that for me. The same goes for all code you write, even if you never plan on reusing it. There may come a time you'll need to get back into it because of some obscure bug, and it'll save you time (and frustration) when all you need to read are some comments to understand what each part does. :)

##### Share on other sites
Thanx for your help guys! Im going to try and make a template/cpp file and whenever i need a window, just including in within the project. Sound good? [smile]

##### Share on other sites
I'm a beginner when it comes to Win32, but my advice would be to learn the code by heart. I too want to learn DirectX, so I started learning Win32 last week, and the method I use is basically memorizing the lines bit by bit. It helps a lot to understand the code too.

For the past week, whenever I had time, I practiced writing the full code to make a window from scratch, and now I can write it without any errors.

##### Share on other sites
Quote:
 Original post by DarklighterI'm a beginner when it comes to Win32, but my advice would be to learn the code by heart. I too want to learn DirectX, so I started learning Win32 last week, and the method I use is basically memorizing the lines bit by bit. It helps a lot to understand the code too.For the past week, whenever I had time, I practiced writing the full code to make a window from scratch, and now I can write it without any errors.

I have to disagree with this. Dont memorize the code, understand it. If you dont remember the members of the WNDCLASSEX structure or how the parameters go for CreateWindowEx(), dont worry - there's a place which can tell you that stuff.

##### Share on other sites
Quote:
 Original post by Colin JeanneI have to disagree with this. Dont memorize the code, understand it. If you dont remember the members of the WNDCLASSEX structure or how the parameters go for CreateWindowEx(), dont worry - there's a place which can tell you that stuff.

I do understand the code though. This is why it's so easy to remember. Also, I don't memorize the parameters for all the functions. If you have Visual Studio, you don't even have to consult the MSDN because it automatically tells you the function prototypes and the WNDCLASSEX members as you type.

##### Share on other sites
I learn Win32 by reading the MSDN on the actual functions I will require most. So many times I see people copying code from something that has worked before and not actually know what the lines of code really do or if they even need it.

How many people stick TranslateMessage before DispatchMessage and never process a WM_CHAR message types.

I memorise everything it takes to write a window but I always understand each line of code that goes into making that window and so I can avoid using redundant code.

Nuclear Rabbit:

This line
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);

you can set to
wc.hbrBackground = NULL;

After all, you probably will clear the viewport in your render function anyway :)

All the best with your Win32 learning.