HWND hWnd

Started by
10 comments, last by Xai 17 years, 1 month ago
How does hWnd allow functions used after other functions use variables of those functions as if those variables were part of that function. example:
void function1(HWND hwnd)
{
   int x;
   x=0;
}

void function2(HWND hwnd)
{
   x+=1;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
   MSG msg;
   MyRegisterClass(hInstance);
   if(!InitInstance(hInstance, nCmdShow))
      return FALSE;
   function1(hWnd);
   function2(hWnd);
   while(GetMessage(&msg, NULL, 0, 0))
   {
      TranslateMessage(&msg);
      DispatchMesssage(&msg);
   }
}

if this code is incorrect, please tell me why. I know it's incomplete.
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
Advertisement
I don't follow. HWND is just another type, that's typedef'd to be a void* (A little more involved than that, but essentially just a void*). Adding a HWND as a parameter doesn't let you do anything special.

Can we see some full code to see what you mean?
HWND does no such thing, that code, as it stands, will not compile. You will have an error in function2, because x is not declared.

If you do see code that compiles, post all of that code. You probably have a global variable called 'x' someplace.
yeah I think jpetrie is right.

I think this is more of a 'scope' question rather than anything to do with HWND.

maybe you could rephrase the question my friend.

From what I see you are delaring 'x' in func1, and trying to use it in func2 ??

If that is your problem, then yeah, you'll need to make 'x' global.
----------------------------------------Now just hit that link that says 'Rate This User' [wink]
The situation you are talking about IS global variable usage. I assume you are not talking about code you thought up and wrote from scratch, but code you see in common projects / examples.

The normal idiom for many old Visual C++ 6.0 people was to put all the really common shared things such as the directDrawDevice, mainWindow, etc as globals so that they would save some typing not having to pass them around (and maybe a very tiny amount of efficiency), and cause they didn't want to write complex OO manager classes they didn't understand (you know the classic C question, why do C++ people put the globals in a class when they know there is only going to be one instance?).

Well versed OO people know that its just as easy to organize everything into its proper containers and then you can mix-and-match pieces (libraries, modules, whatever) in different projects much more safely and easily.
What I'm asking is, certain functions have HWND hwnd as a parameter, why so?
I noticed that functions not used in WinMain do not have HWND hwnd as a parameter.

By the way, the reason that i couldn't give you the entire program, was that i was on a computer at school, i didn't have the files with me. I still am not able to give you my code though, so i'll just have to ask questions like this.

I use directx 9.0b and Microsoft Visual Studio .NET 2003. I'm reading the book "Beginning Game Programming" by Jonathan S. Harbour. (Ever heard of him?)
I got to the part where i displayed a Hummer rotating around in the fullscreen mode. I works perfectly, (after i figured out i had to add some directx libraries to the linker command line!) and i was interested in what exactly hWnd does and how to use it.

also...

I got this neat little program called anim8tor. Has anyone heard of it? (It makes 3d models.) and if so, i have two extra questions,
1) How do you make jointed models and use them in your game?
2) How do you export an animated scene into a .X file?
DO NOT TIME TRAVEL! when you change your position in time, your position in space stays the same relative to the point of the big bang. meanwhile Earth is now somewhere very far away. IN OTHER WORDS: u arn't in Kansas anymore.
An HWND is a handle to a window. Functions take it as a parameter where a reference to a window is required. The reason you tend not to see HWND outside of WinMain in games, is because games don't need to use/manipulate windows beyond creating one at the beginning and destroying it at the end.

Degra
all of the things that start with H in Win32 programs are "handles" they are basically like logical pointers (instead of a memory address they represent just an arbitrary id for that window you are using). Almost every win32 function that creates or retrieves an OS object (Window, Device Context, Brush, Pen, etc) returns a handle. Almost every Win32 function that modifies or uses an OS object accepts a handle parameter as an object.

When people make a main window, they often store it in a global variable and just access it from there, rather than pass it around to every function that might need it.
From the sound of it, it seems like you do not understand how WIN32 API works. I would highly recommend you learn the basics before using another library that relies on it (like DirectX).

I don't have any Win32 tutorials on me, but MSDN is a great resource for all your Win32 (and even DX) needs.
Comrade, Listen! The Glorious Commonwealth's first Airship has been compromised! Who is the saboteur? Who can be saved? Uncover what the passengers are hiding and write the grisly conclusion of its final hours in an open-ended, player-driven adventure. Dziekujemy! -- Karaski: What Goes Up...
Quote:Original post by Koobazaur
From the sound of it, it seems like you do not understand how WIN32 API works. I would highly recommend you learn the basics before using another library that relies on it (like DirectX).

I don't have any Win32 tutorials on me, but MSDN is a great resource for all your Win32 (and even DX) needs.


I disagree, you don't need to know a lot about the win32 api to use directx. As degra said, you usually just create a window at the start and destroy it at the end, and there's a gazillion tutorials online showing how to do just that.

This topic is closed to new replies.

Advertisement