Note that there are some explanatory texts on larger screens.

plurals
  1. POam trying to create class to encapsulate toolbar but the background turns black. c++ win32api
    text
    copied!<p>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) </p> <p><a href="http://i35.tinypic.com/1zmfeip.jpg" rel="nofollow noreferrer">http://i35.tinypic.com/1zmfeip.jpg</a></p> <p>I have no idea now the black background is coming into my application.<br> <b>Here is the class declaration in file CToolBar.h</b><br></p> <pre><code>#ifndef _CTOOLBAR_H #define _CTOOLBAR_H #include&lt;windows.h&gt; #include&lt;commctrl.h&gt; 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 </code></pre> <p><b>here is the implementation in file CToolBar.cpp</b> </p> <pre><code>//CToolBar.cpp #include "CToolBar.h" #include&lt;windows.h&gt; #include&lt;commctrl.h&gt; 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); } </code></pre> <p><b>How i used the class</b><br> <em>in main.cpp, i created a global instance of the class.</em> </p> <pre><code>CToolBar myToolBar; </code></pre> <p><em>in the callback procedure, under WM_CREATE, I used some member functions.</em> </p> <pre><code>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; </code></pre> <p>That's about it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload