Редагувати

Поділитися через


How to: Create Resources (C++)

You can create resources for your project by:

  • Using a resource script file.

    Note

    This step is necessary before you add resources.

  • Adding resources to your project and using the Resource View.

  • Using a resource template to create customized resources.

Use Resource Script Files

Before you create and add new resources to your project, you must first create a resource script (.rc) file.

Note

You can only add a resource script file to an existing project loaded into the Visual Studio IDE. You can't create a standalone resource script outside the project, though resource template (.rct) files can be created anytime.

To create a resource script file

  1. Put focus on your existing project folder in Solution Explorer, for example, MyProject.

    Note

    Don't confuse the project folder with the solution folder in Solution Explorer. If you put focus on the Solution folder, you won't have the same Add New Item choices.

  2. In the menu, go to Project > Add New Item.

  3. Select the Visual C++ folder and choose Resource File (.rc) in the right pane.

  4. Provide a name for your resource script file in the Name text box and select Open.

To open a resource script file

You can view resources in a resource script file without having a project open. The script file opens in a document window as opposed to the Resource View.

Note

Some commands are only available if the file is opened standalone, meaning outside of a project without first loading the project. For example, to use the Save As command and save a file with a different format or file name, the file must be opened standalone.

  • To open a resource script file outside of a project, in the menu, go to File > Open, and choose File. Navigate to the resource script file, highlight the file, and choose Open.

    Note

    There may be times when you want to view the contents of your project's resource script file without using the resource editors to open a resource. For example, you may want to search for a string across all dialog boxes in the resource file without having to open each one separately. You can easily open the resource file in text format to view all the resources it contains and complete global operations supported by the text editor.

    To open a resource script file in text format, use the drop-down arrow on the right side of the Open button in the above step and choose Open With. Select Source Code (Text) Editor and from the Open As drop-down list, select Text and the resource opens in the Source Code editor.

  • To open multiple resource scripts follow the same step above for each file you want to open, for example, Source1.rc and Source2.rc. Then, when both .rc files are open in separate documents windows, either use the Window menu or right-click one of the files, and choose New Horizontal Tab Group or New Vertical Tab Group. The windows are now tiled so you can view them simultaneously.

Tip

You can open resource script files by right-clicking the .rc file in Solution Explorer, selecting Open with and choosing Source Code (Text) Editor.

When you build a Microsoft Foundation Class (MFC) application for Windows using the MFC application wizard, the wizard generates a basic set of files including a resource script (.rc) file) that contains the core features of the MFC. However, these MFC-specific features aren't available when editing an .rc file for Windows applications not based on MFC. This includes code wizards, menu prompt strings, list contents for combo box controls, and ActiveX control hosting.

  • To add MFC support, with the resource script file open, in Resource View, highlight the resources folder (for example, MFC.rc). Then in the Properties window, set MFC Mode to True.

    Note

    In addition to setting MFC Mode, the .rc file must be part of an MFC project. Only setting MFC Mode to True on an .rc file in a Win32 project won't give you MFC features.

Create Resources

You can create a resource as a new default resource meaning a resource that isn't based on a template, or as a resource patterned after a template.

Use the Resource View window to display resource files included in your projects. Expanding the top folder, for example, Project1.rc, shows the resource types within that file. Expand each resource type to show the individual resources of that type.

Tip

To open the Resource View window, go to menu View > Other Windows > Resource View or press Ctrl+Shift+E.

You can also use right-click on the Resource View window to launch a shortcut menu of commands, or double-click the title bar to dock and undock the window. Right-click the title bar for commands that control the behavior of the window. For more information, see Windows Management.

The Resource View windows includes the Add Resource dialog box with the following properties to add resources to a C++ Windows desktop application project:

Property Description
Resource Type Specify the kind of resource you want to create.

