我试图创建类来封装工具栏,但背景变成黑色。 c++ win32api

发布于 2024-08-09 20:28:32 字数 3695 浏览 6 评论 0原文

我创建了一个简单的类来隐藏在 win32 API 中创建工具栏的详细信息,但我不喜欢它生成的工具栏。 (请参阅图片以进行澄清。我没有信誉点,所以我刚刚发布了一个链接)

http:// /i35.tinypic.com/1zmfeip.jpg

我现在不知道黑色背景会进入我的应用程序。
这是文件 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

这是文件 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);
}

我如何使用该类
在main.cpp中,我创建了该类的全局实例。

CToolBar myToolBar; 

在WM_CREATE下的回调过程中,我使用了一些成员函数。

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;

就是这样。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

分分钟 2024-08-16 20:28:32

尝试修改 ImageList_Create 的 flags 参数以包含 ILC_MASK

Try modifying the flags parameter of ImageList_Create to include ILC_MASK as well

雄赳赳气昂昂 2024-08-16 20:28:32

看起来您正在使用带有透明通道的位图。 GDI 不支持 Alpha 通道。它使用特殊的颜色,会是透明的。如果您想支持 32 位位图,您可以使用 GDI+ 来绘制此类位图。另一种选择是使用 CAplhaToolbar 它已经支持位图阿尔法透明度。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文