Advertisement

Static Member Functions?

Started by August 09, 2000 12:51 PM
3 comments, last by Grugnorr 24 years, 4 months ago
.- First of all: Thank you for reading this... .-Ive got a problem trying do make my CWindow class. As you know you need a function to process the windows messages : WindowProcedure. If you write your WndProc as a class function, method, you need it to be static.I´ve found this by reading Adrian Perez´s Advanced 3D Programming... Chapter 1 Demo. He exactly says:" The function is static because non-static class functions havea hidden first variable passed in (the this pointer)that is not compatible with the WndProc declaration". The BIG problem is that static function can only acces static variables, so I cannot manage Windows messages and set bActive for example without setting these vars as static... .-What should I do? .-What are static functions for? .-Help??????????????????????????????? What the hells!
What the hells!
.-Having a look at his CWindow class I´ve seen he doesn´t
declare WndProc as static!!!

.-If I don´t declare it as stactic I get a compiling error:

C2440: ''='' : cannot convert from ''long (__stdcall CWindow::*)(struct HWND__ *,unsigned int,unsigned int,long)'' to ''long (__stdcall *)(struct HWND__ *,unsigned int,unsigned int,long)''

I´m now remembering somethings about directives to change the way you call a function,and that´s looks to be the problem.

.- Any help?


What the hells!
What the hells!
Advertisement
Uh, I never declaired my WndProc as static.

and it works just fine.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
if WndProc is a member of a class it will have to be static because it needs to be called with out specifing an object to work on and therefore can only access variables that are declared independant of any object and in that case they would have to be static.

Grugnorr: Your probably going to have to declare bActive as static witch will limit you to only one CWindow object that will function flawlessly (sp?). That shouldn''t be a problem though as you probably wint need any more than one CWindow object.

------------------------------

"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
------------------------------"My sword is like a menacing cloud, but instead of rain, blood will pour in its path." - Sehabeddin, Turkish Military Commander 1438.
Grugnorr:

You can pull of some fancy windows hackery to get around this nasty problem.

I got this technique and the code from Andreas Jönsson, at http://www.witchlord.com/tutorials/d3dapp.asp.

Basically what you do is when creating your window, you tell windows that you are using a class to handle your windows messages. So you would call CreateWindowEx inside your CWindow class, you would pass it the "this" pointer as shown below. This way, when the window is created it will be passed the address of your class, which is a roundabout way of passing your static window message class the app pointer =).

m_hWnd = CreateWindowEx( ExStyle, "Application Window", 	                 m_szWindowTitle, Style, PosX, PosY,                         Width,Height,NULL,NULL,m_hInst,this );  


Below would be your statically declared WndProc in your class, which will just pass along the message to the real message handler.

LRESULT CALLBACK CWindow::WndProc( HWND hWnd, UINT uMsg,                                    WPARAM wParam, LPARAM lParam )CWindow *pWin = NULL;if( uMsg == WM_CREATE ){  // Extract the window pointer parameter.  CREATESTRUCT *pCS = (CREATESTRUCT *)lParam;  pWin = (CWindow*)pCS->lpCreateParams;  if( pWin )  {    // Save it in the window handle.    SetWindowLong(hWnd, GWL_USERDATA, (LONG)pWin);  }} else{  // Extract the object from the window handle.  pWin = (CWindow *)GetWindowLong(hWnd, GWL_USERDATA);}if( pWin ){  // Let the object handle it.  return pWin->MsgProc(hWnd, uMsg, wParam, lParam);} else{  // Just do the default if the window is not created yet.  return DefWindowProc(hWnd, uMsg, wParam, lParam);}} 


So, in summary, declare your WndProc statically, pass the class reference in the Window Creation parameters and then call your message handler, which doesn''t have to be static!

DISCLAIMER:
The code listed here is all Witchlord''s, I just put up the explanation.

PreManDrake

This topic is closed to new replies.

Advertisement