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 MyData { int stuff1; int stuff2; int stuff3; }; static INT_PTR __stdcall MyDialogDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam) { WORD id, code; switch (message) { case WM_INITDIALOG: { MyData *pMyData = (MyData *) lParam; SetDialogData(hDlg, pMyData; // Now use you can use pMyData to load your dialog - eg: // MyLoad(hDlg, pMyDataxn); } return TRUE; case WM_COMMAND: id = LOWORD(wParam); code = HIWORD(wParam); switch(id) { case IDOK: { MyData *pMyData = (MyData *) GetDialogData(hDlg); // Save what the user entered into pMyData - eg: // MySave(hDlg, pMyData); EndDialog(hDlg, IDOK); } break; case IDCANCEL: EndDialog(hDlg, IDCANCEL); break; } break; // end of WM_COMMAND } return FALSE; } int MyDialog(const HWND hwndParent, MyData *pMyData) { // 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), hwndParent, (DLGPROC) MyDialogDlgProc, (LPARAM)pMyData); } void ExampleUse(const HWND hwndParent) { MyData MyData; MyDialog(hwndParent, &MyData); }
Programming Tips - Win32: Is there a way to avoid global variables with WinXX dialogs?
Date: 2004Aug24
Update: 2025jul20
Language: C/C++
OS: Windows
Platform: win32
Keywords: param, parameter
Q. Win32: Is there a way to avoid global variables with WinXX dialogs?
A. Use DialogBoxParam() and Set/GetDialogData() like this: