工具栏的创建

工具栏的设计:RepositionBars(AFX_IDW_CONTROBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);//显示工具栏

在基于对话框的应用程序中,默认情况下,是不会创建工具栏窗口的。如果用户想要设计工具栏,可以通过工作区的“Resource View”标签页创建工具栏。

插入新的工具栏,并为每一个新插入的对象指定一个ID(双击对象即可打开属性栏,按Enter键也可以),如果不指定,系统会为每个工具栏按钮设置一个默认的ID。

工具栏按钮的命令处理:

在类向导窗口中的“Object IDs”列表中选择命令ID,在“Messages”列表中双击“COMMAND”选项,打开添加成员函数的窗口,这样就可以添加消息处理函数(如果某个菜单项的命令ID和工具栏相同,则单击他们有相同的效果)

工具栏的主要方法:

BOOL Create( CWnd* pParentWnd, DWORD dwStyle = WS_CHILD | WS_VISIBLE | CBRS_TOP, UINT nID = AFX_IDW_TOOLBAR );

这个函数创建一个工具栏并把这个工具栏个一个工具栏对象关联。

void SetSizes( SIZE sizeButton, SIZE sizeImage );

该方法用于设置按钮和位图的大小。CSize()给出

Call this member function to set the toolbar's buttons to the size, in pixels, specified in sizeButton. The sizeImage parameter must contain the size, in pixels, of the images in the toolbar's bitmap. The dimensions in sizeButton must be sufficient to hold the image plus 7 pixels extra in width and 6 pixels extra in height.

void SetHeight( int cyHeight );

该方法用于设计工具栏的高度

After calling SetSizes, use this member function to override the standard toolbar height. If the height is too small, the buttons will be clipped at the bottom.

If this function is not called, the framework uses the size of the button to determine the toolbar height.

 

BOOL LoadToolBar( LPCTSTR lpszResourceName );

BOOL LoadToolBar( UINT nIDResource );

该方法用于加载工具栏资源

lpszResourceName :标识资源名称

nIDResource :标识资源ID

BOOL LoadBitmap( LPCTSTR lpszResourceName );

BOOL LoadBitmap( UINT nIDResource );

该方法用于加载一个位图资源,位图中包含了每个工具栏按钮的图像

Call this member function to load the bitmap specified by lpszResourceName or nIDResource. The bitmap should contain one image for each toolbar button. If the images are not of the standard size (16 pixels wide and 15 pixels high), call SetSizes to set the button sizes and their images.

BOOL SetButtons( const UINT* lpIDArray, int nIDCount );

该方法用于向工具栏中添加按钮,并设置按钮的ID和图像索引。

lpIDArray:标识一个无符号整型树组,其中包含了按钮ID,如果数组中的某个元素值为ID_SEPARATOR,对应的按钮将是一个分隔条。

nIDCount:标识数组中元素的个数

 

This member function sets each toolbar button's command ID to the value specified by the corresponding element of the array lpIDArray. If an element of the array has the value ID_SEPARATOR, a separator is created in the corresponding position of the toolbar. This function also sets each button's style to TBBS_BUTTON and each separator's style to TBBS_SEPARATOR, and assigns an image index to each button. The image index specifies the position of the button’s image within the bitmap.

You do not need to account for separators in the bitmap because this function does not assign image indexes for separators. If your toolbar has buttons at positions 0, 1, and 3 and a separator at position 2, the images at positions 0, 1, and 2 in your bitmap are assigned to the buttons at positions 0, 1, and 3, respectively.

int CommandToIndex( UINT nIDFind );

该方法根据工具栏按钮ID返回按钮索引。如果工具栏没有对应的ID返回-1;

This member function returns the index of the first toolbar button, starting at position 0, whose command ID matches nIDFind.

UINT GetItemID( int nIndex ) const;

该方法根据按钮索引返回按钮ID。

如何nIndex标识的按钮是一个分隔符,返回值是ID_SEPARATOR

virtual void GetItemRect( int nIndex, LPRECT lpRect );

该方法根据按钮索引获得工具栏按钮的显示区域。

void SetButtonStyle( int nIndex, UINT nStyle );

该方法用来设置某个按钮的风格。

  • TBBS_BUTTON   Standard pushbutton (default)

  • TBBS_SEPARATOR   Separator

  • TBBS_CHECKBOX   Auto check-box button

  • TBBS_GROUP   Marks the start of a group of buttons

  • TBBS_CHECKGROUP   Marks the start of a group of check-box buttons
  • 动态创建工具栏

    工具栏才创建有两种方法。一种是先创建一个工具栏资源,然后定义一个工具栏对象,接着调用Create或CreateEx方法创建工具栏窗口,最后调用LoadToolBar方法加载工具栏资源。

    另一种方法是先定义一个工具栏对象,然后调用Create或CreateEx方法创建工具栏窗口,最后调用SetButtons方法添加按钮并设置按钮ID。

    第一种方法:

    (1)创建一个基于对话框的应用程序。

    (2)设计一个工具栏资源,资源ID为IDR_TOOLBAR1

    (3)在对话框类中定义一个工具栏对象m_toolbar。

    (4)在对话框的OnInitDlg方法中创建工具栏窗口,加载工具栏资源

    m_toolbar.Create(this)

    m_toolbar.LoadToolBar(IDR_TOOLBAR1);

    RepositionBars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0);//显示工具栏

    第二种方法:

    (1)创建一个基于对话框的应用程序。

    (2)在对话框类中定义一个工具栏对象m_toolbar

    (3)在对话框初始化函数中创建工具栏窗口,添加工具栏按钮。

    UNIT array[6];

    for(int i=0;i<6;i++)

    {

    if(i==3)

          array[i]=ID_SEPARATOR;

    else

         array[i]=i+1001;

    m_toolbar.create(this);

    m_toolbar.SetButtons(array,6);

    m_toolbar.SetSize(CSize(40,30),CSize(20,20));

    Repositionbars(AFX_IDW_CONTROLBAR_FIRST,AFX_IDW_CONTROLBAR_LAST,0)

    }

     

     

     

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值