С++ Microsoft MFC TreeView Icons - c++

I have written a code of the TreeView Control by a blog article. I am trying to add icons to list items. But icons are no rendered.
I have a next code:
void CLeftView::OnInitialUpdate()
{
CTreeView::OnInitialUpdate();
// TODO: Add items by GetTreeCtrl().
HICON hi = NULL;
hi = AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_ICON1));
if (hi != NULL)
{
MessageBox(NULL, L"resource1");
}
else MessageBox(NULL, L"Not resource1");
HICON lo = NULL;
lo = AfxGetApp()->LoadIcon(MAKEINTRESOURCE(IDI_ICON2));
if (lo != NULL)
{
MessageBox(NULL, L"resource2");
}
else MessageBox(NULL, L"Not resource2");
CImageList m_tree_imglist;
CTreeCtrl & tc = CTreeView::GetTreeCtrl();
m_tree_imglist.Create(16, 16, ILC_COLOR32 | ILC_MASK, 0, 2);
m_tree_imglist.Add(hi);
m_tree_imglist.Add(lo);
tc.SetImageList(&m_tree_imglist, TVSIL_NORMAL);
HTREEITEM hItem;
hItem = tc.InsertItem(L"Fonts", 0, 0, TVI_ROOT);
tc.InsertItem(L"Arial", 0, 0, hItem);
tc.InsertItem(L"Times", 0, 0, hItem);
tc.Expand(hItem, TVE_EXPAND);
}
Icons have added to resource's files. There's do I have a bug? I have message boxes with a next text: "resource1", "resource2".

m_tree_imglist is declared on stack, this image list is destroyed after OnInitialUpdate exits, so CTreeCtrl no longer has an image list.
Image list should be declared as class member instead, so that it remains valid as long as CTreeCtrl needs it. Note the m_ prefix is usually used in MFC to indicate "class member".
class CLeftView : public CTreeView
{
CImageList m_tree_imglist;
...
};
void CLeftView::OnInitialUpdate()
{
...
//CImageList m_tree_imglist; <- remove
tc.SetImageList(&m_tree_imglist, TVSIL_NORMAL);
}

Related

HtmlHelp MS API search string

