CBGraham
CBGraham

Reputation: 1368

How do I get a Resource ID edit in MFC editor to propagate to code?

I make a button in the resource editor in an MFC program.
I give it an ID.
I use the ID in code with CWnd *tLabel = GetDlgItem(IDC_CHANGETWO);
I am pleased.

I go back to editor
I change the ID
Code does not acknowledge new name, even with rebuild
I change some flag (tabstop)
Code still does not care

In order to get an ID name change to propagate to the code, I have to change the ID AND any other field in the same save. Only then does it realize it is dirty and update the resource table.

This is 100% reproducible on mine and my students' computers. This workaround has kept the class from stalling, but... what the heck is this?

Visual Studio Enterprise 2015
Version 14.0.23107.0 D14REL

Upvotes: 1

Views: 7554

Answers (3)

Andrew Truckle
Andrew Truckle

Reputation: 19207

Here is a second possible way to handle the specific issue of IDC_STATIC. Your question should be modified to include this.

I have started a temporary dialogue project and selected the default static text:

Default static text

As you can see, it is labelled IDC_STATIC. I now directly change the ID value in the resource editor itself and hit the enter key:

Rename property

Then I save it (this updates the resource.h file). Then, this is what we see if I use an application called ResOrg:

Using ResOrg

As you can see, the resource.h file is correct. It is at this point you now add your variables.

Interestingly, the IDE does not actually allow you to add variables to IDC_STATIC objects anyway. But suppose you have two if them:

2 Static objects

You can still rename each one, save the file, re-build, and view your resource file:

Rename 2 other static controls

As you can see, it is all still good. It is at this point that you can add variables and event handlers now. And it is in this context where VisualAssist would be used to rename variables/ID's used throughout your application.

I was not sure to edit my existing answer with this additional information. Perhaps that might be better?

Upvotes: 0

Ajay
Ajay

Reputation: 18451

While the answer given is good, it doesn't show what happens behind the scene when you update a resource ID.

  • All resource IDs go into one header: resource.h
  • This file is read by C++ compiler, as well as by resource compiler (the .RC file.)
  • Using resource editor, you just type an ID, and the resource-editor would simply create an entry in resource.h

This header looks like:

//{{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by MFCApplication1.rc
//
#define IDR_MAINFRAME                   128
#define IDM_ABOUTBOX                    0x0010
#define IDD_ABOUTBOX                    100
#define IDS_ABOUTBOX                    101
#define IDD_MFCAPPLICATION1_DIALOG              102

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS

#define _APS_NEXT_RESOURCE_VALUE    129
#define _APS_NEXT_CONTROL_VALUE     1000
#define _APS_NEXT_SYMED_VALUE       101
#define _APS_NEXT_COMMAND_VALUE     32771
#endif
#endif

So, when you add a new control on dialog (or any other form), and name it IDC_DOWNLOAD_NOW, and save the resource, this header will get updated like:

#define IDR_MAINFRAME                   128
#define IDC_DOWNLOAD_NOW                1000

Also, internal variable (like _APS_NEXT_CONTROL_VALUE gets updated).

Well.. when you have same symbol in other dialog, VS will simply use the same macro with same value. You add another button on third dialog with same name, it doesn't modify resource.h either.

But... when you modify control in these 3 dialogs to name is like IDC_DOWNLOAD_LATER, only .RC and resource.h get updated. Old value, and old usages still exist. You wont get any compiler error (since macros are there!).

#define IDR_MAINFRAME                   128
#define IDC_DOWNLOAD_NOW                1000
#define IDC_DOWNLOAD_LATER              1001

In another case, when you remove the button (from any of 3 dialog boxes), program will compile fine, but GetDlgItem, DDX_Control etc will fail at runtime (Since IDC_DOWNLOAD_NOW doesn't exist in current dialog!)

Now think what would happen if you mass rename the symbols using any tool - boom - all resources now have IDC_DOWNLOAD_LATER!

You should use Resource Symbols dialog to find what resource IDs are used by which dialog. From here you can rename and delete also. enter image description here enter image description here

Upvotes: 1

Andrew Truckle
Andrew Truckle

Reputation: 19207

If you change the name of a resource control it will not propagate that throughout your source code as you have already learned.

Ideally you need to use something like VisualAssist: http://www.wholetomato.com/

It allows you to rename your resource ID values

What you need to use is refactoring. If you open your resource file as a text file in the IDE (as opposed to the resource editor) and locate your control, you should be able to right-click it and choose Refactor (VA):

Renaming a resource ID

Then select Rename. It should preview the changes in the various source code files:

Refactor preview

You can use the same procedure for renaming variables and method declarations.

Upvotes: 3

Related Questions