Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All Again.

i have a class that can manage a window.
now what im doing now is makeing another class that uses that "Window Management Class" and have the Windows Message Procedureide my class.
it works while it is declared as statice, but the problem is when i try to use non static veriables inside that static message handler, VS2010 does not like it.
is there a way to access those non statice variables ?
Thanks in Advance.

---edit---
the "Window Management Class"creates a window with the desired icon etc.
and i have another class called class2 and i want the windows message procedure LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) to be inside class2, now it works if it is declared as static.
and ive looked at passing the class instance but i dont see a way to.
Posted
Updated 20-Jun-11 0:54am
v2

You can't use member variables, properties and methods from a static method. So if your message handler only works when declared as static there is no way to access any non static members.

Cheers!

--MRB
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jun-11 12:13pm    
Formally I agree, my 5.

However, there is a method to mimic instance function behavior and pass processing to an instance method! Please see my solution.
--SA
You cannot access non-static members inside a static method: it is a C++ rule, not a VS2010 idiosyncrasy.

The usual workaround is passing the variable (or the variables, at worst this for the whole class instance) to the static method. If you cannot do that (because, for instance, the method signature is fixed) than you can make another static method (that accepts and returns such instance variable) and then call it from your window message procedure.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jun-11 12:14pm    
Formally I agree, my 5. But OOP solution does exist.

However, there is a method to mimic instance function behavior and pass processing to an instance method! Please see my solution.
--SA
CPallini 20-Jun-11 15:16pm    
Well, SetWindowsLong, from my point of view, has nothing to do with an OOP solution. However it is a nice trick my 5.
Sergey Alexandrovich Kryukov 20-Jun-11 21:54pm    
Thank you.
Well, it depends what you call OOP. I don't mean the Windows API function is OOP. The OOP is the wrapper class, and this function is the way to wrap non-OOP with a OOP wrapper, to put OOP reference inside windows structure.
--SA
Your question is not clear.i understand that you are trying no access a nonstatic member variable in your static function.something like as shown below.
class A
{
    int m_A;
    static void Test()
    {
        m_A = 0;
    }
}

If this is what you are trying to do, It is not possible.
You have to make your data member also static.No otherway to do this
class A
{
    static int m_A;
    static void Test()
    {
        m_A = 0;
    }
};
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 20-Jun-11 12:14pm    
I think my solution explains things and solved the problem in OOP way.
Please see it.
--SA
Window Procedures as Class Member Functions[^] discusses different answers (including SAKryukov's) to your question.
cheers,
AR
 
Share this answer
 
Comments
ryan20fun 21-Jun-11 6:18am    
Thanks, this is a better then what i was trying to do :).
You need to make static function behave like it is the instance one (see other answers). To to that, use Windows API SetWindowLong/GetWindowLong, see http://msdn.microsoft.com/en-us/library/ms633591(v=vs.85).aspx[^], http://msdn.microsoft.com/en-us/library/ms633584(v=vs.85).aspx[^]. Use index of, say, DWL_USER+1.

Use these method to store the instance of the class processing events (and essentially doing all work as a wrapper for your low-level non-OOP window). You will retrieve the instance of the class in your static function, cast it to the pointer to your instance and call some instance, not static function which does all the job.

—SA
 
Share this answer
 
v2
If you Google it, you might find some ways to do it.
 
Share this answer
 
Comments
ryan20fun 21-Jun-11 6:02am    
i did, many times.
but i did not find a suitable way.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900