Does any one in 2017 knows how to implement calling HtmlHelp function that will open .chm file with "Search" pane and query string in it's listbox and will be able to execute this query?
I try following:
HH_FTS_QUERY query;
query.cbStruct = sizeof(HH_FTS_QUERY);
query.fUniCodeStrings = TRUE;
query.pszSearchQuery = szSearchStr;
query.iProximity = HH_FTS_DEFAULT_PROXIMITY;
query.fStemmedSearch = TRUE;
query.fTitleOnly = TRUE;
query.fExecute = TRUE;
query.pszWindow = NULL;
HWND hHelpWnd = ::HtmlHelp(m_hWnd, szFile, HH_DISPLAY_SEARCH, (DWORD_PTR)&query);
but the query in query.pszSearchQuery won't be executed. I have opened .chm file with query.pszSearchQuery in "Search" pane's listbox on my screen, but I have to click "List Topics" myself to show results of search.
With the help of HTMLHelp API - VBA, VB6 and VB2003, i will try to reply.
I believe there no API function to list topics in vc++. You have send button click event to launched help window.
LRESULT OnSearch(WORD /*wNotifyCode*/, WORD wID, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
TCHAR szBuf[128];
GetDlgItem(IDC_EDIT1).GetWindowText(szBuf, sizeof(szBuf));
if (!_tcslen(szBuf))
return 0;
//
// First, invoke HtmlHelp with HH_DISPLAY_SEARCH.
// It doesn't execute search, but it's need for showing 'search' tab.
//
HH_FTS_QUERY fq;
ZeroMemory(&fq, sizeof(fq));
fq.cbStruct = sizeof(fq);
fq.fUniCodeStrings = FALSE;
fq.pszSearchQuery = (LPCTSTR)szBuf;
fq.iProximity = HH_FTS_DEFAULT_PROXIMITY;
fq.fStemmedSearch = FALSE;
fq.fTitleOnly = FALSE;
fq.fExecute = FALSE;
fq.pszWindow = NULL;
HWND hwndHelp = ::HtmlHelp(NULL, _T("realplay.chm"), HH_DISPLAY_SEARCH, (DWORD)&fq);
//
// Find query combobox and set query text.
//
HWND hPaneWnd, hPaneLast, hTabWnd, hDlgWnd, hCtlWnd;
hPaneWnd = FindWindowEx(hwndHelp, NULL, _T("HH Child"), NULL);
for (;;) // last HH Child
{
hPaneLast = FindWindowEx(hwndHelp, hPaneWnd, _T("HH Child"), NULL); // last HH Child
if (!hPaneLast)
break;
hPaneWnd = hPaneLast;
}
ATLTRACE("hPaneWnd == %x", hPaneWnd);
hCtlWnd = FindWindowEx(hPaneWnd, NULL, _T("Button"), NULL); // skip Tab Control
//
// There are two types of interfaces:
//
// 1.
// Window hierarchy:
// + Main window
// + HH Child
// + Browser ...
// + HH Child <- second "HH Child" window
// - Edit <- we have to fill this edit
// - Buttons <- and press this buttons
// ...
if (hCtlWnd)
{
hCtlWnd = FindWindowEx(hPaneWnd, NULL, _T("Edit"), NULL); // skip Tab Control
// Set window text
ATLASSERT(hCtlWnd != NULL);
::SendMessage(hCtlWnd, WM_SETTEXT, 0, (LPARAM)szBuf); // fill it by our query
::SendMessage(hwndHelp, WM_COMMAND, 0xbc7, 0); // 0x3ee -- 'List Topics' button, it runs search
if (::SendMessage(GetDlgItem(IDC_CHECK1), BM_GETCHECK, 0, 0) == BST_CHECKED)
::SendMessage(hwndHelp, WM_COMMAND, 0xbbe, 0); // 0x3f1 -- 'Display' button, it shows first item
}
//2.
// Window hierarchy:
// + Main window
// + HH Child
// + Browser ...
// + HH Child <- second "HH Child" window
// + Tab Control
// + Dialog
// - Combobox <- we have to fill this combo
// - Buttons <- and press this buttons
// ...
else
{
hTabWnd = FindWindowEx(hPaneWnd, NULL, _T("SysTabControl32"), NULL); // skip Tab Control
hDlgWnd = FindWindowEx(hTabWnd, NULL, NULL, NULL); // skip dialog
TCHAR szClass[64];
GetClassName(hDlgWnd, szClass, sizeof(szClass));
ATLTRACE("hDlgWnd(1) == %x", hDlgWnd);
if (!_tcsstr(szClass, "#")) // Is it dialog?
hDlgWnd = FindWindowEx(hTabWnd, hDlgWnd, NULL, NULL); // skip dialog
hCtlWnd = FindWindowEx(hDlgWnd, NULL, _T("ComboBox"), NULL); // well, it's combobox
// Set window text
ATLASSERT(hCtlWnd != NULL);
::SendMessage(hCtlWnd, WM_SETTEXT, 0, (LPARAM)szBuf); // fill it by our query
//
// Run search and show first finded page
//
::SendMessage(hwndHelp, WM_COMMAND, 0x3ee, 0); // 0x3ee -- 'List Topics' button, it runs search
if (::SendMessage(GetDlgItem(IDC_CHECK1), BM_GETCHECK, 0, 0) == BST_CHECKED)
::SendMessage(hwndHelp, WM_COMMAND, 0x3f1, 0); // 0x3f1 -- 'Display' button, it shows first item
}
return 0;
}
EDIT:
You can get control ID of List Topic by spy++

Changing MFC List Control header color

