Solved

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

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

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

Suggested Solutions

Title # Comments Views Activity
convert char array to number in c 5 86
Safe conversion? 4 72
Issues with C++ Class 19 101
FMX TEdit KeyUp handler detecting  "enter" key 4 14
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

820 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