在win32程序中添加工具栏状态栏

Posted skdev

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在win32程序中添加工具栏状态栏相关的知识,希望对你有一定的参考价值。

 

#include<commctrl.h>

#pragma comment(lib,"comctl32.lib")

 

HWND MakeToolbar(HWND hWnd,HINSTANCE hInst);

HWND MakeStatusbar(HWND hWnd);

int SetStatus(HWND hWnd,HWND hStatus);

HINSTANCE hInst;

HWND    hToolbar;

HWND    hStatusbar;

 

case WM_CREATE:

     InitCommonControls();

     hToolbar=MakeToolbar(hWnd,hInst);

     hStatusbar=MakeStatusbar(hWnd);

     SetStatus(hWnd,hStatusbar);

     break;

      

case WM_SIZE:

     SendMessage(hToolbar,message,wParam,lParam);

     SendMessage(hStatusbar,message,wParam,lParam);

     SetStatus(hWnd,hStatusbar);

     break;

 

/*名称:MakeToolBar

  功能:创建BMP工具栏

  参数:HWND,HINSTANCE

*/

HWND MakeToolbar(HWND hWnd,HINSTANCE hInst)

{

       HWND    hTOOL;

       TBBUTTON tbb[3];

       HIMAGELIST hImageList;

       HBITMAP      hBitmap;

 

       hTOOL=CreateWindowEx(0,TOOLBARCLASSNAME,(LPCTSTR)NULL, WS_CHILD|WS_BORDER|WS_VISIBLE|TBSTYLE_TOOLTIPS,0,0,0,0,hWnd,(HMENU) ID_TOOLBAR, hInst, NULL);

       SendMessage(hTOOL,TB_SETSTYLE,0,(LPARAM)TBSTYLE_FLAT|CCS_TOP);

        SendMessage(hTOOL, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof(TBBUTTON), 0);

 

       hImageList= ImageList_Create(32,30,ILC_COLOR24,2,1);

       hBitmap = (HBITMAP)LoadImage(hInst,"toolbar.bmp",IMAGE_BITMAP,64,30,LR_LOADFROMFILE);

       ImageList_Add(hImageList,hBitmap,NULL);

       DeleteObject (hBitmap);

       SendMessage(hTOOL,TB_SETIMAGELIST,0,(LPARAM)hImageList);

 

       ZeroMemory(tbb, sizeof(tbb));

       tbb[0].iBitmap = MAKELONG(0,0) ;//MAKELONG(index,0);

       tbb[0].idCommand = IDM_SAVE;

       tbb[0].fsState = TBSTATE_ENABLED;

       tbb[0].fsStyle = TBSTYLE_FLAT;

       tbb[0].iString = (INT_PTR)TEXT("");

       tbb[1].iBitmap = MAKELONG(1,0) ;//MAKELONG(index,0);

       tbb[1].idCommand = IDM_SAVE;

       tbb[1].fsState = TBSTATE_ENABLED;

       tbb[1].fsStyle = TBSTYLE_FLAT;

       tbb[1].iString = (INT_PTR)TEXT("");

       tbb[2].iBitmap = 0

       tbb[2].idCommand =0;

       tbb[2].fsState = TBSTATE_ENABLED;

       tbb[2].fsStyle = TBSTYLE_SEP;

       tbb[2].iString = (INT_PTR)TEXT("");

       SendMessage(hTOOL,TB_ADDBUTTONS,3,(LPARAM)&tbb);

       ShowWindow(hTOOL,SW_SHOW);

 

       MakeTooltips(hTOOL,hInst);

       return hTOOL;

}

int MakeTooltips(HWND hWnd,HINSTANCE hInst)

{

       HWND           hTooltips;

       INITCOMMONCONTROLSEX icex;

    TOOLINFO    ti;

 

    // Load the ToolTip class from the DLL.

    icex.dwSize = sizeof(icex);

    icex.dwICC  = ICC_BAR_CLASSES;

       ti.cbSize  = sizeof(TOOLINFO);

    ti.uFlags  = TTF_SUBCLASS;

    ti.hwnd          = hWnd;  //工具栏的句柄   

    ti.hinst     = hInst;   

    ti.rect.left =0;

       ti.rect.top = 0;

       ti.rect.bottom =0;

       ti.rect.right =0;

 

 

       hTooltips=CreateWindowEx(WS_EX_TOPMOST, TOOLTIPS_CLASS, NULL,

                            WS_POPUP | TTS_NOPREFIX | TTS_ALWAYSTIP,

                            CW_USEDEFAULT, CW_USEDEFAULT,

                            CW_USEDEFAULT, CW_USEDEFAULT,

                            hWnd, NULL, hInst,

                            NULL);

       SetWindowPos(hTooltips, HWND_TOPMOST,0, 0, 0, 0,

             SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);

 

       ti.uId              = IDM_START;

       ti.lpszText = "启动";

       SendMessage(hTooltips,TTM_ADDTOOL,0,(LPARAM)&ti);

       ti.uId              = IDM_STOP;

       ti.lpszText = "停止";

       SendMessage(hTooltips,TTM_ADDTOOL,0,(LPARAM)&ti);

 

       SendMessage(hWnd,TB_SETTOOLTIPS,(WPARAM)hTooltips,0);

       return 1;

}

/*名称:MakeStatusbar

  功能:创建状态栏

  参数:HWND

*/

HWND MakeStatusbar(HWND hWnd)

{

       HWND hStatus;

 

       hStatus=CreateWindow(STATUSCLASSNAME,"",WS_CHILD|WS_VISIBLE,0,0,0,0,hWnd,NULL,hInst,NULL);

       return hStatus;

}

/*名称:SetStatus

  功能:将状态栏划分成多格

  参数:hWnd主窗口句柄,hStatus状态栏句柄

*/

int SetStatus(HWND hWnd,HWND hStatus)

{

       int Rightend[3];

       RECT WinRect;

 

       GetClientRect(hWnd,&WinRect);

       Rightend[0]=WinRect.right/3;

       Rightend[1]=WinRect.right*2/3;

       Rightend[2]=WinRect.right;

       SendMessage(hStatus,SB_SETPARTS,(WPARAM)3,(LPARAM)Rightend);

       return 1;

}

/*名称:SetStatusText

  功能:设置状态栏某一格的标题

  参数:hStatus状态栏句柄,ii格,txt标题

*/

int SetStatusText(HWND hStatus,int i,char *txt)

{

       SendMessage(hStatus,SB_SETTEXT,(WPARAM)i,(LPARAM)txt);

       return 1;

}

toolbar.bmp

以上是关于在win32程序中添加工具栏状态栏的主要内容,如果未能解决你的问题,请参考以下文章

w7如何添加快捷启动图标的相关问题

Qt学习三 - 菜单栏工具栏状态栏

第八课——菜单工具栏和状态栏

透明状态栏下工具栏不会滚动

PyQt5学习记录---QMainWindow菜单栏状态栏和工具栏

DELPHI标题栏添加菜单栏