Solved

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

Posted on 2003-10-27
4
659 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now