C语言 工具栏创建


VOID CreatToolBar(HINSTANCE hInstance,HWND hParent)

{
//1.初始这个公共控件的DLL,因要指定使用ComCtl32.dll来启用可视化方式,需要用InitCommonControlsEx()。否则,将无法创建窗口。
INITCOMMONCONTROLSEX ComCtr={2*sizeof(DWORD),ICC_BAR_CLASSES};
InitCommonControlsEx(&ComCtr);


//2.创建工具栏窗口
HWND hToolBar=CreateWindowEx(WS_EX_TOOLWINDOW,  //dwExStyle
TOOLBARCLASSNAME,  //class name
TEXT("工具栏"),   //window name
WS_CHILD |WS_VISIBLE,  //dwStyle
NULL,  NULL,NULL,NULL, //位置与大小
hParent,  //父窗口
(HMENU)0,hInstance,NULL);   


//3.发送TB_BUTTONSTRUCTSIZE消息初始化这个工具栏,如果使用CreateToolbarEx创建则不用发送,其会自动发出.
//If an application uses the CreateWindowEx function to create the toolbar, 
//the application must send this message to the toolbar before sending the TB_ADDBITMAP or TB_ADDBUTTONS message. 
//The CreateToolbarEx function automatically sends TB_BUTTONSTRUCTSIZE, and the size of the TBBUTTON structure is a parameter of the function. 
SendMessage(      // returns LRESULT in lResult     
(HWND) hToolBar,      // handle to destination control     
(UINT) TB_BUTTONSTRUCTSIZE,      // message ID     
(WPARAM) sizeof(TBBUTTON),      // = (WPARAM) (int) Size, in bytes, of the TBBUTTON structure.    
(LPARAM) 0      // = 0; not used, must be zero
);  


//4.发送TB_ADDBITMAP消息添加工具栏图片
//Handle to the module instance with the executable file that contains a bitmap resource. 
//To use bitmap handles instead of resource IDs, set this member to NULL.
TBADDBITMAP tbTbab;
int nBtn = 15;
//方法一:自定义图片
//HBITMAP hBitmap   =  LoadBitmap(hInstance,   MAKEINTRESOURCE(IDB_BITMAP1));  
//tbTbab.hInst = NULL;
//tbTbab.nID = (UINT_PTR)hBitmap;//使用HBITMAP
//方法二:自定义图片
//tbTbab.hInst = hInstance;
//tbTbab.nID = (UINT_PTR)IDB_BITMAP1;//使用resource 
//方法三:默认图片
tbTbab.hInst = HINST_COMMCTRL;
tbTbab.nID = IDB_STD_SMALL_COLOR;
SendMessage(hToolBar,TB_ADDBITMAP,nBtn,(LPARAM)&tbTbab); 


//5.TBBUTTON数组赋值完成后,发送ADDBUTTONS消息,添加按钮及对应事件到工具栏。
TBBUTTON *ptbBtn=new TBBUTTON[nBtn];


for(int   n=0;   n<nBtn;   n++)   {   
ptbBtn[n].iBitmap   =   n;//图片资源中的第n个元素.
ptbBtn[n].idCommand   =  ID_FILE_OPEN; //对应事件ID
ptbBtn[n].fsState   =   TBSTATE_ENABLED;   
ptbBtn[n].fsStyle   =   TBSTYLE_CHECKGROUP|TBSTYLE_BUTTON|TBSTYLE_CHECK;
ptbBtn[n].dwData   =   0;   
ptbBtn[n].iString   =   0;//(INT_PTR)TEXT("d");下标文字
}


SendMessage(      // returns LRESULT in lResult     
(HWND) hToolBar,      // handle to destination control     
(UINT) TB_ADDBUTTONS, // message ID     
(WPARAM) nBtn,      // = (WPARAM) (UINT) uNumButtons;    
(LPARAM) ptbBtn      // = (LPARAM) (LPTBBUTTON) lpButtons; 
);  
//6.还有TB_ADDSTRING,TB_SETBUTTONWIDTH ,TB_SETBITMAPSIZE ,TB_SETBUTTONSIZE 等消息设置
//7.最后发送TB_AUTOSIZE消息,通知工具栏刷新按钮的大小信息。
SendMessage(hToolBar,   TB_AUTOSIZE,   0,   0);   
ShowWindow(hToolBar,   SW_SHOW);
delete[] ptbBtn;
}

参考文章:http://blog.csdn.net/aasmfox/article/details/5978220

你可能感兴趣的:(C语言,toolbar)