Author: Zhu Jincan
Source: http://blog.csdn.net/clever101
I checked the information on the Internet and initially implemented the use of winapi to create a toolbar.
The following is a function used to create a Standard toolbar. (The Standard toolbar indicates that the toolbar button image is built in winsdk)
Bool createstandardtoolbar (hwnd hparent) {// call the initcommoncontrols function first. Otherwise, the toolbar cannot be created. Initcommoncontrols (); hwnd htool; tbbutton TBB [3]; tbaddbitmap TBAB; hinstance hthisinstance = getmodulehandle (null); htool = createmediawex (0, toolbarclassname, null, ws_child | ws_visible | ccs_adjustable | average | ws_clipchildren, 0, 0, 0, 0, hparent, (hmenu) idc_main_tool, hthisinstance, null); If (htool = NULL ){:: messageBox (hparent, "cocould not create tool bar. "," error ", mb_ OK | mb_iconerror); Return false;} // send the tb_buttonstructsize message for backward compatibility considerations. 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; // send the sendmessage (htool, tb_addbuttons, sizeof (TBB)/sizeof (tbbutton), (lparam) & TBB); Return true ;}
Then, call the following in the message processing function:
Lresult callback windowprocedure (hwnd, uint message, wparam, lparam) {Case wm_create: {createconlbar (hwnd); break;} // The following section lists the case wm_command: {Switch (loword (wparam) {Case id_file_new: MessageBox (hwnd, "new file", "prompt", mb_ OK); break; Case id_file_open ::: messageBox (hwnd, "Open File", "prompt", mb_ OK); break; Case id_file_saveas: MessageBox (hwnd, "save another file", "prompt", mb_ OK ); break; default: break ;}}}
As follows:
However, we generally do not use the SDK's built-in resource images. Now popular interface configuration, specifically, I want to use the bitmap image on the disk, and then dynamically load it as a toolbar button image to the toolbar. So I wrote a function to implement this function,CodeAs follows:
// Command ID # define id_entire 1028 # define id_grid 1029 bool createcooltoolbar (hwnd hparent) {initcommoncontrols (); // create the tool bar window hwnd htool; hinstance hthisinstance = getmodulehandle (null); htool = createmediawex (0, toolbarclassname, null, ws_child | ws_visible | ccs_adjustable | ws_clipchildren, 0, 0, 0, 0, hparent, (hmenu) idc_main_tool, hthisinstance, null); If (htool = NULL) {: MessageBox (hparent, "cocould not create tool bar. "," error ", mb_ OK | mb_iconerror); Return false;} // send the tb_buttonstructsize message for backward compatibility considerations. sendmessage (htool, tb_buttonstructsize, (wparam) sizeof (tbbutton), 0); // create an image list himagelist himglist = imagelist_create (16,16, ilc_color32 | ilc_mask, 0, 0 ); // set the image list for the toolbar: sendmessage (htool, tb_setimagelist, 0, (lparam) himglist); // open the bitmap and add it to the image list tchar szmodulepath [_ max_path];: getmodulefilename (null, szmodulepath, _ max_path); STD: String strbinpath = szmodulepath; int imgid [2]; // obtain the bitmap file path strbinpath = strbinpath. substr (0, strbinpath. rfind ('\') + 1); STD: String strbmp Path = strbinpath + STD: string (_ T ("Toolbar \ entire.bmp"); hbitmaphbitmap = NULL; hbitmap = (hbitmap) LoadImage (hthisinstance, strbmp path. c_str (), image_bitmap, 0, 0, lr_defaultcolor | lr_loadfromfile); imgid [0] = imagelist_add (himglist, hbitmap, null); strbmp Path = strbinpath + STD :: string (_ T ("Toolbar \ grid.bmp"); hbitmap = NULL; hbitmap = (hbitmap) LoadImage (hthisinstance, strbmp path. c_str (), image_bitmap, 0, 0, lr_defaultcolor | lr_loadfromfile); imgid [1] = imagelist_add (himglist, hbitmap, null ); // initialize the toolbar button array 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; // send the message tbaddbitmap TBAB with the Add toolbar button;: sendmessage (htool, tb_addbuttons, sizeof (TBB)/sizeof (tbbutton), (lparam) & TBB ); // set the toolbar button size: 16*16: sendmessage (htool, tb_setbuttonsize, null, (lparam) makelong (16, 16); Return true ;}
As follows:
Author: clever101 posted on 23:48:05 Original article link Read: 527 comments: 3 views comments