Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

729 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