New features in the toolbar in VC6

zhaozj2021-02-08  226

New features in the toolbar in VC6

Add time: 01-1-8 09:10:32

New features in the Toolbar in Visual C Version 6

Author: Dave Schmitt

Microsoft has launched a Visual C 6.0 preview for several months in www.microsoft.com/visualc. Official version is expected to be released by the end of this year. At the same time, the preview version shows that publishing 6 will contain a lot of improvements and improvements, including support Internet controls, such as flat tool bars. Although the improved control pack is independent of the Internet, it first appears in Internet Explorer, so it is taken to do this. In fact, the title of the officially released preview is "Visual C 5.0 Technology Preview for Internet Explorer 4.0."

In the previous discussion topic for the MFC tools, I promised to provide a demonstration of the toolbar in version 6. There is a good news, that is, all the work you work in ctoolbar is valid in the new version, including some of the extensions described in the previous column. Therefore, you will easily modify the existing program to get the "cool" interface like Internet Explorer and Visual Studio. In addition, there is no bad news.

New features of the toolbar

As early as version 4, CToolbar has been fully implemented by the MFC library. Once the public control dynamic link library (named COMCTL32.DLL) has become inevitable, CToolBar has become synonymous with toolbar controls that have been included in the operating system. However, CToolBar does not disclose all the capabilities of the utility bar control. Today, it succeeded through the createex () function.

The public control dynamic link library now contains at least three types of style: initial, added in Internet Explorer 3.0 and adds in Internet Explorer 4.0. Although these versions are in theory, some professionals have written some applications that cannot be run in later versions, which may be that these programs have adopted some unusual functions, which are not Contains in all versions.

Visual C programmers do not have this experience, because ComctL32.dll in Visual C 4.0 or 5.0 is not a re-distributed component, which is updated when installing Internet Explorer, so the MFC programmer cannot rely on the latest version Some functions are used for their programs. This is why CToolBar has only the limited function of the initial DLL. CToolbar can achieve the latest features means that Microsoft will include the latest DLL in Visual C 6.0 and as a component that can be reissurable.

Most new features will be determined by the new style flags specified when calling createex () and other CToolBar members. Here is part of CommCtrl.h, which defines the TBStyle class identifier:

#define TBStyle_Button 0x0000

#define tbstyle_sep 0x0001

#define TBStyle_Check 0x0002

#define tbstyle_group 0x0004

#define TBStyle_CheckGroup (TBStyle_Group | TBStyle_Check)

#if (_WIN32_IE> = 0x0300)

#define TBStyle_Dropdown 0x0008

#ENDIF

#if (_WIN32_IE> = 0x0400)

#define TBStyle_autosize 0x0010 # Define TBStyle_noprefix 0x0020

#ENDIF

#define TBStyle_ToolTIPS 0x0100

#define TBStyle_Wrapable 0x0200

#define TBStyle_Altdrag 0x0400

#if (_WIN32_IE> = 0x0300)

#define TBStyle_FLAT 0x0800

#define tbstyle_list 0x1000

#define TBStyle_Customerase 0x2000

#ENDIF

#if (_WIN32_IE> = 0x0400)

#define tbstyle_registerdrop 0x4000

#define TBStyle_Transparent 0x8000

#define tbstyle_ex_drawddarrows 0x00000001

#ENDIF

You will notice some of these conditions, depending on the value of _win32_ie, which defaults to Internet Explorer 4.0 (ie, 0x0400). For Internet Explorer 3.0 (ie, value 0x0300), most TBStyle identifiers refer to buttons or a set of buttons. Internet Explorer 3.0 introduces flat buttons, text tags, drop-down lists, and custom drawings. Internet Explorer 4.0 enhances drop-down list and custom drawing function and adds support OLE drag to a toolbar.

Flat button and handle

In the past 18 months I often ask how to get a burst button as the toolbar like Internet Explorer and Visual Studio, but use flat buttons with a flat button with easy movement and positioning. These features are not supported by the MFC, so the simplest acquisition method is to buy an extension library. There is no need for Visual C 6.0 because it makes the CToolbar class to support the flattening button, handle and other new visual effects.

