Sign in to follow this  
ChristopherO

Adding icons to a Win32 API toolbar (C++).

Recommended Posts

I have a toolbar in an application I am creating which uses the standard icons. What I want to do is use some ‘.ico’ files of my own, but I am not sure how I should put them in the resource file and I am not sure how to reference them from the code. All of the code is C++, and I am using Visual C++ Express Edition with the Win32 API (not MFC). I know very little about the ‘.rc’ format, but I’ve managed to add an application icon and a menu for the program just fine. I am not sure if the icons for the toolbar should be listed in the same way, though. I have seen several examples of people using bitmaps which contain multiple icons, and it seems as though the bitmap is broken up into smaller images as it is loaded, but I have not found a simple example of how to use individual icon files, though I have seen people say it is possible. Thanks in advance for any help, pointers, etc.

Share this post


Link to post
Share on other sites
About Resource Files - a link to everything you need to know about resource files.

The images on toolbar buttons are actually bitmaps. Adding bitmaps to an RC is very similar to adding an icon. Here is the syntax. To reference it in your code you need to give the bitmap a resource ID (which is already included in the syntax). To do this you generally have a resource.h which defines your IDs and then you use these defines in the .rc file itself and the source files of your application that need that resource. For example


// resource.h
#define IDB_MYBITMAP 2000




// resource.rc
#include "resource.h"

IDB_MYBITMAP BITMAP "mybitmap.bmp"




// Somefile.cpp
#include "resource.h"

HBITMAP LoadMyBitmap() {
HBITMAP hbmp = LoadBitmap(GetModuleHandle(0), MAKEINTRESOURCE(IDB_BITMAP));
return hbmp;
}



You are correct that it is possible to use a single bitmap to reference all of your buttons, but it is not the only way. It's very easy to do it that way but it is also possible to add buttons individually using TB_ADDBITMAP and TB_ADDBUTTONS. Note, that if you go with the one-bitmap-one-toolbar-button approach you cannot also use the one-bitmap-many-toolbar-buttons approach.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this