Archived

This topic is now archived and is closed to further replies.

mk83

Windows in a class

Recommended Posts

mk83    122
Hi Guys, I''m trying to write a windows application, I had it up and running, sort of, but my code looked a real mess, I''m trying to rewrite it in Class form to neaten things up. First problem, after I have created a Header file globals.h, I put a variable in it called BOOL ProgActive = true, this was to be the global variable that keeps the program running with a while (ProgActive) statement. It would not work though, if I changed ProgActive status to false anywhere but in the WinMain function the program would not terminate! I had to create ProgActive in the main .cpp file and redefine it using extern BOOL ProgActive, in the class .cpp file in order for it to terminate the program? Can anyone give me a reason for that? Second, when creating my WNDCLASS structure, the lpfnWndProc won''t let me assign a class private procedure to it even if I make it lpfnWndProc=&wMainWindow::WindowProc it comes up with a error, the only way I could get around that was to create a function outside of the class declaration. But this new function is not aware of which Window is actually calling it without specific coding i.e. if (wparam = MainWindow) then, the biggest problem I have with that is I want to create a Main window that can have multiple child windows, but not a specified ammount, the only way I can think of not having to hardcode for each possible instance of a ChildWindow would to have the WindowProc aware of who is calling it! Are there other ways of doing it? Thanks for the help, Cheers, MK83

Share this post


Link to post
Share on other sites
mk83    122
I got the Callback function to work by making it static, but I still have a problem with it knowing who calls it

    
class wMainWindow {
public :
// ***************** public variables - MainWindow *******************

HWND hbExit;
// ***************** public functions - MainWindow *******************

// (1) Default constructor

wMainWindow(HINSTANCE hofProgram);
// (2) Show window

Show();
// (3) Hide window

Hide();
private :
HINSTANCE hInstance;
HWND hMainWindow;
// ***************** private functions - MainWindow ******************

// (1) Function to set hInstance

int setInstance(HINSTANCE Settoo);
// (2) Main window message function

static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg,
WPARAM wParam,
LPARAM lParam );

};


LRESULT CALLBACK wMainWindow::WindowProc(HWND hwnd, UINT uMsg,
WPARAM wParam,
LPARAM lParam ){

switch (uMsg)
{
case WM_CLOSE :
ProgActive = false;
break;

case WM_COMMAND :
Hide();
break;

}
return(DefWindowProc(hwnd, uMsg, wParam, lParam));
};


But now when I try to call Hide(); it says that I need to call hide with a . or ->, but I'm not sure how to tell my program to call the instance of Hide that the button was pushed on?

Is it at all possible?

Regards,
Robert

Edited by - mk83 on November 5, 2001 3:53:35 AM

Edited by - mk83 on November 5, 2001 4:09:45 AM

Share this post


Link to post
Share on other sites
gizmomogwai    122
you cant call a non static member function without the instance of an obejct. you can only call Hide, if you have an instance of your windowclass.

you could map from the window-handle to your window-class-
instance an call the appropriate methods with this object ...

another problem is, that some windows controlls deliver their events to their parents windowproc, and not to their own ...

Share this post


Link to post
Share on other sites
mk83    122
Hi gizmomogwa

How would I do that? I mean map from the Window Handle to my class?

I''ve tried a number of things,but seem to just land up with memory errors,

Cheers,
MK83

Share this post


Link to post
Share on other sites
i also had to write a small class encapsulating that windows stuff; i simply used

  
void Window::Create (void)
{
...
SetWindowLong(hwnd, GWL_USERDATA, (long) this)
}

and in a global wndproc

LRESULT CALLBACK wndproc (HWND hwnd, ...)
{
Window * w = (Window *) GetWindowLong(hwnd, GWL_USERDATA);
}


i think that''s the fastest way

Share this post


Link to post
Share on other sites
gizmomogwai    122
nice solution ... the create-method is mfc???

i took another aproach (not that elegant) ... a singleton maps all created window handles to the superclass of the ui-elemtents (e.g. window) ... each ui-elements registers itself at this singleton, when the window-handle is created ... for storing the relation i used a simple stl-map.

wouldnt be
LONG_PTR SetWindowLongPtr(
HWND hWnd, // handle to window
int nIndex, // offset of value to set
LONG_PTR dwNewLong // new value
);
a safer solution for the future??


Edited by - gizmomogwai on November 6, 2001 12:54:04 PM

Share this post


Link to post
Share on other sites