Bitmap files added in .rc not listed in resource.h
I added a large number of bitmap files to my MFC project in resource view (Visual Studio). However, the resource.h file 开发者_如何学编程does not list any of these files. I would want them to be listed with exactly the same name as they are added in .rc bitmap list (say the name is xxx) I want it listed as #define IDB_xxx If I try to click on the bitmap in the .rc list and import it, it gets listed as IDB_BITMAP1. Owing to the large number of files it is not feasible for me to manually rename them for use in my code.
If you just drag the file onto the Resource View, it just open the bitmaps for editing.
So you have to Import them, then you will get them referred to as IDB_BITMAPn
To clean that up close the resource view then open the .rc file in View Code mode, find the section with the new bitmaps and find the new bitmaps like this:
IDB_BITMAP4 BITMAP "C:\\picutre1.bmp"
IDB_BITMAP5 BITMAP "C:\\picutre2.bmp"
IDB_BITMAP6 BITMAP "C:\\picutre3.bmp"
So now write a regex to replace the file name into the define, but also we will need to replace the numbers section in the resource.h also.
To make this easiest, make a copy of the above section.
So the first part can be done with this visual studio replace regex, which you'll want to run over the selected area
^:i{:b+}BITMAP{:b+}"{.*\\}{.*}.bmp"
IDB_\4\1BITMAP\2"\3\4.bmp"
this will make it look like this
IDB_picutre1 BITMAP "C:\\picutre1.bmp"
IDB_picutre2 BITMAP "C:\\picutre2.bmp"
IDB_picutre3 BITMAP "C:\\picutre3.bmp"
you then want to do a vertical selection (hold the ATL key while mousing down) over all the IDB_pictureX words then press to upper (C# keymap CTRL+SHIFT+U)
Now the to map the NEW IDB_X to the old values. We could use sed to do the work, or make a remapping file.
The latter is done by using the copy of the original .rc section, and apply this replace regex over the selected area.
^{:i}:b+BITMAP:b+".*\\{.*}.bmp"
#define IDB_\2\t\t\1
giving you this block of code looking like below:
#define IDB_picutre1 IDB_BITMAP4
#define IDB_picutre2 IDB_BITMAP5
#define IDB_picutre3 IDB_BITMAP6
which you can do the same ALT+select to change to upper case the defines, giving this code which you can place into a header file somewhere:
#define IDB_PICUTRE1 IDB_BITMAP4
#define IDB_PICUTRE2 IDB_BITMAP5
#define IDB_PICUTRE3 IDB_BITMAP6
now a more clean solution would be to build a sed command file, and run that against your resource.h which you can do like this
^{:i}:b+BITMAP:b+".*\\{.*}.bmp"
s/\1/IDB_\2/
giving:
s/IDB_BITMAP4/IDB_picutre1/
s/IDB_BITMAP5/IDB_picutre2/
s/IDB_BITMAP6/IDB_picutre3/
copy that output into a file, and use that with the sed -F flag
精彩评论