• Advertisement
Sign in to follow this  

Win32: virtual dialog callbacks and CreateDialog

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

Hey, Im trying to create a simple base Dialog box class that I can derive off of for several dialog boxes that I need for my program. I was thinking something like:
class cDialog
{
public:
	cDialog();
	~cDialog();

	HWND getDialogHandle() { return m_dialogHandle; }

	void create( HINSTANCE windowInstance, LPCTSTR dialogString, HWND parentHandle );

protected:
	virtual LRESULT CALLBACK dialogCallback( HWND, UINT, WPARAM, LPARAM ) = 0;

private:
    HWND m_dialogHandle;
};
With a virtual callback so I can implement that for each individual dialog I make. Then call CreateDialog like:
m_dialogHandle = CreateDialog( windowInstance, dialogString, parentHandle, (DLGPROC)dialogCallback );
Of course this doesnt work, Im looking for some pointers on how to get this to work with the virtual function, do I need to do some crazy <reinterpret_cast> stuff?

Share this post


Link to post
Share on other sites
Advertisement
You'll find that there is a certain amount of functionality that every implementation of the virtual dialogCallback method is going to include - default message handling, storing and retrieving the this pointer and so on. I've found that having the base class implement this common code works well. Also, the use of message maps keeps the code clean - no large, cumbersome switch statements.

class Dialog
{
// this is protected to prevent instantiation of this class
// it must be used as a base class
protected:
Dialog (map <message, function> message_map);
public:
virtual ~Dialog ();

private:
static LRESULT MessageHandler (HWND dialog, UINT message, WPARAM w_param, LPARAM l_param);

map <message, function> m_message_map;
};

The constructor first initialise the m_message_map member using the given argument, calls CreateDialogParam passing the this pointer as the creation parameter and the Dialog::MessageHandler method handler.

The message handler needs to store the this pointer in the dialog's extra data area (GWL_USERDATA), then uses the m_message_map to call the handler for a given message, calling the default window procedure is no handler is defined in the map.

This allows the derived classes to implement methods for the messages they're interested in with all the housekeeping stuff kept in the common base class.

Skizz

Share this post


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

  • Advertisement