[原]使用Win API创建工具栏

作者:朱金灿

来源:http://blog.csdn.net/clever101

 

     

          到网上查了下资料,初步实现了使用WinAPI创建工具栏。


        下面是创建一个标准工具栏的函数,(这里所谓标准工具栏是指工具栏按钮图片是WinSDK内置的)

BOOL CreateStandardToolbar(HWND hParent)
{
    // 首先需要先调用InitCommonControls函数,否则不能创建工具栏。
    InitCommonControls();

    HWND hTool;
    TBBUTTON tbb[3];
    TBADDBITMAP tbab;

    HINSTANCE hThisInstance = GetModuleHandle(NULL);

    hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE|CCS_ADJUSTABLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, 0, 0,
                           hParent,(HMENU)IDC_MAIN_TOOL,hThisInstance, NULL);

    if(hTool == NULL)
    {
        ::MessageBox(hParent, "Could not create tool bar.", "Error", MB_OK | MB_ICONERROR);
        return FALSE;
    }
    // 发送 TB_BUTTONSTRUCTSIZE 消息, 出于向后兼容的考虑.
    SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    tbab.hInst = HINST_COMMCTRL;
    tbab.nID = IDB_STD_SMALL_COLOR;
    SendMessage(hTool, TB_ADDBITMAP, 0, (LPARAM)&tbab);

    ZeroMemory(tbb, sizeof(tbb));
    tbb[0].iBitmap = STD_FILENEW;
    tbb[0].fsState = TBSTATE_ENABLED;
    tbb[0].fsStyle = TBSTYLE_BUTTON;
    tbb[0].idCommand = ID_FILE_NEW;

    tbb[1].iBitmap = STD_FILEOPEN;
    tbb[1].fsState = TBSTATE_ENABLED;
    tbb[1].fsStyle = TBSTYLE_BUTTON;
    tbb[1].idCommand = ID_FILE_OPEN;

    tbb[2].iBitmap = STD_FILESAVE;
    tbb[2].fsState = TBSTATE_ENABLED;
    tbb[2].fsStyle = TBSTYLE_BUTTON;
    tbb[2].idCommand = ID_FILE_SAVEAS;

    // 发送添加按钮的消息
    SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON), (LPARAM)&tbb);
    return TRUE;
}

然后在消息处理函数中调用:

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{

    case WM_CREATE:
    {
        CreateToolbar(hwnd);
        break;
}
// 下面是消息响应部分
  case WM_COMMAND:
    {
        switch(LOWORD(wParam))
        {
        case ID_FILE_NEW:
            ::MessageBox(hwnd, "新建文件", "提示", MB_OK);
            break;
        case ID_FILE_OPEN:
            ::MessageBox(hwnd, "打开文件", "提示", MB_OK);
            break;
        case ID_FILE_SAVEAS:
            ::MessageBox(hwnd, "另存文件", "提示", MB_OK);
            break;
        default:
            break;
        }
    }
}

效果图如下:

                                          

    但是我们一般是不会SDK内置的资源图片。现在流行界面配置,具体就是我想利用磁盘上位图图片,然后把它作为工具栏按钮图片动态加载到工具栏里去。于是我写了一个函数,实现了这个功能,代码如下:

       

// 工具栏按钮的命令ID
#define  ID_ENTIRE     1028
#define  ID_GRID       1029

BOOL CreateCoolToolbar(HWND hParent)
{
    InitCommonControls();
    // 创建工具栏窗口
    HWND hTool;
    HINSTANCE hThisInstance = GetModuleHandle(NULL);
    hTool = CreateWindowEx(0, TOOLBARCLASSNAME, NULL, WS_CHILD | WS_VISIBLE|CCS_ADJUSTABLE|WS_CLIPSIBLINGS|WS_CLIPCHILDREN, 0, 0, 0, 0,
                           hParent,(HMENU)IDC_MAIN_TOOL,hThisInstance, NULL);

    if(hTool == NULL)
    {
        ::MessageBox(hParent, "Could not create tool bar.", "Error", MB_OK | MB_ICONERROR);
        return FALSE;
    }
    // 发送 TB_BUTTONSTRUCTSIZE 消息, 出于向后兼容的考虑.
    SendMessage(hTool, TB_BUTTONSTRUCTSIZE, (WPARAM)sizeof(TBBUTTON), 0);

    // 创建图像列表
    HIMAGELIST  hImgList = ImageList_Create(16,16, ILC_COLOR32|ILC_MASK, 0, 0);

    // 为工具栏设置图像列表
    ::SendMessage(hTool, TB_SETIMAGELIST, 0, (LPARAM)hImgList);

    // 打开位图,将其加进图像列表
    TCHAR szModulePath[_MAX_PATH];
	::GetModuleFileName(NULL,szModulePath,_MAX_PATH);
	std::string strBinPath = szModulePath;

    int ImgID[2];
    // 获取位图文件路径
    strBinPath = strBinPath.substr(0,strBinPath.rfind('\\')+1);
    std::string strBmpPath = strBinPath + std::string(_T("Toolbar\\entire.bmp"));
    HBITMAP		hBitmap				= NULL;
    hBitmap = (HBITMAP)LoadImage(hThisInstance,strBmpPath.c_str(), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR|LR_LOADFROMFILE);
    ImgID[0] = ImageList_Add(hImgList,hBitmap,NULL);

    strBmpPath = strBinPath + std::string(_T("Toolbar\\Grid.bmp"));
    hBitmap				= NULL;
    hBitmap = (HBITMAP)LoadImage(hThisInstance,strBmpPath.c_str(), IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR|LR_LOADFROMFILE);
    ImgID[1] = ImageList_Add(hImgList,hBitmap,NULL);

    // 初始化工具栏按钮数组
    TBBUTTON tbb[2];
    tbb[0].iBitmap = ImgID[0];
    tbb[0].fsState = TBSTATE_ENABLED;
    tbb[0].fsStyle = TBSTYLE_BUTTON;
    tbb[0].idCommand = ID_ENTIRE;

    tbb[1].iBitmap = ImgID[1];
    tbb[1].fsState = TBSTATE_ENABLED;
    tbb[1].fsStyle = TBSTYLE_BUTTON;
    tbb[1].idCommand = ID_GRID;

    // 发送添加工具栏按钮的消息
    TBADDBITMAP tbab;
   ::SendMessage(hTool, TB_ADDBUTTONS, sizeof(tbb)/sizeof(TBBUTTON),(LPARAM)&tbb);

    // 设置工具栏按钮大小:16*16
    ::SendMessage(hTool, TB_SETBUTTONSIZE,NULL,(LPARAM) MAKELONG(16,16));
    return TRUE;
}


效果图如下:

                                                         





作者:clever101 发表于2012-2-29 23:48:05 原文链接
阅读:527 评论:3 查看评论
posted @ 2012-02-29 23:48  程序员天下  阅读(1124)  评论(0编辑  收藏  举报