zoukankan      html  css  js  c++  java
  • 自定义工具条过程及范例

    支持真彩色图标,可添加文字。
    动机:传统的VC工具栏只支持16色的图标,且不能添加文字。
    要点:CToolBarCtrl类的使用。先引用MSDN上的话(翻译水平比较菜,见谅!)
    使用CToolBarCtrl类,一般遵从以下几个步骤:
    1.构造一个CToolBarCtrl对象。
    2.调用Create函数创建Windows工具条通用控件并与CToolBarCtrl对象相关联。
    3.确定工具条上的按钮如何显示:
    (1)使用位图图像。调用AddBitmap向工具条添加按钮位图
    (2)使用图像列表里面显示的图像。调用SetImageList函数、SetHotImageList函数、SetDisabledImageList函数指定图像列表
    (3)作用字符串标签。调用AddString和(或)AddStrings函数为工具栏添加字符串
    4.调用AddButtons函数为工具条添加按钮结构
    5.如果需要为不是CFrameWnd的拥有窗口添加工具提示,需要在工具条拥有窗口中传递TTN_NEEDTEXT消息,该消息在CToolBarCtrl: Handling Tool Tip Notifications中有所描述。
    步骤:
    1.将要作为工具栏图标的位图或图标导入到VC资源管理器中。
    2.在C***Dlg类为添加两个成员变量:CImageList m_ImageList,CToolBarCtrl m_ToolBar
    3.在OnInitDialog()函数中添加如下代码:

    1. CBitmap bm;  
    2. UINT Resource[3]={IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3}; //位图ID数组  
    3. int i;  
    4.   
    5. m_ImageList.Create(32,32,ILC_COLOR24|ILC_MASK,0,0); //创建Image List  
    6. m_ToolBar.Create(TBSTYLE_FLAT | CCS_TOP | WS_CHILD | WS_VISIBLE | WS_BORDER | CCS_ADJUSTABLE,CRect(0,0,0,0),this,IDR_TOOLBAR); //创建Toolbar Control  
    7. m_ToolBar.SetBitmapSize(CSize(32,32));  
    8.   
    9. for(i=0;i<3;i++)  
    10. {  
    11.     bm.LoadBitmap(Resource[i]);  
    12.     m_ImageList.Add(&bm,(CBitmap *)NULL);  
    13.     bm.Detach();  
    14. }  
    15.   
    16. m_ToolBar.SetImageList(&m_ImageList);  
    17.   
    18. TBBUTTON Buttons[3]; //定义TBBUTTON结构体数组  
    19. CString str;  
    20.   
    21. for(i=0;i<3;i++)  
    22. {  
    23.     str.LoadString(IDS_FILE+i); //IDS_FILE是在String Table中添加的String  
    24.     Buttons[i].iString=m_ToolBar.AddStrings(str);  
    25.     Buttons[i].dwData=0;  
    26.     Buttons[i].fsState=TBSTATE_ENABLED;  
    27.     Buttons[i].fsStyle=TBSTYLE_BUTTON;  
    28.     Buttons[i].iBitmap=i;  
    29.     Buttons[i].idCommand=IDS_FILE+i; //按钮命令响应  
    30. }  
    31. m_ToolBar.AddButtons(3,Buttons);  
    32. m_ToolBar.AutoSize();  
    33. m_ToolBar.ShowWindow(SW_SHOW);  

    注解:TBBUTTON是定义工具条按钮的结构体,声明如下:

    1. typedef struct _TBBUTTON { int iBitmap;// zero-based index of button image int idCommand; // command to be sent when button pressed BYTE fsState; // button state--see below BYTE fsStyle; // button style--see below DWORD dwData; // application-defined value   

    调用AddButtons函数向工具栏添加按钮。函数原型如下: 

      1. BOOL AddButtons( int nNumButtons, LPTBBUTTON lpButtons );     //其中nNumButtons是要添加的按钮数目,lpButtons是指向TBBUTTON结构体的指针。   

    范例:

    if (!m_wndToolBar.CreateEx(
       this,
       TBSTYLE_FLAT,
       WS_CHILD|WS_VISIBLE|CBRS_TOP|CBRS_GRIPPER|CBRS_TOOLTIPS|CBRS_FLYBY|CBRS_SIZE_DYNAMIC|CBRS_BORDER_TOP) || !m_wndToolBar.LoadToolBar(IDR_TOOLBAR))
       {
       TRACE0("Failed to create toolbar\n");
       return -1;
       }
       RepositionBars(AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
      
       // Get bitmap 
       CBitmap cBitmap;
       HBITMAP hBmp = (HBITMAP)::LoadImage(
       AfxGetInstanceHandle(),
       MAKEINTRESOURCE(IDB_NORMAL_TB), 
       IMAGE_BITMAP,
       0,
       0,
       LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
       cBitmap.Attach(hBmp);
       m_tbImageList.Create(48, 48, ILC_MASK | ILC_COLOR32, 0, 0);
       m_tbImageList.Add(&cBitmap, RGB(192,192,192));
       m_wndToolBar.GetToolBarCtrl().SetImageList(&m_tbImageList);
       cBitmap.Detach();
       DeleteObject(hBmp);
      
       // Add disable bitmap
       hBmp = (HBITMAP)::LoadImage(
       AfxGetInstanceHandle(),
       MAKEINTRESOURCE(IDB_DISABLE_TB), 
       IMAGE_BITMAP,
       0,
       0,
       LR_CREATEDIBSECTION | LR_LOADMAP3DCOLORS);
       cBitmap.Attach(hBmp);
       m_tbDisableImages.Create(48, 48, ILC_MASK | ILC_COLOR32, 0, 0);
       m_tbDisableImages.Add(&cBitmap, RGB(192,192,192));
       m_wndToolBar.GetToolBarCtrl().SetDisabledImageList(&m_tbDisableImages);
       cBitmap.Detach();
       DeleteObject(hBmp);
      
       // Disable Up button
       m_wndToolBar.GetToolBarCtrl().EnableButton(ID_VIEW_UP, FALSE);
      
       return TRUE;

  • 相关阅读:
    【CompOFA】2021-ICLR-CompOFA Compound Once-For-All Networks For Faster Multi-Platform Deployment-论文阅读
    【NAT】2021-TPAMI-Neural Architecture Transfer-论文阅读
    【CBAM】2018-ECCV-CBAM: Convolutional block attention module-论文阅读
    【CafeNet】2021-ICLR-Locally Free Weight Sharing for Network Width Search-论文阅读
    【ManiDP】2021-CVPR-Manifold Regularized Dynamic Network Pruning-论文阅读
    【AttentiveNAS】2021-CVPR-AttentiveNAS Improving Neural Architecture Search via Attentive Sampling-论文阅读
    【SCAN】2019-NIPS-SCAN: A Scalable Neural Networks Framework Towards Compact and Efficient Models-论文阅读
    【AKD】2020-CVPR-Search to Distill Pearls are Everywhere but not the Eyes-论文阅读
    【Dynamic Convolution】2020-CVPR-Dynamic Convolution Attention over Convolution Kernels-论文阅读
    【DNA】2020-CVPR-Blockwisely Supervised Neural Architecture Search with Knowledge Distillation-论文阅读
  • 原文地址:https://www.cnblogs.com/Victorzsg/p/3423443.html
Copyright © 2011-2022 走看看