• Advertisement
Sign in to follow this  

Window in a Class

This topic is 4730 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings! I would like to get my window stuff into a single class, for example: class CMyWindow { public: virtual void Create(); virutal void WindowProc(); virtual void Destroy(); }; How to Register "WindowProc" when calling CreateDialog(... WindowProc()) ? Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
the problem is that the winproc has to be a static function. You cant pass in a class member as the message handler. You can however keep pointers to your window class and call the winproc member function from within the static message handler.

Share this post


Link to post
Share on other sites
Quote:
Original post by IFooBar
the problem is that the winproc has to be a static function. You cant pass in a class member as the message handler. You can however keep pointers to your window class and call the winproc member function from within the static message handler.


Thats how I do it too. Just use a global variable for your window (or something list-like if you have more windows to handle) and call the appropriate message handler member function from your window class from the static (and global)WndProc function.

Share this post


Link to post
Share on other sites
For this purpose, each window has properties which you can set to any value you wish using Get/SetProp(), e.g. the window class' this pointer. You also have the option of using the slightly faster Get/SetWindowLong(GWL_USERDATA, ...), however this might conflict with other libraries (like MFC) using this data member of a window.

-Markus-

Share this post


Link to post
Share on other sites
Let me show you a bit ...

This is my temporary window class, how you do it is up to you.
1. I have a MsgCenter class that all messages is sent to (not recommended, but I am experimenting with it), that is the m_MsgCenter object.
2. I can ONLY create 1 and only 1 object of this class. a small change is needed to make it create more (simple change).
3. I use a small struct to initialize the window ...

#ifndef WNDBASE_H
#define WNDBASE_H

#include "help.h"
#include "msgcenter.h"
#include "logg.h"

struct WndType
{
LPCTSTR title;
int width;
int height;
bool fullscreen;
DWORD exstyle;
DWORD style;
};

class WndBase
{
public:
WndBase( HINSTANCE hInstance, WndType type, Logg *logg );
virtual ~WndBase();

virtual int CreateWnd(); // creates a window based on WndType
virtual int Initialize(); // not in use at the moment
virtual int Draw() = 0; // pure virtual??
virtual int Destroy(); // does cleanup

void Show( bool show ); // show/hide a window
void ShowMouse( bool show ); // show/hide cursor

MsgCenter* GetMsgCenter(); // return the m_pMsgCenter

protected:
HINSTANCE m_hInstance; // Holds The Instance Of The Application
HDC m_hDC; // Private GDI Device Context
HWND m_hWnd; // Holds Our Window Handle
MSG m_uMsg; // message variable

DWORD m_dwExStyle; // Window Extended Style
DWORD m_dwStyle; // Window Style
RECT m_ScreenRect; // Grabs Rectangle Upper Left / Lower Right Values
RECT m_ClientRect; // Rect of client area
bool m_fullscreen; // fullscreen?
bool m_active;

MsgCenter *m_pMsgCenter;
Logg *m_pLogg;

LPCTSTR m_sCName;
WndType m_WndType;
private:
static LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
LRESULT MessageProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );

static WndBase *sThis;
};
#endif




mmmm.... was thinking of posting a simple .cpp code to just show the implementation, but the code is big, and awfull

But basically, just sett the sThis correctly, and the rest should be ok ...

I must clean up my code first, and improve a few small things before I can post this base class.
Once you have the this base class ready, you just inherit this object into your GDI, DX or OpenGl object to make a window object. It is not 100% DX friendly yet, but give me some time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement