SDK implements window programs with toolbar and status bar

Source: Internet
Author: User
Tags first string

Create a toolbar

Two functions can be used, createconlbarex or createmediawex. The latter is used here. To use createmediawex, you must specify the window class as toolbarclassname. The Initialization is not including any buttons. Msdn also said that it is a common control and needs to be initialized using initcommoncontrolsex. You can add a button with two messages. Tb_addbuttons and tb_insertbutton are used respectively. Finally, you need to send the tb_autosize message. When you insert some buttons or strings to the toolbar, the toolbar needs to re-estimate its size.

Msdn:

You can use two functions to create a toolbar-createrelbarex or createmediawex. the createmedilbarex function creates a toolbar and adds an initial set of buttons to it. you can also use createpolicwex, specifying the toolbarclassname window class to create
A toolbar that initially contains no buttons. the toolbarclassname window class is registered when the common control DLL is loaded. to ensure that this DLL is loaded, use the initcommoncontrolsex function first. if you create an empty toolbar, you add buttons
To it by using the tb_addbuttons or tb_insertbutton message. you must send the tb_autosize message after all the items and strings have been inserted into the control to cause the toolbar to recalculate its size based on its content.

Specify the size and position of the toolbar

Ccs_top and ccs_bottom determine whether the toolbar is above or below the customer zone. By default, the toolbar has the ccs_top style.

The tb_style_tooltips toolbar creates and manages a tooltip control, which is a small pop-up window that contains a description of the buttons on the toolbar. It is hidden at first. It takes about one second to move the mouse to a toolbar button. It is displayed near the cursor. When the toolbar receives the wm_mousemove message, it sends a notification message to the tooltip control. The notification message is ttn_getdispinfo.

The program can directly send the tb_gettooltips message to obtain the handle of the tooltip control. You can also use the tb_settooltips message to replace one tooltip control with another tooltip control.

There are also many other styles, such as ccs_noresize, ccs_noparentalign ...... and so on.

If createmedilbarex is used to create a toolbar, you can specify the width and height of the toolbar (in pixels) in the parameter. It is no longer convenient to use createmediawex, the Window Process of the toolbar automatically adjusts its size and position. The height is based on the button height on the toolbar. The width is the same as the width of the customer zone. If you want to change the automatic settings, you need to use the tb_setbuttonsize message.

Msdn:

If you create a toolbar using createconlbarex, the function enables you to specify in pixels the height and width of the toolbar. however, the createmediawex function does not have parameters for specifying toolbar size. the toolbar window procedure automatically
Sets the size and position of the toolbar window. the height is based on the height of the buttons in the toolbar. the width is the same as the width of the parent window's client area. to change the automatic size settings, send a tb_setbuttonsize message.
The ccs_top and ccs_bottom common control styles determine whether the toolbar is positioned along the top or bottom of the client area. By default, a toolbar has the ccs_top style.
 

It is very important that the toolbar window will automatically adjust its size when it receives the wm_size or tb_autosize message.

 

Use bitmap to define button images

Each button on the toolbar contains a bitmap image, which is stored in an internal list. You can use the tb_addbitmap message to add images.

Each image has a 0-based index. The first image is 0, and then increases sequentially. The tb_addbitmap message adds an image to the end of the image list in the toolbar and returns the index of the first added image. You can send a tb_addbuttons message to associate an image with a button.

Msdn:

Each image has a zero-based index. the first image added to the internal list has an index of 0, the second image has an index of 1, and so on. tb_addbitmap adds images to the end of the list and returns the index of the first new image that it added. to
Associate the image with a button, you must send a tb_addbuttons message and specify the image's index after you add bitmaps to the internal image list.
Define text for button

Each button can display a string. The list maintained inside the toolbar also contains all the string variables used on the toolbar button. You can add the tb_addstring message to the list...

The tb_addstring method is not the only way to add a string to the toolbar. It can also display the istring field of a string pointer in the tbbutton struct. This struct is finally passed to tb_addbuttons. In addition, you can assign values to the toolbar buttons using tb_setbuttoninfo. For more information, see msdn.

Each string has a zero-based index. the first string added to the internal list of strings has an index of 0, the second string has an index of 1, and so on. tb_addstring adds strings to the end of the list and returns the index of the first new string.
You use a string's index to associate the string with a button.

Using tb_addstring is not the only way to add strings to a toolbar. you can display a string in a button by passing a string pointer in the istring field of the tbbutton structure that is passed to tb_addbuttons. additionally, you can use tb_setbuttoninfo
To assign text to a toolbar button. For more information, see assigning text to a toolbar button.

Add button on toolbar

The createmedilbarex method fills the tbbutton struct array as required. The createmediawex creates an empty toolbar and needs to send the tb_addbuttons message to fill it.

If you use the createmedilbarex function to create a toolbar, you can add buttons to the toolbar by filling an array of tbbutton structures and specifying the address of the array in the function call. however, the createmediawex function does not have
Parameter for passing a tbbutton structure. createappswex creates an empty toolbar that you fill by sending a tb_addbuttons message, specifying the address of a tbbutton structure.
Add another message:

Tb_buttonstructsize

If an application uses the createmediawex function to create the toolbar, the application must sends this message to the toolbar before sending the tb_addbitmap or tb_addbuttons
Message. The createconlbarex function automatically sends tb_buttonstructsize, and the size of the tbbutton structure is a parameter of the Function

The status bar below

Create

The two functions createstatuswindow, create‑wex + statusclassname window class. It is also a public control and requires initcommoncontrols.

Style Type

The default status bar follows its parent window, but the ccs_top style can be specified to display it in the customer area.

Size and height

The window procedure for the status bar automatically sets the initial size and position of the window, ignoring the values specified in the createmediawex function. the width is the same as that of the parent window's client area. the height is based on
The metrics of the font that is currently selected into the status bar's device context and on the width of the window's borders.

The window procedure automatically adjusts the size of the status bar whenever it has es a wm_size message. Typically, when the size of the parent window changes, the parent sends a wm_size message to the status bar.

Column in the status bar

You can use the sb_setparts message column. A maximum of 256 columns can be used to obtain the number of sb_getparts messages.

To write text to a column, you need to use the sb_settext message. For details, refer to msdn.

A status bar can have your different parts, each displaying a different line of text. you divide a status bar into parts by sending the window an sb_setparts message, specifying the number of parts to create and the address of an integer array. the array
Contains one element for each part, and each element specifies the client coordinate of the right edge of a part.

A status bar can have a maximum of 256 parts, although applications typically use far fewer than that. you retrieve a count of the parts in a status bar, as well as the coordinate of the right edge of each part, by sending the window an sb_getparts message.

Code:

/* ------------------------------------------------------------ Hellowin. c -- displays "Hello, Windows 98! "In client area (c) Charles Petzold, 1998 ------------------------------------------------------------ */# include <windows. h> # include <commctrl. h> # pragma comment (Lib, "Warn") # define BTN 1 # define idc_main_tool 1111 # define id_file_new 1200 # define id_file_open 1201 # define limit 1202 lresult callback wndproc (hwnd, uint, wparam, lparam); lresult callback buttonwndproc (hwnd, uint, wparam, lpar Am); wndproc oldwndproc; hwnd hbtn; hinstance hinst; int winapi winmain (hinstance, hinstance hprevinstance, pstr szcmdline, int icmdshow) {static tchar szappname [] = text ("hellowin"); hwnd; MSG; wndclass; initcommoncontrolsex initctrlex; initcommoncontrolsex (& initctrlex); wndclass. style = cs_hredraw | cs_vredraw; wndclass. lpfnwndproc = wndproc; wndclass. cbclsextra = 0; wndcia SS. cbwndextra = 0; hinst = wndclass. hinstance = hinstance; wndclass. hicon = loadicon (null, idi_application); wndclass. hcursor = loadcursor (null, idc_arrow); wndclass. hbrbackground = (hbrush) getstockobject (white_brush); wndclass. lpszmenuname = NULL; wndclass. lpszclassname = szappname; If (! Registerclass (& wndclass) {MessageBox (null, text ("this program requires Windows NT! "), Szappname, mb_iconerror); Return 0;} hwnd = createwindow (szappname, // window class nametext (" the hello program "), // window captionws_overlappedwindow, // window stylecw_usedefault, // initial X positioncw_usedefault, // initial y positioncw_usedefault, // initial X sizecw_usedefault, // initial y sizenull, // parent window handlenull, // Window Menu handlehinstance, // program instance hand Lenull); // creation parametersshowwindow (hwnd, icmdshow); updatewindow (hwnd); While (getmessage (& MSG, null, 0, 0) {translatemessage (& MSG ); dispatchmessage (& MSG);} return MSG. wparam;} lresult callback buttonwndproc (hwnd, uint message, wparam, lparam) {return callwindowproc (oldwndproc, hwnd, message, wparam, lparam );} lresult callback wndproc (hwnd, uint message, wparam, Lparam) {HDC; paintstruct pS; rect; tchar buttonname [] = text ("I Am a button"); static hwnd hwndstatus, hwndtool; tchar szbuf [max_path]; int ncount = 3; int array [3] = {250,500,-1}; static tbbutton TBB [3]; static tbaddbitmap TBAB; lpnmhdr; lptooltiptext lpttext; Switch (Message) {Case wm_create: hwndstatus = createmediawex (0, statusclassname, "", ws_child | ws_border | ws_visible,-100,-, 10, 10, hwnd, (hmenu) 1 00, hinst, null); If (! Hwndstatus) MessageBox (hwnd, text ("can't create statusbar! "), Text (" error_policy "), mb_ OK); sendmessage (hwndstatus, sb_setparts, (wparam) ncount, (lparam) array); sendmessage (hwndstatus, sb_settext, (lparam) 1, (wparam) text ("I am Status Bar"); sendmessage (hwndstatus, sb_settext, (lparam) 2, (wparam) text ("by yiruirui ")); hwndtool = createjavaswex (0, toolbarclassname, null, ws_child | ws_visible | tbstyle_tooltips, 1111, hwnd, (hmenu), hinst, null); sendmessage (hwndtool, tb_buttonstr Uctsize, (wparam) sizeof (tbbutton), 0); TBAB. hinst = hinst_commctrl; TBAB. nid = idb_std_small_color; If (sendmessage (hwndtool, tb_addbitmap, 1, (lparam) & TBAB) =-1) MessageBox (hwnd, text ("failed "), text ("CREATE"), 0); 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 [0]. istring = (int_ptr) "Yi"; TBB [1]. ibitmap = std_fileopen; TBB [1]. f Sstate = tbstate_enabled; TBB [1]. fsstyle = tbstyle_button; TBB [1]. idcommand = id_file_open; TBB [1]. istring = (int_ptr) "Rui"; TBB [2]. ibitmap = std_filesave; TBB [2]. fsstate = tbstate_enabled; TBB [2]. fsstyle = tbstyle_button; TBB [2]. idcommand = id_file_saveas; TBB [2]. istring = (int_ptr) "Rui"; sendmessage (hwndtool, callback, sizeof (TBB)/sizeof (tbbutton), (lparam) & TBB); sendmessage (hwndtool, tb_setbuttonsize, 0, (lparam) makelong (3 5, 35); sendmessage (hwndtool, tb_autosize, 0, 0); Return 0; Case wm_size: {hwnd htool; htool = getdlgitem (hwnd, idc_main_tool); sendmessage (htool, tb_autosize, 0, 0); movewindow (hwndstatus, 0, 0, 0, true);} break; casewm_mousemove: wsprintf (szbuf, "mouse position: % d, % d", loword (lparam ), hiword (lparam); sendmessage (hwndstatus, sb_settext, 0, (lparam) (lpstr) szbuf); break; Case wm_drawitem :{} break; Case wm_paint: HDC = beginpaint (Hwnd, & PS); getclientrect (hwnd, & rect); drawtext (HDC, text ("Hello, Windows 98! "),-1, & rect, dt_singleline | dt_center | dt_vcenter); endpaint (hwnd, & PS); Return 0; Case wm_command: Switch (loword (wparam )) {Case id_file_new: MessageBox (hwnd, text ("Y"), text ("file new"), 0); break; Case id_file_open: MessageBox (hwnd, text ("Y"), text ("file open"), 0); break; Case id_file_saveas: MessageBox (hwnd, text ("notify "), text ("File Save as"), 0); break;} break; Case wm_policy: lpnmhdr = (lpnmhdr) lparam; If (lpnmhdr-> code = ttn_getdispinfo) {lpttext = (lptooltiptext) lparam; Switch (lpttext-> HDR. idfrom) {Case id_file_new: lpttext-> lpsztext = "new"; break; Case id_file_open: lpttext-> lpsztext = "open"; break; Case id_file_saveas: lpttext-> lpsztext = "Save as"; break ;}} break; Case wm_destroy: postquitmessage (0); Return 0;} return defwindowproc (hwnd, message, wparam, lparam );}

But some relatively simple functions, more complicated, need to read msdn carefully.

Post-run:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.