Solved

Window Procedure inside a class - Problems with static - Non-static and access to other class functions

Posted on 2003-10-27
4
696 Views
Last Modified: 2012-05-04
Hi people. Here is my problem.
My problem basicaly is: Im creating a class for a window, and i have a window procedure inside this class. But inside the WNDCLASSEX    lpfnWndProc i must declare the MyWindow::WinProc function. And the compiler just dont accept this, only if i declare WinProc as STATIC. Declaring static, all looks ok, but now i cant access the other class functions from inside WinProc procedure. Oh my, there´s is no solution for this?

Thanks a lot for your help!!

Here is the code:

class MyWindow
  {
     HWND window, button1, button2, BckImage;
     void RegClass(HINSTANCE);
     void Create(HWND,HINSTANCE);
     void Show();
     void Hide();
     static LRESULT CALLBACK WinProc(HWND,UINT,WPARAM,LPARAM);
  };

void MyWindow::RegClass(HINSTANCE h)
  {
     WNDCLASSEX w;
      w.lpfnWndProc = WinProc;  
     // here is all the variables of WNDCLASSEX
  }

void Create(HWND parent, HINSTANCE h)
  {
       // here comes the creation of windows, etc...
  }

void MyWindow::Show()
  {ShowWindow(window,1);}
voidMyWindow::Hide()
  {ShowWindow(window,0);}

LRESULT CALLBACK MyWindow::WinProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
  {
       // here is the window procedure code,etc...
  }
0
Comment
Question by:richardomus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 125 total points
ID: 9628611
The usual technique is to store the 'this' pointer of the corresponding 'MyWindow' object in the 'GWL_USERDATA' storage area that is associated with every window, e.g.

SetWindowLong ( window, GWL_USERDATA, ( LONG) this);

and in your 'WinProc()', use

MyWindow* pWnd = (MyWindow*) GetWindowLong ( hwnd, GWL_USERDATA);

pWnd->CallAnyFunctionofMyWindow ();
0
 
LVL 6

Expert Comment

by:GaryFx
ID: 9628647
The solution is to create the windows procedure outside the class, making sure that the relevant object is somehow embedded inside one of the parameters.

The problem is that Windows callbacks are essentially C functions, not C++.  As such, they don't know how to deal with the implicit object required by non-static methods.  Or, to put it another way, they have no way of setting the value for "this."

So, short of not using a class at all, the only solution is to pass the object via the parameters,and then inside your new windows procedure call something to get the real thing.

For example,

LRESULT CALLBACK MyWinProc(HWND hwnd, UINT msg, WPARAM w, LPARAM l)
 {
      // here is the window procedure code,etc...
      MyWindow* pMywindow = (MyWindow*)l;
      pMywindow->realWinProc(hwnd, msg, w);
 }

This assumes that the LPARAM is available to you for this purpose.  If not (and I don't know off the top of my head), you'll have to pass it some other way.

Gary
0
 
LVL 86

Expert Comment

by:jkr
ID: 9628746
>>making sure that the relevant object is somehow embedded inside one of the parameters

That's the main problem - you cannot make that sure, as a couple of window messages use *both* parameters, a very common one being e.g. WM_COMMAND.
0
 
LVL 1

Author Comment

by:richardomus
ID: 9629283
Thanks!! This solution is incredible, and smart. Thank you both for helping me. It works really well!!! Bye!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question