inline void SetDialogData(const HWND hDlg, const LPVOID pVoid) { SetWindowLongPtr(hDlg, DWL_USER, (LONG) pVoid); } inline LPVOID GetDialogData(const HWND hDlg) { return (LPVOID) GetWindowLongPtr(hDlg, DWL_USER); } struct Record { int stuff1; int stuff2; int stuff3; }; static BOOL __stdcall MyDialogDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { WORD id, code; switch (message) { case WM_INITDIALOG: { Record *pRecord = (Record *) lParam; SetDialogData(hDlg, pRecord); // Now use you can use pRecord to load your dialog - eg: // MyLoad(hDlg, pRecord); } return TRUE; case WM_COMMAND: id = LOWORD(wParam); code = HIWORD(wParam); switch(id) { case IDOK: { Record *pRecord = (Record *) GetDialogData(hDlg); // Save what the user entered into pRecord - eg: // MySave(hDlg, pRecord); EndDialog(hDlg, IDOK); } break; case IDCANCEL: EndDialog(hDlg, IDCANCEL); break; } break; // end of WM_COMMAND } return FALSE; } int MyDialog(const HWND hDlg, Record *record) { // You need to set global variable HINSTANCE ghInstance when your program begins // You need to make your dialog and call it IDD_MY_DIALOG return DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_MY_DIALOG), hDlg, (DLGPROC) MyDialogDlgProc, (LPARAM)record); }
Programming Tips - Is there a way to avoid global variables with WinXX dialogs?
Date: 2004Aug24
Updated: 2013mar1
Language: C/C++
OS: Windows
Platform: win32
Keywords: param, parameter
Q. Is there a way to avoid global variables with WinXX dialogs?
A. Use DialogBoxParam() and Set/GetDialogData() like this: