Advertisement

Callback crisis

Started by March 05, 2001 01:31 PM
2 comments, last by ludemann8 23 years, 11 months ago
I have this problem with a callback with dialog boxes... I want to make a class giving basic functionality using the DialogBox/CreateDialog functions, but the callback function is a problem, as I don't know how to access my original class from it. Creating a global value just defeats the purpose of having it in a class, and there are no user defined parameters I can pass into the DialogBox/CreateDialog functions for use in the callback function (so no pointer passing). And just using mfc is out because I want to use seperate resources from my dlls (which mfc, as far as I can figure out, spits the dummy over (AfxSetResourceHandle sets the resource for the entire program)) At the moment it is like this...
    
int CMyDialog::DoModal()
{
	DialogBox(hInst,MAKEINTRESOURCE(DialogRef),AfxGetMainWnd()->GetSafeHwnd(),DialogProc);
	return 0;
}

INT_PTR CMyDialog::DialogProc(HWND hWndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_CLOSE:
		EndDialog(hWndDlg,0);
		break;
	};
	return FALSE;
};
    
where DialogProc is static, but I want to get at the CMyDialog class that created the hWnd... This is really pissing me off. Any ideas? btw, I'm not going to be able to reply for a while... Edited by - ludemann8 on March 5, 2001 2:34:25 PM
Here is a simple idea, hope it what you need.
When you call your callback function from you class pass this to it, now you've got a pointer to you dialog class.
I just noticed that you callback function is part of you dialog class so you don't need to pass this to it, its already available.


Edited by - xstreme2000 on March 5, 2001 3:43:44 PM
Advertisement
Here''s how it''s usually done:

  int CMyDialog::DoModal(){	DialogBoxParam(hInst,MAKEINTRESOURCE(DialogRef),AfxGetMainWnd()->GetSafeHwnd(),DialogProc, (int)this);	return 0;}INT_PTR CMyDialog::DialogProc(HWND hWndDlg,UINT uMsg,WPARAM wParam,LPARAM lParam){		CMyDialog*	_this;	_this = (CMyDialog*)GetWindowLong( hWndDlg, GWL_USERDATA );	if( _this == NULL )		return FALSE;		switch(uMsg)	{		case WM_INITDIALOG:			SetWindowLong( hWndDlg, GWL_USERDATA, lParam );			break;		case WM_CLOSE:			EndDialog(hWndDlg,0);			break;		case WM_COMMAND:			_this->OnCommand();			break;	};	return FALSE;};  


Cheers!
Meduzza
Thanks for the replies,

xstreme2000, that is no good as the callback function type is already defined.
Meduzza, thanks very much... I had a look and found that DialogBox is just a redifinition of DialogBoxParam. Thanks.

Edited by - ludemann8 on March 6, 2001 7:53:32 AM

Edited by - ludemann8 on March 6, 2001 7:55:52 AM

This topic is closed to new replies.

Advertisement