Jump to content
  • Advertisement
Sign in to follow this  
xegoth

OO Dialog headache.

This topic is 4853 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

I'm trying to create a class to represent a Win32 dialog in my app. The problem is, the dialog must have a proc which can't be a member of a class. Here's how I implimented it:
// Global
CConfigMenu *g_ConfigMenu = NULL;

// A callback director for the graphics settings dialog's message handler.
BOOL CALLBACK ConfigDlgProcDirector( HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam )
{
	return g_ConfigMenu->ConfigDlgProc( hDlg, message, wParam, lParam );
}

CConfigMenu::CConfigMenu() :
					m_pEnumeration( NULL )

{
	g_ConfigMenu = this;
}


This is all good and fine unless there is more than one instance of the class. Does anyone know of an elegant way to roll a win32 dialog up into a class? My current way of doing it seems hackish.

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
You can always make it a static member of the class. In the header something like:

class CConfigMenu
{
public:
CConfigMenu();
...
static BOOL CALLBACK DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);

private:
HWND m_handle;
};

and in your implementation something like:


CConfigMenu::CConfigMenu
{
m_handle = CreateDialog(..., ..., ..., DialogProc);
}

BOOL CALLBACK CConfigMenu:DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
//implementation here
}


It had been a long time since I did any Win32 stuff so this might not work but it might give you something else to think about.

Share this post


Link to post
Share on other sites
A static function within a class can be pointed at with an ordinary function pointer, and thus work as a callback, but there can still only be one instance of it per class, which doesn't really make it any better than a global.

The problem with a non-static member function being used as a callback is simply: member of which object? You need a way to indicate which CConfigMenu is active (and should thus have its ConfigDlgProc() registered as a callback), and the syntax gets quite hairy - but it is doable.

[google] "pointer to member function", and find out what exactly CALLBACK expands to, and see if you can't figure something out.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
As a refinement then how about using a map to associate between the hwnd and the instance of the dialog class? Something like:

class CConfigMenu
{
...
private:
HWND m_handle;

static std::map s_wndMap;
};

CConfigMenu::CConfigMenu
{
m_handle = CreateDialog(..., ..., ..., DialogProc);
s_wndMap[m_handle] = this;
// other setup here
}

CConfigMenu::~CConfigMenu
{
s_wndMap.erase(m_handle);
// other clean up here
}

BOOL CALLBACK CConfigMenu:DialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
CConfigMenu *current = s_wndMap[hDlg];
//implementation here
}


I might have gotten the semantics for the map wrong because, as I said, it has been a while.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!