I am looking for the simplest way to change the header color of a 'List Control' header in MFC C++. I have found ways to change individual cells and rows, but can't get a working version that changes the header color. This is all the code I am using that deals with the header:
//Initializes the List Control with four columns
m_CListCtrl.InsertColumn(0, _T("Option"), LVCFMT_LEFT, 200);
m_CListCtrl.InsertColumn(1, _T("User"), LVCFMT_LEFT, 60);
m_CListCtrl.InsertColumn(2, _T("Value"), LVCFMT_LEFT, 80);
m_CListCtrl.InsertColumn(3, _T("Description"), LVCFMT_LEFT, 800);
This can be done, but, not without a little extra coding. What you’ll need to do:
Derive your own class from CListCtrl.
Derive your own class from CHeaderCtrl.
Replace the standard CListCtrl header with yours.
Use custom drawing for the header control.
In derived CListCtrl,
void MyListCtrl::PreSubclassWindow()
{
// TODO: Add your specialized code here and/or call the base class
CHeaderCtrl* pHeader = NULL;
pHeader = GetHeaderCtrl();
if (pHeader != NULL)
{
VERIFY(m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd)); // m_HeaderCtrl is the new wrapper object
}
CListCtrl::PreSubclassWindow();
}
In the header class,
void MyHeader::OnNMCustomdraw(NMHDR *pNMHDR, LRESULT *pResult)
{
LPNMCUSTOMDRAW pNMCD = reinterpret_cast<LPNMCUSTOMDRAW>(pNMHDR);
// TODO: Add your control notification handler code here
*pResult = CDRF_DODEFAULT;
if (pNMCD->dwDrawStage == CDDS_PREPAINT)
{
CDC* pDC = CDC::FromHandle(pNMCD->hdc);
CRect rect(0, 0, 0, 0);
GetClientRect(&rect);
pDC->FillSolidRect(&rect, RGB(255, 0, 0));
*pResult = CDRF_NOTIFYITEMDRAW;
}
else if (pNMCD->dwDrawStage == CDDS_ITEMPREPAINT)
{
HDITEM hditem;
TCHAR buffer[MAX_PATH] = { 0 };
SecureZeroMemory(&hditem, sizeof(HDITEM));
hditem.mask = HDI_TEXT;
hditem.pszText = buffer;
hditem.cchTextMax = MAX_PATH;
GetItem(pNMCD->dwItemSpec, &hditem);
CDC* pDC = CDC::FromHandle(pNMCD->hdc);
pDC->SetTextColor(RGB(0, 0, 0));
pDC->SetBkColor(RGB(255, 0, 0));
CString str(buffer);
pDC->DrawText(str, CRect(pNMCD->rc), DT_VCENTER | DT_LEFT);
*pResult = CDRF_SKIPDEFAULT;
}
}
Using my sample code, you should see....
I'll leave any customization for you to finish.

TreeView add Shell Icons

I already created treeview where I can add some items. Basicly I want to tree-view all directories and files with icons associated to them.
So I have these functions:
Adding items to treeview
HTREEITEM AddItemToTree(HWND hwndTree, char *text, int nLevel)
{
TVINSERTSTRUCT tvins;
static HTREEITEM hPrev = (HTREEITEM)TVI_FIRST;
static HTREEITEM hRootItem = NULL;
static HTREEITEM hPrevLev2Item = NULL;
AddIconToTree(hwndTree, text); //////////// THIS IS THE FUNCTION BELOW...
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIS_STATEIMAGEMASK;
tvi.iImage = 0;
tvi.iSelectedImage = 0;
tvi.pszText = GetFileNameFromPath(text);
tvins.hInsertAfter = hPrev;
tvins.item = tvi;
if(nLevel == 1)
{
tvins.hParent = TVI_ROOT;
}
else if(nLevel == 2)
{
tvins.hParent = hRootItem;
}
else
{
TVITEM tviSetup;
tviSetup.hItem = hPrev;
tviSetup.mask = TVIF_PARAM;
TVITEM * tviLocal = &tviSetup;
TreeView_GetItem(hwndTree, tviLocal);
if(nLevel > tviLocal->lParam)
{
tvins.hParent = hPrev;
}
else
{
HTREEITEM hPrevLocal = TreeView_GetParent(hwndTree, hPrev);
tviLocal->hItem = hPrevLocal;
TreeView_GetItem(hwndTree, tviLocal);
for(int i = nLevel; i <= tviLocal->lParam;)
{
HTREEITEM hPrevLocalTemp = TreeView_GetParent(hwndTree, hPrevLocal);
hPrevLocal = hPrevLocalTemp;
tviLocal->hItem = hPrevLocal;
TreeView_GetItem(hwndTree, tviLocal);
}
tviLocal->mask = TVIF_TEXT;
TreeView_GetItem(hwndTree, tviLocal);
tvins.hParent = hPrevLocal;
}
}
hPrev = (HTREEITEM)SendMessage(hwndTree, TVM_INSERTITEM, 0, (LPARAM)(LPTVINSERTSTRUCT)&tvins);
if(hPrev == 0)
{
return false;
}
if(nLevel == 1)
{
hRootItem = hPrev;
}
return hPrev;
}
ADDING ICONS TO TREEVIEW:
int AddIconToTree(HWND hwndTree, char *strPath)
{
SHFILEINFO sfi;
memset(&sfi, 0, sizeof(sfi));
SHGetFileInfo(strPath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi), SHGFI_ICON | SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON);
int index = sfi.iIcon;
ICONINFO iconinfo;
GetIconInfo(sfi.hIcon, &iconinfo);
HBITMAP hBitmap = iconinfo.hbmColor;
DestroyIcon(sfi.hIcon);
himg = ImageList_Create(16, 16, ILC_COLOR32, 1, 1);
int iImageList = ImageList_Add(himg, hBitmap, NULL);
DeleteObject(hBitmap);
//TreeView_SetImageList(hwndTree, himg, TVSIL_NORMAL);
SendMessage(hwndTree, TVM_SETIMAGELIST, (WPARAM)TVSIL_NORMAL, (LPARAM)(HIMAGELIST)himg);
MessageBox(hwnd, strPath, "Path:", MB_OK); /* Because of this msgbox I found out what is
really happening, because without it everything I have seen when I run the program was
treeview with icon of the last file, which was folder...So blank icon.*/
return index;
}
My main problem is, that when I'm setting some icon, it is set not only for the one item as I'm expecting, but on all items in treeview. So basicly every item's icon is overwritten by new item's icon. By the way I know that if I want to get icon of folder, I need to use FILE_ATTRIBUTE_DIRECTORY...
So that's it.
Any help would be greatly appriciated!
Thank You in advance :-)
In your AddIconToTree function you're creating a brand new image list every time, which will only ever have one icon in it. You need to maintain the same image list and add additional icons to it rather than re-creating it for every item.
Alternatively, you can get a handle to the shell imagelist with the SHGetImageList function and then assign it to the tree directly. If you don't need to add any of your own images to the tree's image list this is a much easier way of displaying system icons for files and folders as the shell imagelist handles all that for you.
// To initialise the tree's image list - do this one time only
HIMAGELIST himg;
if (SUCCEEDED(SHGetImageList(SHIL_SMALL, IID_IImageList, reinterpret_cast<void**>(&himg))))
SendMessage(hwndTree, TVM_SETIMAGELIST, (WPARAM)TVSIL_NORMAL, (LPARAM)himg);
Then your AddIconToTree function simply becomes:
int AddIconToTree(HWND hwndTree, char *strPath)
{
SHFILEINFO sfi;
memset(&sfi, 0, sizeof(sfi));
// SHGFI_SYSICONINDEX will return the icon's index within the shell image list
SHGetFileInfo(strPath, FILE_ATTRIBUTE_NORMAL, &sfi, sizeof(sfi),
SHGFI_SYSICONINDEX | SHGFI_USEFILEATTRIBUTES);
return sfi.iIcon;
}
And when you actually add items to the list, make sure you assign the index to the item:
tvi.mask = TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIS_STATEIMAGEMASK;
tvi.iImage = tvi.iSelectedImage = AddIconToTree(hwndTree, text);

Get text in a listbox control added to a child window

I want to add the text in a listbox control to the child of my main window. The child is essentially an edit control, but is not a dialog. I have tried a few different functions already with no success, I believe my problem is that I need to somehow switch focus from the dialog window to the child window before adding the text. I would prefer not to get an answer with specific code, but if I could be pointed to a helpful function or concept, that would be great!
EDIT: The listbox is part of a larger dialog window that allows the user to enter text and then add it to the list. These functions are working very well. What I'd like to do is get the text that is added to the list moved into the child window when the user clicks a button on the dialog, preferably without the user having to select the items before clicking the button.
There's a lot of code, but I think these pieces are relevant:
Child window:
case WM_CREATE:
{
hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL,
0, 0, 100, 100, w, (HMENU) IDC_EDIT, NULL, NULL);
if (hEdit == NULL){
MessageBox(NULL, "Could not create child window :(", "ERROR", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
}
break;
case WM_SIZE:
{
RECT wSize;
GetClientRect(w, &wSize);
SetWindowPos(hEdit, NULL, 0, 0, wSize.right, wSize.bottom, NULL);
}
Function to add text to child window by clicking a button on the dialog (HWND hEdit, the child window, is defined globally):
case ID_ADDMAIN:
{
HWND hList = GetDlgItem(w, IDC_LIST1);
int count = SendMessage(hList, LB_GETCOUNT, NULL, NULL);
if (count > 0){
DWORD textLength = GetWindowTextLength(hList);
LPSTR alloc;
alloc = (LPSTR) GlobalAlloc(GPTR, textLength + 1);
if(GetWindowText(hList, alloc, textLength + 1)){
SendMessage(hEdit, WM_SETTEXT, NULL, (LPARAM) alloc);
}
GlobalFree(alloc);
}
else{
MessageBox(NULL, "There's nothing to add!", "???", MB_ICONINFORMATION | MB_OK);
}
}
break;
Besides the SendMessage function, I also tried SetWindowText and I tried getting each string in the listbox separately using a for loop rather than GetWindowText. Thank you in advance for your help.
You misunderstand the list box. The LB_GETCOUNT message is used to get the number of items (rows) in the list box. (Not the number of characters.)
GetWindowText is not appropriate for a list box: It gets the titlebar text, but a list box has no titlebar. What you can do with a list box is find out which row is selected (LB_GETCURSEL) and then get the text from that row (LB_GETTEXT).
Here is a list of functions I wrote for my WINAPI class library..
int ListBox::GetItemCount() const
{
return SendMessage(Control::GetHandle(), LB_GETCOUNT, 0, 0);
}
int ListBox::GetSelectedIndex() const
{
return SendMessage(Control::GetHandle(), LB_GETCURSEL, 0, 0);
}
void ListBox::SetSelectedIndex(int Index)
{
SendMessage(Control::GetHandle(), LB_SETCURSEL, Index, 0);
}
void ListBox::AddItem(const tstring &Item, int Index)
{
SendMessage(Control::GetHandle(), Index == 0 ? LB_ADDSTRING : LB_INSERTSTRING, Index, reinterpret_cast<LPARAM>(Item.c_str()));
}
void ListBox::RemoveItem(int Index)
{
SendMessage(Control::GetHandle(), LB_DELETESTRING, Index, 0);
}
void ListBox::Clear()
{
SendMessage(Control::GetHandle(), LB_RESETCONTENT, 0, 0);
}
int ListBox::GetIndexOf(const tstring &Item)
{
return SendMessage(Control::GetHandle(), LB_FINDSTRINGEXACT, -1, reinterpret_cast<LPARAM>(Item.c_str()));
}
int ListBox::GetIndexPartial(const tstring &Item)
{
return SendMessage(Control::GetHandle(), LB_FINDSTRING, -1, reinterpret_cast<LPARAM>(Item.c_str()));
}
void ListBox::SetColumnWidth(int Width)
{
SendMessage(Control::GetHandle(), LB_SETCOLUMNWIDTH, Width, 0);
}
tstring ListBox::GetItem(int Index) const
{
std::vector<char> Buffer(SendMessage(Control::GetHandle(), LB_GETTEXTLEN, Index, 0) + 1);
SendMessage(Control::GetHandle(), LB_GETTEXT, Index, reinterpret_cast<LPARAM>(Buffer.data()));
return tstring(Buffer.begin(), Buffer.end());
}
You'll need:
ListBox::GetItem(ListBox::GetSelectedIndex());
to get the text of the current item in the list box..
For the Edit control, I have:
void TextBox::SetReadOnly(bool ReadOnly)
{
SendMessage(Control::GetHandle(), EM_SETREADONLY, ReadOnly, 0);
}
void TextBox::SetPassword(bool Enabled, char PasswordChar)
{
SendMessage(Control::GetHandle(), EM_SETPASSWORDCHAR, Enabled ? PasswordChar : 0, 0);
}
std::uint32_t TextBox::GetTextLength() const
{
return GetWindowTextLength(Control::GetHandle());
}
void TextBox::ShowScrollBar(bool Show, int wBar)
{
::ShowScrollBar(Control::GetHandle(), wBar, true);
}
void TextBox::AppendText(const tstring &Text) const
{
SendMessage(Control::GetHandle(), EM_SETSEL, -1, -1);
SendMessage(Control::GetHandle(), EM_REPLACESEL, 0, reinterpret_cast<LPARAM>(Text.c_str()));
}
tstring TextBox::GetText() const
{
std::vector<TCHAR> Buffer(GetWindowTextLength(Control::GetHandle()) + 1);
GetWindowText(Control::GetHandle(), Buffer.data(), Buffer.size());
return tstring(Buffer.begin(), Buffer.end());
}
void TextBox::SetText(const tstring &Text)
{
this->Title = Text;
SetWindowText(Handle, Text.c_str());
}
You can get the text from the edit box easily with these.. I hope these aid you greatly..

am trying to create class to encapsulate toolbar but the background turns black. c++ win32api

I created a simple class to hide the details of creating a toolbar in win32 API but I don't like the toolbars it is producing. (See image for clarification. I don't have reputation points so I have just posted a link)
http://i35.tinypic.com/1zmfeip.jpg
I have no idea now the black background is coming into my application.
Here is the class declaration in file CToolBar.h
#ifndef _CTOOLBAR_H
#define _CTOOLBAR_H
#include<windows.h>
#include<commctrl.h>
class CToolBar
{
public:
CToolBar();//constructor
~CToolBar();//destructor
void AddButton(int iconID, int command);//add Both a button, its icon and its command ID
void Show();//display the toolbar
void Initialise(HINSTANCE hInst, HWND hParent);
protected:
HINSTANCE m_hInst;
HWND m_hParent;
HWND m_hToolBar;
HIMAGELIST m_hImageList;
TBBUTTON m_Tbb[4]; //toolbar buttons
int m_numberButtons;
};
#endif
here is the implementation in file CToolBar.cpp
//CToolBar.cpp
#include "CToolBar.h"
#include<windows.h>
#include<commctrl.h>
CToolBar::CToolBar()//the constructor
{
m_hImageList=ImageList_Create(32, 32, ILC_COLOR32, 0, 15);//returns NULL if the function fails
//finish other initialisations
InitCommonControls();//initialise commctrl.dll whatever.. or else your toolbar wont appear
}
void CToolBar::Initialise(HINSTANCE hInst, HWND hParent)
{
m_hInst=hInst;
m_hParent=hParent;
m_hToolBar=CreateWindowEx(
WS_EX_PALETTEWINDOW ,
TOOLBARCLASSNAME,
"",
WS_CHILD | WS_CLIPCHILDREN | WS_CLIPSIBLINGS |WS_VISIBLE|TBSTYLE_BUTTON | TBSTYLE_TOOLTIPS | CCS_ADJUSTABLE | CCS_TOP ,
0, 0,
0, 0,
m_hParent,
NULL,
m_hInst,
0);
}
CToolBar::~CToolBar()//destructor
{
ImageList_Destroy(m_hImageList);
}
void CToolBar::AddButton(int iconID, int command)
{
HICON hIcon = LoadIcon(m_hInst, MAKEINTRESOURCE(iconID));
ImageList_AddIcon(m_hImageList, hIcon);
DeleteObject(hIcon);
if(iconID!= -1)//-1 means the separator. The rest are mere buttons
{
m_Tbb[m_numberButtons].iBitmap =m_numberButtons;
m_Tbb[m_numberButtons].idCommand = command;
m_Tbb[m_numberButtons].fsState = TBSTATE_ENABLED;
m_Tbb[m_numberButtons].fsStyle = TBSTYLE_BUTTON;
m_Tbb[m_numberButtons].dwData = 0;
m_Tbb[m_numberButtons].iString = 0;
}
else//ie if (iconID== -1) ; then display the separator. the command value is ignored
{
m_Tbb[m_numberButtons].iBitmap =-1;
m_Tbb[m_numberButtons].idCommand = 0;
m_Tbb[m_numberButtons].fsState = TBSTATE_ENABLED;
m_Tbb[m_numberButtons].fsStyle = TBSTYLE_SEP;
m_Tbb[m_numberButtons].dwData = 0;
m_Tbb[m_numberButtons].iString = 0;
}
m_numberButtons++;
}
void CToolBar::Show()
{
SendMessage(m_hToolBar, TB_SETIMAGELIST , (WPARAM)0, (LPARAM)m_hImageList);
SendMessage(m_hToolBar, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0);//message for backward
//compatibility
SendMessage(m_hToolBar, TB_ADDBUTTONS, m_numberButtons, (LPARAM)m_Tbb);
SendMessage(m_hToolBar,WM_SIZE,0,0);
ShowWindow(m_hToolBar, SW_SHOW);
}
How i used the class
in main.cpp, i created a global instance of the class.
CToolBar myToolBar;
in the callback procedure, under WM_CREATE, I used some member functions.
case WM_CREATE:
myToolBar.Initialise(g_hInst,hwnd);
myToolBar.AddButton(IDI_OPEN, ID_OPEN);
myToolBar.AddButton(IDI_MAIN,ID_OPEN);//Separator button
myToolBar.AddButton(IDI_CLOSE, ID_CLOSE);
myToolBar.AddButton(IDI_CLOSEALL, ID_CLOSE);
myToolBar.Show();
break;
That's about it.
Try modifying the flags parameter of ImageList_Create to include ILC_MASK as well
Looks like you are using bitmap with transparency channel. GDI does not support alpha channel. It uses special color which will be transparent. If you want to support 32-bit bitmaps you could use GDI+ for drawing such bitmaps. Another option is to use CAplhaToolbar which already supports bitmaps with alpha transparency.