I have some defined constants:
#define IDD_MAINDIALOG 101
#define IDC_EXIT 1001
#define IDC_START 1002
#define IDC_STOP 1003
#define IDC_STATE1 1004
#define IDC_STATE2 1005
#define IDC_STATE3 1006
#define IDC_STATE4 1007
#define IDC_STATE5 1008
#define IDC_FORCE1 1009
#define IDC_FORCE2 1010
....
meaning corresponding cells of Static Text
in my program
And I need to constantly change values in these cells. I think to do in with SetDlgItemText
. But it needs the int
value of changed control as a second parameter. I tried to do like this:
sprintf(forceId, "IDC_FORCE%d", philNum 1);
sprintf(wisdomId, "IDC_WISDOM%d", philNum 1);
...
SetDlgItemText(hWnd, (int)forceId, force);
SetDlgItemText(hWnd, (int)wisdomId, wisdom);
But the program displays nothing, and while tracing I found that I have (int)forceId
and (int)wisdomId
values about 11793000 instead of 1007 or something.
So, what is the correct way to convert defined constant's name into the int
value which it is associated with?
I don't want to use exact names of controls to avoid copying of code, because I have many cells, and I also understand that in this case it is possible to use the numbers 1001, 1002, ... in a loop but I want to exclude crash of the program if some changes will be made (like deleting a cell with id IDC_STATE3
).
CodePudding user response:
Looks like your forceId
and wisdomId
are char*
, so you are using the addresses of those strings.
Regardless, you won't have IDC_STATE1
values at runtime, it will be 1004
For things like that, I would manually arrange the IDs in a proper order, and just do calculations based on the first ID:
SetDlgItemText(hWnd, IDC_FORCE1 philNum, force);
CodePudding user response:
Your constant names don't exist at runtime, they are only meaningful to the preprocessor. At runtime, only their numbers will exist. Everywhere your code uses IDC_FORCE1
, etc, they will be replaced with their actual numbers during the compiling process.
SetDlgItemText()
takes a parent window and a child control ID as input. You are trying to pass in a formatted string as the control ID. That will not work. Just pass in the actual control ID number as-is instead, eg:
#define IDC_FORCE_BASE 1009
#define IDC_FORCE1 IDC_FORCE_BASE 0
#define IDC_FORCE2 IDC_FORCE_BASE 1
...
SetDlgItemText(hWnd, IDC_FORCE_BASE philNum, force);
Do the same with IDC_STATE...
, IDC_WISDOM...
, etc.