[Windows programming] Series 6: Creating Toolbar, Statusbar, and toolbarstatusbar

Source: Internet
Author: User

[Windows programming] Series 6: Creating Toolbar, Statusbar, and toolbarstatusbar

HIMAGELIST ImageList_Create (int cx, int cy, UINT flags, int cInitial, int cGrow );

 

The function usage is clear on MSDN. After the list is created, there is no image. It is just a list-managed container and you need to load the image set. The APIS required for adding images are as follows:

int ImageList_AddMasked(HIMAGELIST himl, HBITMAP hbmImage, COLORREF crMask);

 

After an image is loaded, you can set the display attributes of the image, including the image display, text, and information prompting functions. The response of the tool button is implemented by sending the WM_COMMAND message to the window handler, and the user can process the button event. The prompt message in the toolbar is sent through WM_NOTIFY, which is set by the user. For details about how to create an instance in the toolbar, refer to the application instance.

  • Create Status Bar

The installation status bar is much simpler than the toolbar. After creating the status bar using createjavaswex, the status bar has only one display panel by default. You need to create multiple panels, send the SB_SETPARTS message to the status bar. The message parameters are the number of panels and the destination array of the Panel. For example:

int array[3]={120,120*2,-1};SendMessage(hWndStatus,SB_SETPARTS,(WPARAM)3,(LPARAM)array);

 

The status bar panel is divided into three parts, the first part is divided into 120 pixels, the second part is divided into 240 pixels, and the remaining (-1) is all divided into the third part.

Set panel text content. You can send SB_SETTEXT messages to the status bar. To add a small icon to the status bar, you can send SB_SETICON messages to the status bar.

The toolbar and status bar above only show the basic information. For more settings and message processing, refer to MSDN. This series focuses on basic programming and usage, but only introduces common usage. However, these usage can be used by beginners.

  • Use instance

The following describes how to create and use the toolbar and status bar using pure Windows APIs through a basic instance program. The Toolbar created in the program has three icons. When the program is displayed normally, move the mouse over a Toolbar button or is in Disable state, different images are displayed, you can change the icons you like.

