Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Find boost header files using gnu make (MinGW) on Win 7 11 86
mixing C++ & C# in Vis Studio 2013 7 170
Unable to start eclipse ? 17 141
Path to  STL Map header file 1 57
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

776 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