In the preview, AppWizard does not automatically include these new features, but they are easily joined. Table 1 shows the oncreate () function of the primary frame window created by AppWizard, which shows what modifications you need to do to get a toolbar with a flat button and handle. Figure 1 shows the toolbar created by Table 1, and Figure 2 shows the toolbar of Table 2.

Table 1: CMAINFRAME :: OnCreate As Generated by AppWizard

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cmdiframewnd :: oncreate (lpcreateStruct) == -1)

Return -1;

IF (! m_wndtoolbar.create (this) ||! m_wndtoolbar.loadtoolbar (iDR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

IF (! m_wndstatusbar.create (this) ||

M_WndStatusBar.setindicators (Indicators,

SIZEOF (Indicators)

figure 1

Table 2: adding flat buttons and the gripper

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct) {

IF (cmdiframewnd :: oncreate (lpcreateStruct) == -1)

Return -1;

IF (! m_wndtoolbar.createex (this) ||! m_wndtoolbar.loadtoolbar (iDR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

IF (! m_wndstatusbar.create (this) ||

M_WndStatusBar.setindicators (Indicators,

SIZEOF (INDICATORS) / SIZEOF (UINT))))

{

Trace0 ("Failed to Create Status Bar / N);

Return -1; // fail to create

}

// Todo: Remove this if you don't want Tips or a Resizeable Toolbar

m_wndtoolbar.setbarstyle (m_wndtoolbar.get barStyle () |

CBRS_GRIPPER | CBRS_BORDER_3D | CBRS_ToolTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

// Todo: Delete these Three Lines if you don't wantbar to

// be Dockable

m_wndtoolbar.enabledocking (CBRS_ALIGN_ANY);

Enabledocking (CBRS_ALIGN_ANY);

DockControlbar (& M_WndToolbar);

Return 0;

}

figure 2

In order to make a flat button, I must use createEx () instead of CREATE (). This new function is declared in AFXT.H:

Bool creteex

(

CWnd * pparentWnd, // Parent Window

DWORD dwctrlStyle = TBStyle_flat, // extended Style

DWORD DWSTYLE = // Style

WS_CHILD | WS_VISIBLE | CBRS_ALIGN_TOP,

CRECT RCBORDERS = CRECT (0, 0, 0), // Border Rectangle

UINT NID = AFX_IDW_TOOLBAR / / IDENTIFIER

);

Because the default refers to the default refers to TBStyle_flat, I have to get a flat button only need to simply change create () in the code formed by AppWizard to createEx (). I will implement other extension styles later.

In order to get the handle, I must include the CBRS_GRPER flag when calling the setbarsty () function, see Table 2. This is a new style of the CControlbar class, while the CToolbar class is inherited from it. Please note that I also joined the CBRS_Border_3D flag, which is to correct an unknown drawing problem, which will draw some extra points at the edge of the toolbar. This may mean that the preview does have this problem, because once I add 3D logo to join, I will solve it immediately and don't seem to affect anything else.

The two simple changes made above are the most labor-saving methods for causing a saved program to get the Cool interface. While a program has a flat button and handle, it does not have other changes that should not occur. Text label

Internet Explorer makes the ordinary toolbar have a large button and a text tag that replaces the text prompt. The MFC programmer can set a text string for each button through the setButtonText () function to get this effect. Although this function has been included in Visual C 5.0, it will not achieve satisfactory results if the flat button is not used.

Table 3 shows how to use an existing text prompt as a button label, and Figures 3 and 4 show the effects of positioning the toolbar at the top and right respectively. I still make the text prompt valid, but you can make it invalid by removing the CBRS_Tooltips style when calling setbarstyle ().

Table 3: Adding text labels

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cmdiframewnd :: oncreate (lpcreateStruct) == -1)

Return -1;

IF (! m_wndtoolbar.createex (this) ||

! m_wndtoolbar.loadtoolbar (idR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

IF (! m_wndstatusbar.create (this) ||

! m_wndstatusbar.setindicators (Indicators, Sizeof (Indicators) / Sizeof (UINT)))

{

Trace0 ("Failed to Create Status Bar / N);

Return -1; // fail to create

}

// Todo: Remove this if you don't want Tips or a Resizeable Toolbar

m_wndtoolbar.setbarstyle (m_wndtoolbar.get barStyle () |

CBRS_GRIPPER | CBRS_BORDER_3D | CBRS_ToolTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

// Add text to each button

For (int i = 0; i

{Uint id = m_wndtoolbar.getItemId (i);

CString S;

IF (! s.loadstring (id)) Continue;

INT j = s.find (_t ('/ n'));

IF (j <0) continued;

S = s.right (s.getLength () - J - 1);

m_wndtoolbar.setButtonTontext (i, s);} // adjust sizz to include text

CRECT RECT;

m_wndtoolbar.getitemRect (0, & Rect);

M_WndtoolBar.setsizes (Rect.SIZE (), CSIZE (16, 15)); // Todo: Delete these Three Lines if you don't want the toolbar to

// be Dockable

m_wndtoolbar.enabledocking (CBRS_ALIGN_ANY);

Enabledocking (CBRS_ALIGN_ANY);

DockControlbar; return 0;}

Figure 4

In order to generate a button label, a simple FOR loop swept through all buttons and extracts the prompt text from the help string connected thereto. After the tag of each button is set, the set of the toolbar is recalculated to recalculate the appearance of the toolbar to make the tag visible. Maybe there will be a better way to achieve the last step, but I still borrowed this process from Microsoft's MFCIE sample program, not doing, only for it.

In many ways, MFCIe is worth careful. In essence, it is actually a miniature version of Internet Explorer, and it also elaborated how to use some of the new features of version 6. You will be surprised to add HTML browsing features in your MFC application.

In most cases you want the text tag to display under the button (this is the only choice in the original public control library). The Internet Explorer version 3.0 joined the TBStyle_List style that caused the text tag to display the right side of the button. This style is useful for displaying a drop-down list or button next to the tag is covered by a sub-window.

drop-down list

In the article "Deformation of Tools", I demonstrate the case of adding a drop-down list on the toolbar by using a combo box as a sub-window. This method is still valid in version 6, and it is still useful when you want to display the current option on the toolbar. However, sometimes you want the toolbar to include a button that displays the selection list or menu when you click. This feature can now be implemented by extended style TBStyle_Dropdown and TBStyle_ex_drawdddarrows.

For example, suppose I want to correspond to the "New" button to display a list of all file types that I have created. Table 4 shows how to add this function in the previous example, and Figure 5 shows the toolbar. I grabbed the screen when the mouse staying on the button, which shows the separation between the button and the drop-down arrow.

In order to join a drop-down arrow, first, I must call the setExtendedStyle () function on the basic toolbar control, and specify the TBStyle_ex_drawddarrows. Then I must specify the TBStyle_Dropdown style on each button I want to display the drop-down arrow.

Table 4: Converting The File Menu New Command to a Drop-Down Button

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cmdiframewnd :: oncreate (lpcreateStruct) == -1)

Return -1;

IF (! m_wndtoolbar.createex (this) ||

! m_wndtoolbar.loadtoolbar (idR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

IF (! m_wndstatusbar.create (this) ||

! m_wndstatusbar.setindicators (Indicators, Sizeof (Indicators) / Sizeof (UINT)))

{

Trace0 ("Failed to Create Status Bar / N);

Return -1; // fail to create

}

// Todo: Remove this if you don't want Tips or a Resizeable Toolbar

m_wndtoolbar.setbarstyle (m_wndtoolbar.get barStyle () |

CBRS_GRIPPER | CBRS_BORDER_3D | CBRS_ToolTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC); // Add TEXT TO Each Button

For (int i = 0; i

Figure 5

When the user clicks on the New button, the basic button operation occurs as shown in Figure 5. In this example, the command message causes call cwinapp :: ONFileNew (). However, when the user clicks down the arrow, the toolbar will issue a TBN_DropDown notification message to the parent window of the toolbar. I can get this message by overnotify () function or by adding an on_notify in the message map.

Table 5 shows the latter implementation techniques. Usually I display a pop-up menu or a list box to respond to this notification message. In most cases, there is no need to provide a result value because the default (TBDDRET_DEFAULT) shows that I have acquired this event. Other possible results values ​​are TBDDRET_NODEFAULT and TBDDRET_TREATPRESSED. The former indicates that the event is not controlled. When the latter appears, the toolbar is as if the button is clicked.

Table 5: Handling Drop-Down Notifications

Begin_MESSAGE_MAP (CMAINFRAME, CMDIFRAMEWND)

// {{AFX_MSG_MAP (CMAINFRAME)

ON_WM_CREATE ()

//}} AFX_MSG_MAP

ON_NOTIFY (TBN_DROPDOWN, AFX_IDW_TOOLBAR, OONDROPDOWN)

END_MESSAGE_MAP ()

Void CMAINFRAME :: OnDropdown (nmhdr * pnotifystruct, lresult * result)

{

NMTOOLBAR * PINFO = (NMToolbar *) PNOTIFYSTRUCT;

Switch (Pinfo-> IIITEM)

{

Case id_file_new:

Trace0 ("ID_FILE_NEW DROP DOWN / N);

// Todo: Display Popup Menu Or List Box

Break;

// Todo: Add Cases for Other Drop-Down Buttons

}

}

Hot image

Run Internet Explorer 4.0 and scratch the mouse over the toolbar. I can notice that each button is plain color. When the mouse is touched to the button, he raised and showed a bright color. This visual effect uses the function of the "hotspot image" of the toolbar control.

The average AppWizard code uses toolbar resources to specify the appearance of the button on the button and a command ID connected to each button. The toolbar resource actually contains a composite bitmap that will be converted into an image list when calling the loadtoolbar () function (see Table 4). To activate a hot image function, I must provide the second image list through the sethotimagelist () function, as shown in Table 6.

Table 6: Using Hot images

INT CMAINFRAME :: OnCreate (lpcreatestruct lpcreatestruct)

{

IF (cmdiframewnd :: oncreate (lpcreateStruct) == -1)

Return -1;

IF (! m_wndtoolbar.createex (this) ||

! m_wndtoolbar.loadtoolbar (idR_mainframe))

{

Trace0 ("Failed to Create Toolbar / N);

Return -1; // fail to create

}

CIMAGELIST IMG;

IF (iv.create (IDB_MAINFRAME, 16, 0, RGB (128, 128, 128)))) {

Trace0 ("Failed to Load Hot Images / N");

Return -1;

}

M_wndtoolbar.gettoolbarctrl (). sethotimagelist (& img);

img.detach ();

IF (! m_wndstatusbar.create (this) ||

! m_wndstatusbar.setindicators (Indicators, Sizeof (Indicators) / Sizeof (UINT)))

{

Trace0 ("Failed to Create Status Bar / N);

Return -1; // fail to create

}

// Todo: Remove this if you don't want Tips or a Resizeable Toolbar

m_wndtoolbar.setbarstyle (m_wndtoolbar.get barStyle () |

CBRS_GRIPPER | CBRS_BORDER_3D | CBRS_ToolTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

// Add text to each button

For (int i = 0; i

Image 6

Figure 6 shows the situation where hotspot images are activated. The mouse is staying on the New button, which is displayed as an empty document. Other buttons are flat and inert. In order to generate this improved toolbar, I will copy the toolbar.bmp under the res subdirectory to Toolbar1.bmp, and then insert it as a bitmap resource into the resource, and take the ID to IDR_MAINFRAME. Next, I edit the IDR_MAINFRAME toolbar, replace it with a very light color (such as a light gray). Finally, I joined the call to the function setimagelist () as shown in Table 6.

This example demonstrates the shortcut to add a hotspot image in a deposited toolbar, but he does not improve the look of the toolbar. If you want to achieve better results at this point, the MFCIe example contains a very good etch bitmap, which demonstrates what level visual effect can be achieved when you use very artistic images.

Other characteristics

The previous explanation demonstrates how you simply change the existing MFC program to get the function of the flat button, handle, drop-down list, and hotspot images. Visual C 6.0 also provides several other interested toolbar features, including custom drawing, OLE drag into the target and using Rebar (Sorry, not know what the word refers to what) container. These features will be the topic of future columns.

转载请注明原文地址:https://www.9cbs.com/read-3074.html

New Post(0)