# Include <windows. h> # include <commctrl. h> # include <tchar. h> # pragma comment (lib, "comctl32.lib") // Windows XP sytle button # pragma comment (linker, "\"/manifestdependency: type = 'win32 '"\" name = 'Microsoft. windows. common-Controls '"\" version = '6. 0.0.0 'processorarchitecture = '*' "\" publicKeyToken = '6595b64144ccf1df 'language =' * '\ "") # define IDC_TOOLBAR 1001 # define IDC_STATUSBAR 1002 // # define PIC_RESOURCE_USED # Ifdef latest # define IDB_NEW 110 # define IDB_OPEN 111 # define IDB_SAVE 112 # endif # define ID_FOPEN 1111 # define ID_FCLOSE 1112 # define ID_FSAVE 1113 static TCHAR szAppName [] = TEXT ("toolbar" ); static lresult callback (HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) {HWND hWnd; MSG msg; WNDCLASS wndclass; wndclass. Style = CS_HREDRAW | CS_VREDRAW; wndclass. lpfnWndProc = WndProc; wndclass. cbClsExtra = 0; wndclass. cbWndExtra = 0; 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;} // initialize the public space INITCOMMONCONTROLSEX icc; icc. dwSize = sizeof (INITCOMMONCONTROLSEX); icc. dwICC = ICC_BAR_CLASSES; InitCommonControlsEx (& icc); hWnd = CreateWindow (szAppName, // window class name szAppName, // window caption WS_OVERLAPPEDWINDOW, // window style CW_USEDEFAULT, // initial x position CW_USEDEFAULT, // initial y position 400, // initial x size 300 ,// Initial y size NULL, // parent window handle NULL, // window menu handle hInstance, // program instance handle NULL); // creation parameters ShowWindow (hWnd, iCmdShow ); updateWindow (hWnd); while (GetMessage (& msg, NULL, 0, 0) {TranslateMessage (& msg); DispatchMessage (& msg);} return msg. wParam;} HWND createconlbar (HWND hParentWnd) {HWND hWndTB; TBBUTTON tbb [3]; HIMAGELIST hImageList, hHotImageList, h DisableImageList; HBITMAP hBitmap; HINSTANCE hInst = GetModuleHandle (NULL); // create a Toolbar control hWndTB = createjavaswex (0, TOOLBARCLASSNAME, TEXT (""), WS_CHILD | WS_VISIBLE | WS_BORDER | TBSTYLE_LIST | TBSTYLE_AUTOSIZE | TBSTYLE_TOOLTIPS, hParentWnd, (HMENU) IDC_TOOLBAR, hInst, NULL); if (! HWndTB) {return NULL;} SendMessage (hWndTB, TB_BUTTONSTRUCTSIZE, (WPARAM) sizeof (TBBUTTON), 0); // create a list of three groups of 24x24 pixel bitmap images, hImageList = toolbar (24, 24, ILC_COLOR24 | ILC_MASK, 3, 1); # ifdef export hBitmap = LoadBitmap (hInst, MAKEINTRESOURCE (IDB_COLOR3); # else hBitmap = (HBITMAP) loadImage (NULL, TEXT ("color24x3.bmp"), IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION); // load a group Image # endif ImageList_AddMasked (hImageList, hBitmap, RGB (255,255,255); DeleteObject (hBitmap); SendMessage (hWndTB, TB_SETIMAGELIST, 0, (LPARAM) hImageList ); // The image list hHotImageList = ImageList_Create (24, 24, ILC_COLOR24 | ILC_MASK,); # ifdef export hBitmap = LoadBitmap (hInst, MAKEINTRESOURCE (IDB_GREEN3 )); # else hBitmap = (HBITMAP) LoadImage (NULL, TEXT ("green24x3.bmp"), IMAGE_BITMAP, 0, 0, LR_LOA DFROMFILE | LR_CREATEDIBSECTION); # endif locate (hHotImageList, hBitmap, RGB (255,255,255); DeleteObject (hBitmap); SendMessage (hWndTB, success, 0, (LPARAM) hHotImageList ); // image list when hovering the cursor (hDisableImageList = ImageList_Create (24, 24, ILC_COLOR24 | ILC_MASK, 3, 1); # ifdef export hBitmap = LoadBitmap (hInst, MAKEINTRESOURCE (IDB_GRAY3 )); # else hBitmap = (HBITMAP) LoadImage (NULL, TEXT ("gra Y24x3.bmp "), IMAGE_BITMAP, 0, 0, latency | LR_CREATEDIBSECTION); # endif transform (hDisableImageList, hBitmap, RGB (255,255,255); DeleteObject (hBitmap); SendMessage (hWndTB, latency, latency, 0, (LPARAM) hDisableImageList); // The image list ZeroMemory (tbb, sizeof (tbb) when the toolbar button fails; tbb [0]. iBitmap = MAKELONG (0, 0); tbb [0]. fsState = TBSTATE_ENABLED; tbb [0]. fsStyle = TBSTYLE_BUTTON | BTNS_AUTOSI ZE; tbb [0]. idCommand = ID_FOPEN; tbb [0]. iString = (INT_PTR) TEXT ("open"); tbb [1]. iBitmap = MAKELONG (1, 0); tbb [1]. fsState = TBSTATE_ENABLED; tbb [1]. fsStyle = TBSTYLE_BUTTON | BTNS_AUTOSIZE; tbb [1]. idCommand = ID_FCLOSE; tbb [1]. iString = (INT_PTR) TEXT ("close"); tbb [2]. iBitmap = MAKELONG (2, 0); tbb [2]. fsState = TBSTATE_ENABLED; tbb [2]. fsStyle = TBSTYLE_BUTTON | BTNS_AUTOSIZE; tbb [2]. idCommand = ID_FSAVE; tbb [2]. I String = (INT_PTR) TEXT ("save"); SendMessage (hWndTB, TB_ADDBUTTONS, sizeof (tbb)/sizeof (TBBUTTON), (LPARAM) & tbb ); // configure the toolbar button information SendMessage (hWndTB, WM_SIZE,); return hWndTB;} HWND CreateStatusBar (HWND hParentWnd) {# define PANEL_NUM 3 int array [PANEL_NUM] = {120,120*2,-1}; HINSTANCE hInst = GetModuleHandle (NULL); // create the Statusbar control HWND hWndStatus = create1_wex (0, STATUSCLASSNAME, TEXT (""), WS_CHILD | WS_BOR DER | WS_VISIBLE, 0, 0, 0, 0, hParentWnd, (HMENU) IDC_STATUSBAR, hInst, NULL); if (hWndStatus) {SendMessage (hWndStatus, SB_SETPARTS, (WPARAM) PANEL_NUM, (LPARAM) array); // set the number of panel SendMessage (hWndStatus, SB_SETTEXT, (LPARAM) 1, (WPARAM) TEXT ("panel-1 ")); // set SendMessage (hWndStatus, SB_SETTEXT, (LPARAM) 2, (WPARAM) TEXT ("panel-2") in the second panel ")); // set the third panel content} # undef PANEL_NUM return hWndStatus;} static lresult callback Wnd Proc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hDC; PAINTSTRUCT ps; static HWND hToolbar; static HWND hStatusbar; switch (message) {case WM_CREATE: hToolbar = createmedilbar (hWnd); hStatusbar = CreateStatusBar (hWnd); return 0; case WM_COMMAND: {int wmId = LOWORD (wParam); int wmEvent = HIWORD (wParam ); // select switch (wmId) {case ID_FOPEN: SendMessage (hToolbar, TB_ENABLEBUTTON, (WPARAM) ID_FOPEN, (LPARAM) MAKELONG (FALSE, 0); SendMessage (hToolbar, TB_ENABLEBUTTON, (WPARAM) ID_FSAVE, (LPARAM) MAKELONG (TRUE, 0 )); break; case ID_FSAVE: SendMessage (hToolbar, TB_ENABLEBUTTON, (WPARAM) ID_FSAVE, (LPARAM) MAKELONG (FALSE, 0); SendMessage (hToolbar, TB_ENABLEBUTTON (WPARAM) ID_FOPEN, (LPARAM) MAKELONG (TRUE, 0); break; case ID_FCLOSE: MessageBox (hWnd, TEXT ("click! "), TEXT (" hint "), MB_ OK); break ;}return 0; case WM_NOTIFY: {LPNMHDR lpnmhdr = (LPNMHDR) lParam; LPTOOLTIPTEXT lpttext; if (lpnmhdr-> code = TTN_GETDISPINFO) {// handle the text prompt lpttext = (LPTOOLTIPTEXT) lParam when the mouse is floating on the toolbar; switch (lpttext-> hdr. idFrom) {case ID_FOPEN: lpttext-> lpszText = TEXT ("Open File"); break; case ID_FCLOSE: lpttext-> lpszText = TEXT ("Close file"); break; case ID_FSAVE: lpttext-> lpszText = TEXT ("Save As file"); break ;}} return 0; case WM_SIZE: {SendMessage (hToolbar, TB_AUTOSIZE, 0, 0 ); sendMessage (hStatusbar, WM_SIZE, 0, 0);} return 0; case WM_MOUSEMOVE: {TCHAR szBuf [MAX_PATH]; _ stprintf (szBuf, TEXT ("Mouse (% d, % d) "), LOWORD (lParam), HIWORD (lParam); SendMessage (hStatusbar, SB_SETTEXT, 0, (LPARAM) (LPSTR) szBuf);} return 0; case WM_PAINT: hDC = BeginPaint (hWnd, & ps); EndPaint (hWnd, & ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0 ;} return DefWindowProc (hWnd, message, wParam, lParam );}

After the instance program, the result is displayed as follows:

After you press the "open" button on the toolbar of the program, it becomes Disable. After you click "save", the "open" button is activated again. As you can see, when you move the mouse over the "save" button, a text prompt indicating "Save As file" appears.

The status bar has three panels, and the last two are statically assigned strings at creation. The first is to capture and display the coordinates of the mouse in the customer zone in real time.

In general, the basic usage of the toolbar and status bar is relatively simple, but because these two controls are common controls of Microsoft, you need to call InitCommonControlsEx to initialize the universal control library and set the controls to be used before creation. This article is written here. If you are interested, please continue to pay attention to the subsequent articles in the basic series of Windows programming.

 

Public interest platform: coder_online (coder_online) allows you to obtain original technical articles and become a friend of java/C ++/Android/Windows/Linux, exchange programming experience online, obtain basic programming knowledge, and solve programming problems. Programmer InterAction alliance, developer's own home.

Please indicate the source for reprinting. Thank you for your cooperation!

Related Article

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.