CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    23

    load wordpad icons into an imagelist

    I would like to use the bold, italic, undeline icons that wordpad uses. Is there a way to load these into an imagelist so I can use them in my toolbar?

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: load wordpad icons into an imagelist

    In fact, the toolbar images of wordpad are from a bitmap resource.
    As a quick solution, you can open wordpad.exe file in Visual Studio, export the bitmap resource, then import into your own resources.
    Further, create an imagelist based on that bitmap, and so on...
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    Join Date
    May 2009
    Posts
    23

    Re: load wordpad icons into an imagelist

    Thanks ovidiucucu

    I did what you said and was able to successfully export the bmp into my project. However, the image does not show up on my toolbar. Here is the code I am using to create the toolbar....

    Code:
    HWND CreateToolbar2( HWND hWndParent){
    
    	// Define some constants.
        const int ImageListID = 0;
        const int numButtons = 1;
        const DWORD buttonStyles = BTNS_AUTOSIZE;
        const int bitmapSize = 16;
    
        // Create the toolbar.
        // CCS_NOPARENTALIGN and CCS_NORESIZE prevent toolbar from aligning to the top
        HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
            WS_CHILD | TBSTYLE_WRAPABLE |TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_NODIVIDER,
            0, 0, 0, 0,
            hWndParent, NULL, hInst, NULL);
    
    	// check if toolbar was created
    	if (hWndToolbar == NULL)
        {
    		MessageBoxW( hWndParent, L"Toolbar2 not created", L"Error", MB_OK );
            return NULL;
        }
    
    	// Indent the first button so the button list starts to the right of the "font size" combobox
    	SendMessage(hWndToolbar, TB_SETINDENT, 60, NULL );
    
    	// create combobox
    	HWND hCombobox = CreateWindowEx( 0, WC_COMBOBOXEX, NULL,
    		WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWN,
    		0,0,55,22,
    		hWndToolbar, NULL, hInst, NULL); 
    
        // Create the imagelist.
        HIMAGELIST hImageList = ImageList_Create(
            bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
            ILC_COLOR16 | ILC_MASK,   // Ensures transparent background.
            numButtons, 0);
    
        // Set the image list.
        SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ImageListID, 
            (LPARAM)hImageList);
    
        // Load the image list that I obtained from wordpad.
        SendMessage(hWndToolbar, TB_LOADIMAGES, (WPARAM)IDB_BITMAP2, 
             (LPARAM)HINST_COMMCTRL);
    
    
        // Initialize button info and create the bold button.  The index is 1, which is the large B in the imagelist.
        TBBUTTON tbButtons[numButtons] = 
        {
            { MAKELONG(1, ImageListID), IDM_BOLD, TBSTATE_ENABLED, buttonStyles, {0}, 0, NULL }
        };
    
        // Add buttons.
        SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
        SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
    
        // Tell the toolbar to resize itself, and show it.
        SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
        ShowWindow(hWndToolbar, TRUE);
        return hWndToolbar;
    }

    I checked my .rc file and the bitmap shows up there, so I believe that I imported it correctly. It's also listed on my resource.h file as..

    Code:
    #define IDB_BITMAP2                     137
    The code runs fine, it's just that the image doesn't show up on the button. Am I doing something wrong? This code works fine if I use the IDB_STD_SMALL_COLOR imagelist. The only difference is that I use STD_PRINT instead of the index value to specify which image in the imagelist I want.

  4. #4
    Join Date
    Jul 2009
    Location
    Genova - Italy
    Posts
    38

    Re: load wordpad icons into an imagelist

    Hello, I am not an expert, but I will try to suggest you something.
    1. Are you sure that bitmapSize parameters in ImageList_Create() function equals to real dimensions of the bitmap (in pixel) ? If not, the image will not be loaded in your imagelist.
    2. Where do you add your bitmap in your ImageList? In my code, I am using something like this:
    Code:
    int ImageList_Add(          
        HIMAGELIST himl,         // A handle to the image list
        HBITMAP hbmImage,    // A handle to the bitmap that contains the image or images
        HBITMAP hbmMask      // A handle to the bitmap that contains the mask
    );
    3. In my code, I call these three functions in the following order:
    Code:
    ImageList_Create         // Image list creation
    LoadImage                   // Load a bitmap handler
    ImageList_Add             // Add my bitmap to my imagelist
    SendMessage(..., TB_SETIMAGELIST, ...)        // To associate my imagelist to my toolbar
    SendMessage(..., TB_ADDBUTTONS, ...)         // To add my buttons to my toolbar
    I hope this may be helpful to you

    Regards

    Fabrizio

  5. #5
    Join Date
    May 2009
    Posts
    23

    Re: load wordpad icons into an imagelist

    Hello motobizio

    Yes, the icons in my imagelist are 16x16.

    I'm setting up my toolbar in a slightly different way than you suggested. My toolbar is basically the code here on MSDN

    Except in my case, instead of using the IDB_STD_SMALL_COLOR, I imported an imagelist from wordpad as a resource.

    Another difference is that I can't specify which image from the bitmap image list I want to use in the TBBUTTON by calling MAKELONG(STD_FILENEW, ImageListID), my imported imagelist doesn't have a STD_FILENEW button. I specify by this by using MAKELONG(1, ImageListID). Where 1 is the index of the first image in the image list.

    so instead of calling....

    Code:
    ImageList_Create     
    LoadImage                  
    ImageList_Add             
    SendMessage(..., TB_SETIMAGELIST, ...)        
    SendMessage(..., TB_ADDBUTTONS, ...)
    I'm callling

    Code:
    ImageList_Create
    SendMessage(..., TB_SETIMAGELIST, ...) 
    SendMessage(..., TB_LOADIMAGES, ...)   
    TBBUTTON tbButtons[numButtons]
    SendMessage(..., TB_BUTTONSTRUCTSIZE, ...)        
    SendMessage(..., TB_ADDBUTTONS, ...)
    This worked perfectly fine when I was using the IDB_STD_SMALL_COLOR imagelist, but it does not work with my imported imagelist. I tried your method, but it was not successful. I'm guessing that I either imported the imagelist improperly or I'm calling the index of the images in the imagelist improperly. I'm pretty sure that I imported it properly though because I can see the entry in my .rc file and on my resource.h file. Also, the code runs fine, no compile errors, there just isn't any icon in the button.

  6. #6
    Join Date
    May 2009
    Posts
    23

    Re: load wordpad icons into an imagelist

    motobizio

    You were correct. The proper way to deal with an imported or custom bitmap as an imagelist is not to do what I did and call....

    Code:
    ImageList_Create
    SendMessage(..., TB_SETIMAGELIST, ...) 
    SendMessage(..., TB_LOADIMAGES, ...)   
    TBBUTTON tbButtons[numButtons]
    SendMessage(..., TB_BUTTONSTRUCTSIZE, ...)        
    SendMessage(..., TB_ADDBUTTONS, ...)
    The above only works for the image lists defined here. If you import the imagelist from an external file you have to do it like you said and call..

    Code:
    ImageList_Create     
    LoadImage                  
    ImageList_Add             
    SendMessage(..., TB_SETIMAGELIST, ...) 
    Create TBBUTTON      
    SendMessage(..., TB_BUTTONSTRUCTSIZE, ...)
    SendMessage(..., TB_ADDBUTTONS, ...)
    Here is my code

    Code:
    HWND CreateToolbar2( HWND hWndParent){
    
    	// Define some constants.
        const int ImageListID = 0;
        const int numButtons = 2;
        const DWORD buttonStyles = BTNS_AUTOSIZE;
        const int bitmapSize = 16;
    
        // Create the toolbar.
        // CCS_NOPARENTALIGN and CCS_NORESIZE prevent toolbar from aligning to the top
        HWND hWndToolbar = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, 
            WS_CHILD | TBSTYLE_WRAPABLE |TBSTYLE_FLAT | TBSTYLE_TRANSPARENT | CCS_NOPARENTALIGN | CCS_NORESIZE | CCS_NODIVIDER,
            0, 0, 0, 0,
            hWndParent, NULL, hInst, NULL);
    
    	// check if toolbar was created
    	if (hWndToolbar == NULL)
        {
    		MessageBoxW( hWndParent, L"Toolbar2 not created", L"Error", MB_OK );
            return NULL;
        }
    
    	// Indent the first button so the button list starts to the right of the "font size" combobox
    	SendMessage(hWndToolbar, TB_SETINDENT, 60, NULL );
    
    	// create combobox
    	HWND hCombobox = CreateWindowEx( 0, WC_COMBOBOXEX, NULL,
    		WS_CHILD | WS_VISIBLE | WS_TABSTOP | CBS_DROPDOWN,
    		0,0,55,22,
    		hWndToolbar, NULL, hInst, NULL); 
    
        // Create the imagelist.
        HIMAGELIST hImageList = ImageList_Create(
            bitmapSize, bitmapSize,   // Dimensions of individual bitmaps.
            ILC_COLOR16 | ILC_MASK,   // Ensures transparent background.
            numButtons, 0);
    
    	// Load the Bitmap and check the return to make sure it's been loaded
    	// Bitmap size is 112x16, so it contains seven 16x16 images
    	HANDLE hbmp = LoadImage(NULL, L"toolbar2.bmp", IMAGE_BITMAP, 112, 16, LR_LOADFROMFILE);
    	if (hbmp == NULL)
    			MessageBoxW( hWndParent, L"Imagelist for Toolbar2 not loaded", L"Error", MB_OK);
    
    	// Add the bitmap into the imagelist and check the return
    	// TODO  make a bitmap mask, I will ignore this for now
    	int ImageListCheck = ImageList_Add( hImageList, (HBITMAP)hbmp, NULL);
    	if (ImageListCheck == -1)
    			MessageBoxW( hWndParent, L"Failed to add bitmap to imagelist of toolbar2", L"Error", MB_OK);
    
        // Set the image list.
        SendMessage(hWndToolbar, TB_SETIMAGELIST, (WPARAM)ImageListID, (LPARAM)hImageList);
    
        // Initialize button info and create the bold button.  The index is 1, which is the large B in the imagelist.
        TBBUTTON tbButtons[numButtons] = 
        {
            { MAKELONG(0, ImageListID), IDM_BOLD, TBSTATE_ENABLED, buttonStyles, {0}, 0, NULL },
    		{ MAKELONG(1, ImageListID), IDM_ITALIC, TBSTATE_ENABLED, buttonStyles, {0}, 0, NULL }
        };
    
        // Add buttons.
        SendMessage(hWndToolbar, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);
        SendMessage(hWndToolbar, TB_ADDBUTTONS, (WPARAM)numButtons, (LPARAM)&tbButtons);
    
        // Tell the toolbar to resize itself, and show it.
        SendMessage(hWndToolbar, TB_AUTOSIZE, 0, 0); 
        ShowWindow(hWndToolbar, TRUE);
        return hWndToolbar;
    }
    Thanks again for the help!

  7. #7
    Join Date
    Jul 2009
    Location
    Genova - Italy
    Posts
    38

    Re: load wordpad icons into an imagelist

    you are wellcome!! :-D

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured