April 29, 2024, 05:24:05 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


Create toolbar with individual button bitmaps/icons

Started by Sam, February 01, 2014, 07:37:09 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Sam

Greetings all. Don't know if this is old hat, but I thought I would post it in case someone might find it useful.

This code creates a toolbar using individual button size bitmaps instead of a strip of bitmaps. I find this useful because there are a lot of nice icon/bitmaps free all over the internet that come in square button sizes. It uses SDK but it's pretty straight forward.
Of course you'll need to supply your own bitmaps/icons.
Also, the image list should be deleted at the end of the program with ImageList_Destroy(g_hImg).

sub CreateToolbar(), LONG

  uint hIcon[5]
  uint hTB
  int rslt
  int IcnSz = 32 ' The size of the icon/bitmap image. Change if you use a different size.
  int numImgs = 5 ' The number of images in the image list.
  int CanGrowBy = 1 ' The number of additional images the list may grow by.

  TBBUTTON tbButtons[5]
 
  def sTmp[5]: string
  sTmp = "Text", "Box", "Line", "Print", "Exit"
 
  '-------------------------------------------------------------------------------------------------
  ' Create an image list. g_hImg is a global uint.
  '-------------------------------------------------------------------------------------------------
  g_hImg = ImageList_Create(IcnSz, IcnSz, ILC_COLOR16|ILC_MASK, numImgs, CanGrowBy)

  '-------------------------------------------------------------------------------------------------
  ' Load the toolbar button images from disk.
  '-------------------------------------------------------------------------------------------------
$ifdef USEICON

hIcon[0] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\HOMER.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[1] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\MARGE.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[2] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\BART.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[3] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\LISA.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[4] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\MAGGIE.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)

  rslt = ImageList_AddIcon(g_hImg, hIcon[0])
  rslt = ImageList_AddIcon(g_hImg, hIcon[1])
  rslt = ImageList_AddIcon(g_hImg, hIcon[2])
  rslt = ImageList_AddIcon(g_hImg, hIcon[3])
  rslt = ImageList_AddIcon(g_hImg, hIcon[4])

$else

  hIcon[0] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\TEXT TOOL.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[1] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\RESTANGLE.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[2] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\LINE.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[3] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\PRINT.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[4] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\EXIT.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)

  rslt = ImageList_AddMasked(g_hImg, hIcon[0], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[1], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[2], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[3], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[4], RGB(0xff, 0xff, 0xff))

$endif

  '-------------------------------------------------------------------------------------------------
  ' Create the toobar. w1 is the window or dialog for which the toolbar is being created.
  '-------------------------------------------------------------------------------------------------
  hTB = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD, 0, 0, 0, 0, w1.hWnd, NULL, 0, NULL)
 
  '-------------------------------------------------------------------------------------------------
  ' Attach the image list to the toolbar.
  '-------------------------------------------------------------------------------------------------
  rslt = _SendMessage(hTB, TB_SETIMAGELIST, 0, g_hImg)

  '-------------------------------------------------------------------------------------------------
  ' Create and populate a toolbar button structure -> TBBUTTON.
  '-------------------------------------------------------------------------------------------------
int btn  
for btn = 0 to 4
tbButtons[btn].iBitmap = btn
tbButtons[btn].idCommand = 3000 + btn
tbButtons[btn].fsState = TBSTATE_ENABLED
tbButtons[btn].fsStyle = BTNS_AUTOSIZE
tbButtons[btn].dwData = 0
tbButtons[btn]._iString = &sTmp[btn]
next btn
 
  '-------------------------------------------------------------------------------------------------
  ' Add the buttons to the toolbar.
  '-------------------------------------------------------------------------------------------------
  _SendMessage(hTB, TB_BUTTONSTRUCTSIZE, SIZEOF(TBBUTTON), 0)
  _SendMessage(hTB, TB_ADDBUTTONS, 5, &tbButtons)

  '-------------------------------------------------------------------------------------------------
  ' Resize the toolbar and show it.
  '-------------------------------------------------------------------------------------------------
  _SendMessage(hTB, TB_AUTOSIZE, 0, 0)
  _ShowWindow(hTB, SW_NORMAL)

  return hTB
ENDsub

LarryMc

tried to compile and it wouldn't as posted.
Where are the required constants, functions that are needed declared?
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

Sam

Quote from: LarryMc on February 01, 2014, 08:29:54 PM
tried to compile and it wouldn't as posted.
Where are the required constants, functions that are needed declared?

That's because it's just a subroutine. This will compile, but to run it, you need to supply the bitmaps. The USEICON constant is so you can use either icon images or bitmap images.
Sorry. I should have listed a compilable  example.

$include "windowssdk.inc"
$include "commctrl.inc"

$DEFINE USEICON
def w1: window

uint g_hImg

openwindow w1, 100, 100, 600, 300, @MINBOX|@MAXBOX|@SIZE|@VSCROLL, 0, "Sam's Window", &WndProc
waituntil w1 = 0
end

sub WndProc(), int
select @MESSAGE
case WM_CREATE
'--------------------------------------------------------------------------------------
' Create a toolbar.
'--------------------------------------------------------------------------------------
CreateToolbar()

case @IDCLOSEWINDOW
Imagelist_Destroy(g_hImg)
closewindow w1
default
endselect

return 0
endsub

sub CreateToolbar(), LONG

  uint hIcon[5]
  uint hTB
int rslt
int IcnSz = 32 ' The size of the icon/bitmap image. Change if you use a different size.
int numImgs = 5 ' The number of images in the image list.
int CanGrowBy = 1 ' The number of additional images the list may grow by.

  TBBUTTON tbButtons[5]
 
  def sTmp[5]: string
  sTmp = "Text", "Box", "Line", "Print", "Exit"
 
  '-------------------------------------------------------------------------------------------------
  ' Create an image list. g_hImg is a global uint.
  '-------------------------------------------------------------------------------------------------
  g_hImg = ImageList_Create(IcnSz, IcnSz, ILC_COLOR16|ILC_MASK, numImgs, CanGrowBy)

  '-------------------------------------------------------------------------------------------------
  ' Load the toolbar button images from disk.
  '-------------------------------------------------------------------------------------------------
$ifdef USEICON

hIcon[0] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\HOMER.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[1] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\MARGE.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[2] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\BART.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[3] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\LISA.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)
hIcon[4] = _LoadImage(NULL, "C:\\INCONS\\TOOLBAR_ICONS\\SIMPICON\\MAGGIE.ICO", IMAGE_ICON, IcnSz, IcnSz, LR_LOADFROMFILE)

  rslt = ImageList_AddIcon(g_hImg, hIcon[0])
  rslt = ImageList_AddIcon(g_hImg, hIcon[1])
  rslt = ImageList_AddIcon(g_hImg, hIcon[2])
  rslt = ImageList_AddIcon(g_hImg, hIcon[3])
  rslt = ImageList_AddIcon(g_hImg, hIcon[4])

$else

  hIcon[0] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\TEXT TOOL.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[1] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\RESTANGLE.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[2] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\LINE.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[3] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\PRINT.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)
  hIcon[4] = _LoadImage(NULL, "C:\\ICONS\\NEW_ICONS\\BMP\\32X32\\EXIT.BMP", IMAGE_BITMAP, IcnSz, IcnSz, LR_LOADFROMFILE)

  rslt = ImageList_AddMasked(g_hImg, hIcon[0], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[1], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[2], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[3], RGB(0xff, 0xff, 0xff))
  rslt = ImageList_AddMasked(g_hImg, hIcon[4], RGB(0xff, 0xff, 0xff))

$endif

  '-------------------------------------------------------------------------------------------------
  ' Create the toobar. w1 is the window or dialog for which the toolbar is being created.
  '-------------------------------------------------------------------------------------------------
  hTB = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD, 0, 0, 0, 0, w1.hWnd, NULL, 0, NULL)
 
  '-------------------------------------------------------------------------------------------------
  ' Attach the image list to the toolbar.
  '-------------------------------------------------------------------------------------------------
  rslt = _SendMessage(hTB, TB_SETIMAGELIST, 0, g_hImg)

  '-------------------------------------------------------------------------------------------------
  ' Create and populate a toolbar button structure -> TBBUTTON.
  '-------------------------------------------------------------------------------------------------
int btn  
for btn = 0 to 4
tbButtons[btn].iBitmap = btn
tbButtons[btn].idCommand = 3000 + btn
tbButtons[btn].fsState = TBSTATE_ENABLED
tbButtons[btn].fsStyle = BTNS_AUTOSIZE
tbButtons[btn].dwData = 0
tbButtons[btn]._iString = &sTmp[btn]
next btn
 
  '-------------------------------------------------------------------------------------------------
  ' Add the buttons to the toolbar.
  '-------------------------------------------------------------------------------------------------
  _SendMessage(hTB, TB_BUTTONSTRUCTSIZE, SIZEOF(TBBUTTON), 0)
  _SendMessage(hTB, TB_ADDBUTTONS, 5, &tbButtons)

  '-------------------------------------------------------------------------------------------------
  ' Resize the toolbar and show it.
  '-------------------------------------------------------------------------------------------------
  _SendMessage(hTB, TB_AUTOSIZE, 0, 0)
  _ShowWindow(hTB, SW_NORMAL)

  return hTB
ENDsub








LarryMc

Quote from: tennisbum on February 01, 2014, 09:43:50 PM
Sorry. I should have listed a compilable  example.
Not a problem.
Both you and I knew what needed to be done.

I only mentioned it because it is not as obvious to some others.
Thanks for updating.
LarryMc
Larry McCaughn :)
Author of IWB+, Custom Button Designer library, Custom Chart Designer library, Snippet Manager, IWGrid control library, LM_Image control library

AdrianFox

Many thanks also for posting this extremely useful example.   :)
Adrian Fox