هذه المقالة متوفرة أيضا باللغة العربية، اقرأها هنا.
Contents of this article:
- Resource IDs (ResIDs)
- Resource Names
- Using Resource IDs
- Using Resource Names
This article teaches you how to refer to resources, to use Resource IDs, and to use Resource Names. It doesn’t talk about resource, how to define them, or how to load them. It assumes previous knowledge of ABCs of handling resources in an IDE like Microsoft Visual Studio or Microsoft Visual C++.
If you have a resource in your program, you can refer it in your code using one of 2 ways:
- Resource ID (ResID)
- Resource Name
Actually, choosing between the two ways depends on the way you name the resource while creating it.
Resource IDs (ResIDs)
If you want to refer the resource by its ID, just type the ID name in the ID field of the Resource Properties window (like IDB_BACKGROUND for a bitmap,) and Visual Studio will automatically adds your ID to resource.h (simply a #define statement) and assigns it a valid number to distinguish it from other resources. Later, you can refer the resource by the ID or by its given number. Figure 1 shows Resource Properties window in Microsoft Visual C++ and figure 2 shows the same window in Microsoft Visual Studio. Figures demonstrated in high-contrast mode.
On the other hand, if you want to refer the resource by a name (string,) just type the resource name enclosed with quotation marks in the ID field (like €œMyBackground€ for a bitmap) and you can later refer the resource by that name.
Using Resource IDs
Let’s take an example. Suppose we have a bitmap in our project resources. You can set this bitmap an ID like IDB_BACKGROUND using bitmap Resource Properties window and refer it using either that ID or the actual number.
// DO NOT FORGET to include "resource.h" LoadBitmap(hInstance, MAKEINTRESOURCE(IDB_BACKGROUND));
The next statement uses the actual ID number to load the same bitmap. It would success if, and only if, IDB_BACKGROUND equals to 101.// No need to include "resource.h" LoadBitmap ( hInstance , MAKEINTRESOURCE ( 101 ) );
Yes, you can look directly in resource.h and see the actual ID. Another way is to use the IntelliSense feature in Microsoft Visual Studio.
The previous code and the next one are very rude. You strictly should avoid hard-coding numbers in your code. If it’s mandatory, define them as constants.
The next statement is equivalent to the previous. However, it uses another way to represent the actual ID value. It surrounds the ID with two quotation marks and prefixes it with the number sign (#) to distinguish it from Resource Names that we will discuss shortly.// No need to include "resource.h" LoadBitmap ( hInstance , "#101" );
You will need to use MAKEINTRESOURCE macro to convert the resource ID to a string because resource functions accept only string pointers. MAKEINTRESOURCE macro is simply defined as following:#define MAKEINTRESOURCE(i) ((LPTSTR)((ULONG_PTR)((WORD)(i))))
If you like, you can convert the resource ID to a string without MAKEINTRESOURCE:LoadBitmap ( hInstance , (LPTSTR) IDB_BACKGROUND );
The last thing to mention about resource IDs is that you can write the ID value (e.g. 101) directly in the ID field of the Resource Properties window, and that will order Visual Studio or Visual C++ not to add the ID in resource.h and not to give it a name.
Using Resource Names
If you are sick of resource IDs and numbers, you can give your resource a name; a string that identifies the resource.
To specify the resource name, just type the name enclosed with quotation marks (like €œMyBackground€) in the ID field of the Resource Properties window.
After that you can refer the resource just by entering the resource name.LoadBitmap ( hInstance , "MyBackground" );
It’s worth to mention that you can’t use resource names that contain white space characters (like a space.) If you enter a name with a space in the Resource Properties window, the IDE would simply omit the spaces from the string.
This article wasn’t the much of how to refer resources. However, it established the fundamentals of locating resources and referring to them. In other articles, we’ll talk about resources, how to handle them, and how to use them in your application.
- Great Visual Studio 2010 Wallpapers, Logos, and Color Schemes
- Requesting Admin Approval at Application Start
- Consuming URL Shortening Services – X.co
- Consuming URL Shortening Services – Tweetburner (twurl)
- Clearing the Console Screen using API
- Why you receive a System.Security.SecurityException exception!
- Understanding Value Types and Reference Types
- A Short Speech about Unions