Date: 2004Aug24
OS: Windows
Q. Is there a way to avoid global variables with win32 dialogs?
A. Use DialogBoxParam() and Set/GetDialogData() like this:
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);
Load(hDlg, pRecord);
}
return TRUE;
case WM_COMMAND:
id = LOWORD(wParam);
code = HIWORD(wParam);
switch(id)
{
case IDOK:
{
Record *pRecord = (Record *) GetDialogData(hDlg);
Save(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)
{
return DialogBoxParam(ghInstance, MAKEINTRESOURCE(IDD_MY_DIALOG), hDlg, (DLGPROC) MyDialogDlgProc, (LPARAM)record);
}
| What this info useful to you? You can donate to say thanks |
Add a comment
Sign in to add a comment