You can expand the cursor and dialog box resource categories to reveal additional resources, which are located in ..\Microsoft Visual Studio <version>\VC\VCResourceTemplates\<LCID>\mfc.rct. If you need to add .rct files, either put them here or specify another include path. Resources shown at the top level in the tree control are the default resources provided by Visual Studio. Resources in .rct files appear at the second level under the appropriate category. There's no preset limit to the number of .rct files you can add.

New Create a resource based on the type selected in the Resource Type box and open the resource in the appropriate editor.

For example, if you create a dialog resource, it opens the resource in the Dialog Editor.
Import Open the Import dialog box to navigate to the resource you want to import into your current project.

You can import a bitmap, icon, cursor, HTML, sound (.WAV), or custom resource file.
Custom Open the New Custom Resource dialog box to create a custom resource.

Also includes a Resource Type property that provides a text box for you to enter the name of the custom resource type. Visual C++ automatically capitalizes the name when you exit. Custom resources are only edited in the Binary Editor.

When you create a new resource, Visual C++ assigns a unique name to it, for example, IDD_Dialog1. You can customize this resource ID by editing the resource properties either in the associated resource editor or in the Properties window.

Note

Don't specify a resource name or ID that is reserved by Visual Studio. Reserved names are DESIGNINFO, HWB, and TEXTINCLUDE, and the reserved ID is 255.

To create a resource

  • In Resource View, select your .rc file, then use Edit > Add Resource and choose the type of resource to add to your project.

    Tip

    You can also right-click the .rc file in Resource View and choose Add Resource from the shortcut menu.

  • In Solution Explorer, right-click the project folder, select Add > Add Resource and choose the type of resource to add to your project.

    Note

    If you don't already have an .rc file in your project, this step will create one. You can then repeat this step to add specific resource types to the new .rc file.

  • In Class View, right-click the project, select Add > Add Resource and choose the type of resource to add to your project.

  • Use the menu Project > Add Resource.

Use Resource Templates

A resource template is a customized resource that you've saved as an .rct file. A resource template then serves as a starting point for creating resources. Resource templates save time in developing additional resources or groups of resources that share features, such as standard controls or repeated elements. For example, if you want to include a help button with a company logo icon in several dialog boxes, create a new dialog box template and customize it with the help button and the logo.

After customizing a resource template, save your changes in the template folder or the location specified in the include path, so that the new resource template will appear under its resource type in the Add Resource dialog box. You can now use the new resource template as often as needed.

Note

The resource editor automatically provides a unique resource ID. You can revise the resource properties as needed.

Note

Place language-specific template files in subdirectories of the main template directory. For example, English-only template files go in ..\<resource template directory>\1033.

Visual Studio searches for new .rct files in \Program Files\Microsoft Visual Studio <version>\VC\VCResourceTemplates, \Program Files\Microsoft Visual Studio <version>\VC\VCResourceTemplates\<LCID> (such as an LCID of 1033 for English), or anywhere on the include path. If you prefer to store your .rct files in another location, you must add the location to the include path.

To create and use a resource template

  1. In Solution Explorer, right-click your project and select Add > Add New Item.

  2. In the Templates: pane, select Resource Template File (.rct).

  3. Provide a name and location for your new .rct file and choose Open.

    The new .rct file is added to your project and appears in Solution Explorer under the Resources folder.

  4. Double-click the .rct file to open it in a document window. To add resources, right-click the file in the document window and choose Add Resource.

    You can customize your added resources and save the .rct file.

  5. In the Resource View pane, right-click the .rc file and choose Add Resource.

  6. Select the plus sign (+) next to a resource to expand the resource node and view the templates available for that resource.

  7. Double-click the template you want to use.

    You can modify the added resource as needed in its resource editor.

To convert an existing resource file to a template

With the resource script file open, in the menu, go to File > Save <filename> As. Specify a location and choose OK.

Requirements

Win32

See also

Resource Files
How to: Manage Resources
How to: Include Resources at Compile Time