-
Resources ID based on other resources ID
My problem: I need to give to icons resources identifiers values based on other (buttons) identifiers values.
example, if i have a button with CM_TEST identifier, i need to have a corresponding icon with a resource ID = CM_TEST+xxxx
I tried the method explained below without success
Including Shared (Read-Only) or Calculated Symbols
http://msdn.microsoft.com/en-us/library/zakskay9.aspx
So, i follow this method and my rc file contains:
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "dialogh2.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
my dialogh2.h content some icons identifiers as:
#define IDI_OK (IDOK+20000)
#define IDI_OK_DISABLED (IDOK+30000)
#define IDI_CANCEL (IDCANCEL+20000)
#define IDI_CANCEL_DISABLED (IDCANCEL+30000)
Result: all identifiers are not calculated and seems to get a value of 0.
some observations:
-icon property window show :
ID: "(IDOK+20000)"
(quotes are present)
-when displaying rc resource symbol dialog, my identifiers IDI_OK, IDI_OK_DISABLED ... don't appear (read-only symbols checkbox checked)
-I did some tests as :
#define IDI_OK (1+20000)
same result..
In all cases, identifiers seems to evaluate to 0 (because they become my app icon and i already have an app icon with ID = 1)
How to solve this problem ?
Thank you very much in advance
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
my dialogh2.h content some icons identifiers as:
#define IDI_OK (IDOK+20000)
#define IDI_OK_DISABLED (IDOK+30000)
#define IDI_CANCEL (IDCANCEL+20000)
#define IDI_CANCEL_DISABLED (IDCANCEL+30000)
Are you sure that IDOK and IDCANCEL are already defined, prior to the above #defines? In other words are you certain that windefs.h or winuser.h have already been included somehow? #define is very unintelligent and the compiler probably won't give you a warning if those symbols aren't known yet.
-
Re: Resources ID based on other resources ID
yes, they are.
And i also test different things as:
#define IDC_TEST 10
#define IDI_OK (IDC_TEST+100)
same problem !
even
#define IDI_OK (1+100) don't work
-
Re: Resources ID based on other resources ID
You're doing something wrong somewhere else. Build and run this small program as a console app:-
Code:
#include <stdio.h>
#include <conio.h>
#define IDC_TEST 10
#define IDI_OK (IDC_TEST+100)
int main (int argc, char *argv[])
{
int n = IDI_OK;
printf ("Value of 'n' = %d", n);
getch ();
return 0;
}
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
#define IDI_OK (1+100) don't work
Doesn't that define a macro which takes one parameter and expands to nothing?
Try if this works:
Code:
#define IDI_OK 1+100
If that works, then I'm still not sure if you can replace the 1 with IDOK. That is, I'm not sure the IDOK will get expanded. You may need to do something like
Code:
#define ADD(x, y) (x + y)
#define IDI_OK ADD(IDOK, 100)
Also see http://www.boost.org/doc/libs/1_45_0...doc/index.html
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by pascal_33
-when displaying rc resource symbol dialog, my identifiers IDI_OK, IDI_OK_DISABLED ... don't appear (read-only symbols checkbox checked)
Afraid your problem is... using resource editor. :) As a project coded strictly in plain text editor compiles and works perfectly fine.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
John E
You're doing something wrong somewhere else. Build and run this small program as a console app:-
Code:
#include <stdio.h>
#include <conio.h>
#define IDC_TEST 10
#define IDI_OK (IDC_TEST+100)
int main (int argc, char *argv[])
{
int n = IDI_OK;
printf ("Value of 'n' = %d", n);
getch ();
return 0;
}
You do not understand me. The problem relates to the compilation of rc file.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
D_Drmmr
Doesn't that define a macro which takes one parameter and expands to nothing?
Try if this works:
Code:
#define IDI_OK 1+100
If that works, then I'm still not sure if you can replace the 1 with IDOK. That is, I'm not sure the IDOK will get expanded. You may need to do something like
Code:
#define ADD(x, y) (x + y)
#define IDI_OK ADD(IDOK, 100)
Also see
http://www.boost.org/doc/libs/1_45_0...doc/index.html
I have tried a number of different ways and it just does not work. The problem is that in the .rc file the 'calculated' value is replaced by the preprocessor but the rc compiler does not recalculate it but accepts it as a string value.
#define IDI_X 100
#define IDI_Y (ID_X + 100)
...
IDI_X ICON ...
IDI_Y ICON ...
// in rc file gets replaced thus
100 ICON ...
(ID_X + 100) ICON
which is obviously wrong. It seems that rc will not recalculate expressions that are used as labels in resource scripts.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
Igor Vartanov
Afraid your problem is... using resource editor. :) As a project coded strictly in plain text editor compiles and works perfectly fine.
not only the resource editor, also the resource compiler (see my reply to D_Drmmr)
-
Re: Resources ID based on other resources ID
Don't know if it helps, but a popular problem with the VS resource editor is having two different versions of the .h file, one used when compiling the code, the other by the resource compiler. It's worth checking that you only have one copy of the dialog2.h file on your system.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
The problem is that in the .rc file the 'calculated' value is replaced by the preprocessor but the rc compiler does not recalculate it but accepts it as a string value.
#define IDI_X 100
#define IDI_Y (ID_X + 100)
...
IDI_X ICON ...
IDI_Y ICON ...
// in rc file gets replaced thus
100 ICON ...
(ID_X + 100) ICON
which is obviously wrong. It seems that rc will not recalculate expressions that are used as labels in resource scripts.
As I said earlier, #define is not in any way intelligent. It performs a simple (textual) search and replace, substituting (textually) one expression for another. Whether your compiler subsequently understands the substituted text will vary from one compiler to another. If the resource compiler doesn't understand the end result there's very little you can do (except to keep modifying your #defines until you arrive at something it does understand).
-
Re: Resources ID based on other resources ID
Shouldn't that be
Quote:
Originally Posted by
pascal_33
#define IDI_X 100
#define IDI_Y (IDI_X + 100)
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
alanjhd08
Don't know if it helps, but a popular problem with the VS resource editor is having two different versions of the .h file, one used when compiling the code, the other by the resource compiler. It's worth checking that you only have one copy of the dialog2.h file on your system.
There is only one dialog2.h file which is normally used as "read-only" file (i follow the microsoft link in my 1st post)
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
D_Drmmr
Shouldn't that be
it was only an example for the problem explanation ;)
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
John E
As I said earlier, #define is not in any way intelligent. It performs a simple (textual) search and replace, substituting (textually) one expression for another. Whether your compiler subsequently understands the substituted text will vary from one compiler to another. If the resource compiler doesn't understand the end result there's very little you can do (except to keep modifying your #defines until you arrive at something it does understand).
Yes, but following microsoft
http://msdn.microsoft.com/en-us/library/zakskay9.aspx
it should work !
but it does not :(
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
it was only an example for the problem explanation ;)
Quote:
Originally Posted by
pascal_33
Please post a minimal but complete example. There's no point in us guessing what code or project settings you have.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
One more thing.... that link is specific to Visual Studio 2010. Is that the compiler version you're using?
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
John E
One more thing.... that link is specific to Visual Studio 2010. Is that the compiler version you're using?
yes
but the same resource apply for all versions beginning from VS 2003 .net (there is other versions links in the page)
-
Re: Resources ID based on other resources ID
Dear pascal_33!
Is the 2.5 hours not enough to create what D_Drmmr has asked?
Quote:
Originally Posted by
D_Drmmr
Please post a minimal but complete example. There's no point in us guessing what code or project settings you have.
-
Re: Resources ID based on other resources ID
Hi,
Just checked this out myself, I see the same as you, i.e. expressions are not evaluated, so I looked at this note on the MSDN page you linked:
If a string or a numeric expression is expected, then the expression is not evaluated.
Since a resource ID can be either a string or a numeric expression, looks like that covers it.
-
Re: Resources ID based on other resources ID
Below is the rc file :
// Microsoft Visual C++ generated resource script.
//
#include "resource.h"
#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "dialogh2.h"
/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
// Français (France) resources
#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_FRA)
#pragma code_page(1252)
/////////////////////////////////////////////////////////////////////////////
//
// Icon
//
// Icon with lowest ID value placed first to ensure application icon
// remains consistent on all systems.
IDI_CANCEL ICON "dialogh_bmp/cancel2.ico"
1 ICON "dialogh_bmp/AppIcon.ico"
resource.h :
#define IDC_OK 1
dialogh2.h
#define IDI_CANCEL (IDC_OK+20000)
Creating the file "dialog2.h" is needed (following MSDN method) as read-only file (rc editor will not modify it in any way).
result : IDI_CANCEL should be calculated and get 20001 value. But no, it is not calculated and get a 0 value (cancel2.ico became the application icon)
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
alanjhd08
Hi,
Just checked this out myself, I see the same as you, i.e. expressions are not evaluated, so I looked at this note on the MSDN page you linked:
If a string or a numeric expression is expected, then the expression is not evaluated.
Since a resource ID can be either a string or a numeric expression, looks like that covers it.
yes, but there is also
The environment will correctly interpret these calculated symbols as long as:
* The calculated symbols are placed in a read-only symbols file.
* Your resource file contains resources to which these calculated symbols are already assigned.
* A numeric expression is expected.
All this does not appear very clear ....
-
Re: Resources ID based on other resources ID
pascal_33 - I don't wish to seem disrespectful but week after week on this site we find ourselves strugglling to solve problems for newbies who are only willing to release small snippets of code that only tell part of the story. As has been pointed out earlier though, resource IDs can either be numbers or strings. In your case it looks as if an expression like (IDC_OK+20000) will get interpreted as a string, rather than a number which is what you were hoping for. If that doesn't seem to explain your problem, the only other way to solve it would be for you to upload a small (but buildable) example program. Only then will anyone be able to figure out if you've done something wrong.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
#include "dialogh2.h"
Quote:
Originally Posted by
pascal_33
dialogh2.h
Quote:
Originally Posted by
pascal_33
Creating the file "dialog2.h" is needed (following MSDN method) as read-only file (rc editor will not modify it in any way).
The last reference uses a different name. Probably a typo in this here post, but just pointing out.
Quote:
Originally Posted by
pascal_33
Creating the file "dialog2.h" is needed (following MSDN method) as read-only file (rc editor will not modify it in any way).
Just want to point out that the "read-only" ONLY refers to the fact that neither VC nor the resource compiler will modify the .h files listed here. They are "readonly" in use.
This does NOT mean that the file in question needs to have the read-only file attribute set or requires some other special rights to actually make it unwritable.
-
Re: Resources ID based on other resources ID
As already said multiple times, please post a sample project.
It could have several reasons. For example, we don't know how you are using those resource IDs in your code.
-
Re: Resources ID based on other resources ID
@ John E and Marc G
I fully understand that you request a full project. But it is very complicated because my app rely on OWLNET libraries (https://sourceforge.net/apps/mediawi...itle=Main_Page)
Nobody want to take hours to install and compile these libraries.
alanjhd08 did a test, and he got same results as me.
My main question is how do you understand this MSDN page ?
http://msdn.microsoft.com/en-us/library/zakskay9.aspx
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
OReubens
The last reference uses a different name. Probably a typo in this here post, but just pointing out.
Just want to point out that the "read-only" ONLY refers to the fact that neither VC nor the resource compiler will modify the .h files listed here. They are "readonly" in use.
This does NOT mean that the file in question needs to have the read-only file attribute set or requires some other special rights to actually make it unwritable.
yes, typo error, sorry.
About read-only , of course it is not related to the file attributes but to the method used to add it from the "resource include" dialog : "Read-only symbol directives" input
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
@ John E and Marc G
I fully understand that you request a full project.
No, you didn't understand! :(
No one here needs your "full project"!
We need a very small project that reproduces the same problem you do have now! A very very small! (Or you mean that without linking to "OWLNET libraries" you could not reproduce your problem? :confused:
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
pascal_33
yes, typo error, sorry.
And where was this "typo": in the source code or in you posts? If the latter - then it one more reason to post a real working project instead of buggy "snippets" with a lot of "typos"
-
Re: Resources ID based on other resources ID
pascal_33 - we all understand how frustrating it is to be constantly asked for a small sample project when you think you've already given us all the relevant information but the simple truth is that we've all been down this road, many times before.
Just last week I spent over two days trying to solve a newbie's question - only for him to admit that he'd found the problem in a completely different part of the code that he hadn't told anyone about!
-
Re: Resources ID based on other resources ID
Ok... i'm preparing to post a sample project with the problem ;)
-
Re: Resources ID based on other resources ID
The operation is pretty clear tbh.
#define ID_ONE 1
#define ID_TWO (ID_ONE+1)
Then use ID_TWO as a resource ID in a resource file.
If the ID can be interpreted as either string or numeric, it'll parsed and processed but not evaluated and used as a string resource.
If the ID can be interpreted as numeric only, it'll be parsed, processed and evaluated numerically.
What this means:
Any "loadable" resource (icon, bitmap, dialog, html, manifest, ....) will be added as a string resource.
So for ID_TWO you will get a loadable resource named "1+1" rather than having numeric ID 2.
Any item which is an (numerically) identifiable part of a resource (resource string, an ID of a control on a dialog) will be evaluated to having the value 2.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
OReubens
The operation is pretty clear tbh.
#define ID_ONE 1
#define ID_TWO (ID_ONE+1)
Then use ID_TWO as a resource ID in a resource file.
If the ID can be interpreted as either string or numeric, it'll parsed and processed but not evaluated and used as a string resource.
If the ID can be interpreted as numeric only, it'll be parsed, processed and evaluated numerically.
What this means:
Any "loadable" resource (icon, bitmap, dialog, html, manifest, ....) will be added as a string resource.
So for ID_TWO you will get a loadable resource named "1+1" rather than having numeric ID 2.
Any item which is an (numerically) identifiable part of a resource (resource string, an ID of a control on a dialog) will be evaluated to having the value 2.
I agree, I think you're right.
But what can I do to get my icon ID dependent of buttons ID values (that was my first aim)
-
1 Attachment(s)
Re: Resources ID based on other resources ID
Here is a sample project
in the dialog box, you will see only 2 icons
But if you look at the test1.rc source code, you should expect 3 icons
Note that the rc editor give an error (on IDI_MYICON1), because it is unable to interpret it
-
Re: Resources ID based on other resources ID
1. You resource file compiles (I used VS2010) with the warning
Code:
warning RC2182: duplicate dialog control ID 2
2. If I comment out the line
Code:
// ICON IDI_MYICON1,IDR_MAINFRAME3,238,55,20,20
in your .rc file then the resource view becomes available and what you could see is the ID of the icon "..\test1\res\cancel2.ico" is "(1999+2)"
So it looks like the compiler is expecting a string or a numeric expression and therefore the expression is not evaluated. :sick:
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
VictorN
1. You resource file compiles (I used VS2010) with the warning
Code:
warning RC2182: duplicate dialog control ID 2
2. If I comment out the line
Code:
// ICON IDI_MYICON1,IDR_MAINFRAME3,238,55,20,20
in your .rc file then the resource view becomes available and what you could see is the ID of the icon "..\test1\res\cancel2.ico" is
"(1999+2)"
So it looks like the compiler is expecting
a string or a numeric expression and therefore the expression is not evaluated. :sick:
I'm afraid there is no solution to this problem :(
-
1 Attachment(s)
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
VictorN
So it looks like the compiler is expecting a string or a numeric expression and therefore the expression is not evaluated. :sick:
As I said, this works perfectly fine for both compilers, cl and rc:
Code:
#define IDD 100
#define IDC_BUTTON1 1000
#define IDC_BUTTON2 (IDOK + 20000)
#define IDC_BUTTON3 (IDOK + 30000)
-
1 Attachment(s)
Re: Resources ID based on other resources ID
Hi,
I've added an ICON to this, and in this case the expression is not evaluated, even when using rc from the command line (I have VS2008 installed)
From the MSDN note, it looks like this might work with older versions of the resource compiler, e.g. from VS .NET (2003)
Alan
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
alanjhd08
Hi,
I've added an ICON to this, and in this case the expression is not evaluated, even when using rc from the command line (I have VS2008 installed)
From the MSDN note, it looks like this might work with older versions of the resource compiler, e.g. from VS .NET (2003)
Alan
I agree with you. That don't work for loadables resources.
However, Igor Vartanov test is interesting showing it is possible to order (IDC1= IDC0+1, IDC2=IDC0+2 ...) a serie of controls which may be useful in some cases (loop treatments relying on controls ID).
-
Re: Resources ID based on other resources ID
there is a solution, even a dynamic one, but you'll need extra tools since the regular resource compiler won't work.
* Have a .h witht he button id's
* have a .h with the icon id's. You create/update it as part of your build, it needs to have regular '#define ID value' lines not '#define ID expression'.
* include both H's in the resource symbols
* make the .rc compilation dependant on the .h (may not be needed in 2010, explicit dependant is needed in VS2008).
While unusual, .h and .cpp files in your project can be the result of a tool generating those files. prior to the compiler picking them up for compilation. You sort of make your own "precompiler" in a way.
-
Re: Resources ID based on other resources ID
Quote:
Originally Posted by
OReubens
there is a solution, even a dynamic one, but you'll need extra tools since the regular resource compiler won't work.
* Have a .h witht he button id's
* have a .h with the icon id's. You create/update it as part of your build, it needs to have regular '#define ID value' lines not '#define ID expression'.
* include both H's in the resource symbols
* make the .rc compilation dependant on the .h (may not be needed in 2010, explicit dependant is needed in VS2008).
While unusual, .h and .cpp files in your project can be the result of a tool generating those files. prior to the compiler picking them up for compilation. You sort of make your own "precompiler" in a way.
Thanks. This seem a bit complicated but I'll think about it ;)