VC ++ Design for Tools

xiaoxiao2021-04-10  535

High color toolbar under VC

introduction

Some Windows systems comes with the toolbar of the resource manager, Internet Explorer, and the toolbar of the Internet, which looks not too, there are many tools that have other programs. In fact, the icons on the toolbars of these programs are 256 colors, and the color of the common application is usually only 16 colors on the toolbar, which determines that the latter is very beautiful. Since the procedure with the system with the system is also written by the developer, this indicates that the display can implement 256 colors or even more color icons on the toolbar by program coding. For this purpose, the high color toolbar is implemented in the application through the MFC programming. The main method of implementation will be introduced as follows to the majority of readers.

Basic design ideas

Before achieving a high color toolbar, study the implementation process of the general 16-color toolbar and summarize the improvement method. In the resource view of the VC, the toolbar is a Toolbar-type resource named IDR_MAINFRAME and can complete the icon icon on the toolbar by icons on the editing button. Although the icon of each button on the toolbar in the resource view is independent of each other, it is not saved as an ICO format file as an icon when it is stored, but is saved on the disk in the BMP bitmap format. This bitmap is a long-type bitmap image consisting of a button icon on the toolbar. There is no gap in the middle, and the image is loaded into an image list when the program runs and edits the toolbar on the resource view, and then The toolbar is sequentially displayed from the image list to the various buttons of the toolbar according to the index. Since the icon on the VC Limit Toolbar cannot exceed 16 colors, whether it is a toolbar that is more than 256 colors that are not able to edit the bitmap in the resource view or by copying paste (Note: Although the method with copy paste is edited The 256-color icon can be temporarily displayed, but it is still degraded into 16 colors when the program is running.

Since the icon of 16-color Toolbar cannot be implemented by editing the Toolbar resource in the resource view, the tool bar is not directly acquired from the Toolbar to read the icon from the Toolbar, so you can do it through other image processing software. Similar to the BMP image of the toolbar (only color than the ordinary toolbar BMP image, the rest is exactly the same), and adds to the program resources in a bitmap. When using, first read it to the image list, such image listing can be used to display the icon on the toolbar or 256, 24 bits, or even 32 colors. Since the toolbar defaults will directly load the resources called IDR_MAINFRAME as the source of the icon, it must also be specified as an icon source with a high color toolbase graph through the setimageList () function.

Realization of true color tools

Since the creation of the toolbar is done in the oncreate () function of the main framework class, the replacement of the load and image list of high color images must be treated here. Before proceeding, you need to do a variety of preparations, such as the drawing of high color tools, and high color bitmaps are added to resources. When drawing the toolbelogram, you must control the size of the image. If you need N edge length M, you need to draw the bitmap size to be long = N * m; width = m. The true color graph cannot be edited in the VC's resource view after joining the project. Since this color bitmap only plays only the role of beautification interface, the specific pair of event responses will be completed by setting the original Toolbar resources.

After you are ready, you must first load the toolbar map to the image list so that you can get it by the toolbar. When making this step, you must use the: LoadImage () function to load the toolbits, and specify which resource bitmap you want to load through macro makeintresource ():

Hbitmap HBM = (Hbitmap) :: loadImage (AfxGetInstanceHandle (), makeintResource (IDB_TOOLBAR), // Load IDB_TOOLBAR

Image_bitmap, // Bit map format

0,0, // CX, CY

Lr_createdibsection | LR_LOADMAP3DCOLORS;

LoadImage returns a bitmaphe Hbitmap, but uses CBITMAPS under the MFC to be more convenient, you can create one

CBITMAP objects are connected to the bitmap handle with the attach () member function:

CBITMAP BM;

Bm.attach (HBM);

When the MFC load tool field map is used, an internal function AFXLOADSYSCOLORBITMAP () sets the default color to 16 colors, so in order to display images of 16-color or more, you must create an image list when calling an image list class cimagelist. Further processing for image list:

m_iltoolbar.create (32, 32, ILC_COLOR8, 4, 4);

m_iltoolbar.add (& BM, (CBitmap *) NULL);

Here, ILC_Color8 indicates that the created image list is 256 colors, and it is defined in the CommCtrl.h of the VC, and there is also a predefined predefined with several other color bits:

#define ilc_color4 0x0004 // 16 colors

#define ilc_color8 0x0008 // 256 colors

#define ilc_color16 0x0010 // 16 color

#define ilc_color24 0x0018 // 24 bit color

#define ilc_color32 0x0020 // 32 bit color

If the toolboot map used is only 256 colors (enough for most programs), it is clear that it is clear that the higher level of bit depth definition is used. The last step is also one of the most critical steps. You must specify the icon M_WndToolbar through the setimagelist () function. The source of the toolbar m_wndtoolbar is no longer the original default image list but contains a high color bitmap M_ILToolBar:

M_WndtoolBar.gettoolbarctrl (). setimagelist (& m_iltoolbar);

To this end, you can implement a beautiful toolbar similar to software such as IE in the program written in your own. The following figure is the program interface obtained by the author with the above method:

summary

This article enables a high color toolbar that has previously only Windows system comes with the previous Windows system with the previous WINDOWS system with the previous MFC applications by replacing a list of images from the source of the toolbar icon. It is better to beautify the interface interface. This article is compiled by Microsoft Visual C 6.0 under Windows 98.

Cool toolbar with VC

After Microsoft launched Windows 95, a large number of new controls make our app more beautiful and use more convenient. One of the significant changes is that the toolbar is no longer a prominent 3D small box, but it turns into a flat state, but as long as the mouse moves to the top, it will automatically float, which greatly facilitates the user.

After a period of exploration, the author finally found the method of making this tool bar. It turns out that Windows 95 encapsulates many commonly used controls, mostly in Comctrl32.dll, where the Toolbar control is used to make tool bars. The following brief introduces how to add a TOOLBAR in VC5.0.

As we all know, all controls are a type of window, so making Toolbar will start from the production window. Since the MFC's Toolbar class does not support new features, we have to use the SDK method to complete the entire process via the API call, which is similar to that makes a traditional toolbar.

Toolbar is an extension that belongs to Comctrl32.dll, so you have to call the INITCOMMONCONTROLSEX () function. This function has an important parameter that determines support for Toolbar, which is the registration Toolbar window so that this window is created in later programs, while the normal toolbar wants to call ITCOMMANDControls (). It should be noted that the writing of these two functions. ICMMONTROLSEX ICEX;

DWORD DWSTYLE;

Icex.dwsize = sizeof (INITCOMMONCONTROLSEX);

// Note The following two parameters determine if the registration Toolbar

ICEX.DWICC = ICC_COOL_CLASSES | ICC_BAR_CLASSES;

INITCOMMONCONTROLSEX (& ICEX);

Then you can call the CreateWindowEx function to create a Toolbar window:

HWND HWNDTB = CREATEWINDOWEX (

WS_EX_TOOLWINDOW, / / ​​Extension Tool Style

ToolbarclassName, // Toolbar class name

NULL,

WS_CHILD | WS_VISIBLE | TBSTYLE_FLAT, // Window Style

0, 0, 0, 0, //

AFXGetApp () -> getMainWnd (), // Father window name

NULL,

AFXGETITISTANCEHANDLE (), // instance

NULL);

Judging the window handle, if it is not empty, it means that the window has successfully established. The Toolbar at this time is just an empty window, we can add a button to the inside as needed. The addition of the button to Toolbar is implemented by sending a message to it, the following procedure is basically consistent with the production of the traditional toolbar. First, create an ImageList control, then determine the type of each button by defining the data structure of the button.

// Establish an ImageList control,

HWND HIML;

// myicon_cx, myicon_cy is the size of each button

HiML = imagelist_create (myicon_cx, myicon_cy, ilc_color4, 0, 4);

/ / Add in advance work bar graph idb_bitmap2

ImageList_add (HiML,

Loadbitmap (AfxgetInstanceHandle (), makeintResource (idb_bitmap2)), null);

// Add a bitmap to Toolbar by message

SendMessage (HWNDTB, TB_SETIMAGELIST, 0, (LPARAM) HIML);

The following is added to the 5 ordinary buttons:

TBButton TBARRAY [5]; // Data structure of the button

For (i = 0; i <5; i )

{

TBARRAY [I] .IBITMAP = i; // 第 位 个 图

TBARRAY [i] .idcommand = IDM_BUTTONSTART i;

// Command ID

TBARRAY [i] .fsState = TBSTATE_ENABLED;

TBARRAY [I] .fsstyle = TBStyle_Button;

// button style

TBARRAY [I] .dwdata = 0;

TBARRAY [I] .Inse = i; // Display string

}

/ / Set the size of the button structure

:: SendMessage (HwndTb, Tb_ButtonStructSize, Sizeof (TBButton), 0);

// Add button to Toolbar

:: SendMessage (HWNDTB, TB_ADDBUTTONS, (UINT) 5, (LPARAM) TBARRAY

At this point, a cool toolbar basically makes completed, and then calls the function showWindow (): showWindow (hwndtb, sw_showmaximized); When the button is clicked, Toolbar is transferred to the parent window, responds to the message by the parent window . The ID of the button in the Toolbar is included in the WPARAM parameter of the message function, which can set it to scheder different modules. At this time, you can overload the o nCommand () function of the parent window, and determine which button is clicked according to the WPARAM parameter. Assignment The father window is the main window frame, the code is as follows:

Bool CMAINFRAME :: OnCommand (WPARAM WPARAM, LPARAM LPARAM)

{

Switch (WPARAM)

{

Case IDM_ButtonStart 0:

AfxMessageBox ("You have a first button !!", MB_ICONInformation);

Break;

Case IDM_ButtonStart 1:

AfxMessageBox ("You have a second button !!", MB_ICONInformation);

Break;

CASE IDM_BUTTONSTART 2:

AfxMessageBox ("You have a third button !!", MB_ICONInformation);

Break;

}

Return CMAINFRAME :: OnCommand (WPARAM, LPARAM);

}

Visual C Version 6 New Features of the Toolsum

Dave Schmi

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 this 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 the original cause of ctoolbar only has the limited functionality 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_CheckGrou TBStyle_Group | TBStyle_Check

#if (_WIN32_IE $ # @ 62; = 0x0300)

#define TBStyle_Dropdown 0x0008

#ENDIF

#if (_WIN32_IE $ # @ 62; = 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 $ # @ 62; = 0x0300)

#define TBStyle_FLAT 0x0800

#define tbstyle_list 0x1000

#define TBStyle_Customerase 0x2000

#ENDIF

#if (_WIN32_IE $ # @ 62; = 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 Explorer3.0 introduces flat buttons, text tags, drop-down lists, and custom drawing. 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 dont want Tool 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 dont want the Toolbar 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 = // Stylews_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 have to include the CBRS_GRIPPer flag when calling the setbarstyle () 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 is really this problem, because once I add 3D logo to the next, I will explain 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.

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

New Post(0)