Win32 API create rebar and toolbar Method

Source: Internet
Author: User

Note that the toolbar must have the ccs_noresize style. Otherwise, the position will be automatically set and the problem will occur.

// Rebar. cpp: defines the entry point of the application.
//

# Include "stdafx. H"
# Include "rebar. H"
# Include "commctrl. H"
# Pragma comment (Lib, "comctl32.lib ")

# Define max_loadstring 100

// Global variables:
Hinstance hinst; // current instance
Tchar sztitle [max_loadstring]; // Title Bar text
Tchar szwindowclass [max_loadstring]; // main window class name

// The Forward Declaration of the functions contained in this Code module:
Atom myregisterclass (hinstance );
Bool initinstance (hinstance, INT );
Lresult callback wndproc (hwnd, uint, wparam, lparam );
Int_ptr callback about (hwnd, uint, wparam, lparam );

Int apientry _ twinmain (hinstance,
Hinstance hprevinstance,
Lptstr lpcmdline,
Int ncmdshow)
{
Unreferenced_parameter (hprevinstance );
Unreferenced_parameter (lpcmdline );

// Todo: place the code here.
MSG;
Haccel hacceltable;

// Initialize the global string
Loadstring (hinstance, ids_app_title, sztitle, max_loadstring );
Loadstring (hinstance, idc_rebar, szwindowclass, max_loadstring );
Myregisterclass (hinstance );

// Execute application initialization:
If (! Initinstance (hinstance, ncmdshow ))
{
Return false;
}

Hacceltable = loadaccelerators (hinstance, makeintresource (idc_rebar ));

// Main message loop:
While (getmessage (& MSG, null, 0, 0 ))
{
If (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
}

Return (INT) msg. wparam;
}

//
// Function: myregisterclass ()
//
// Purpose: register the window class.
//
// Note:
//
// Only if you want
// This code matches "registerclassex" added to Windows 95"
// This function and its usage are required only when the Win32 system is compatible with the function. It is very important to call this function,
// So that the application can obtain the associated
// The "correct format" icon.
//
Atom myregisterclass (hinstance)
{
Wndclassex wcex;

Wcex. cbsize = sizeof (wndclassex );

Wcex. Style = cs_hredraw | cs_vredraw;
Wcex. lpfnwndproc = wndproc;
Wcex. cbclsextra = 0;
Wcex. cbwndextra = 0;
Wcex. hinstance = hinstance;
Wcex. hicon = loadicon (hinstance, makeintresource (idi_rebar ));
Wcex. hcursor = loadcursor (null, idc_arrow );
Wcex. hbrbackground = (hbrush) (color_window + 1 );
Wcex. lpszmenuname = makeintresource (idc_rebar );
Wcex. lpszclassname = szwindowclass;
Wcex. hiconsm = loadicon (wcex. hinstance, makeintresource (idi_small ));

Return registerclassex (& wcex );
}

//
// Function: initinstance (hinstance, INT)
//
// Purpose: Save the instance handle and create the Main Window
//
// Note:
//
// In this function, we save the instance handle in the global variable and
// Create and display the main program window.
//
Bool initinstance (hinstance, int ncmdshow)
{
Hwnd;

Hinst = hinstance; // store the instance handle in a global variable

Hwnd = createwindow (szwindowclass, sztitle, ws_overlappedwindow | ws_clipchildren,
Cw_usedefault, 0, cw_usedefault, 0, null, null, hinstance, null );

If (! Hwnd)
{
Return false;
}

Showwindow (hwnd, ncmdshow );
Updatewindow (hwnd );

Return true;
}

Hwnd createatoolbar (hwnd hwndparent)
{
Hwnd hwndtb;
Tbbutton TBB [3];
Tchar szbuf [16] = _ T ("test ");
Int icut, icopy, ipaste;
Initcommoncontrolsex icex;

// Ensure that the common control DLL is loaded.
Icex. dwsize = sizeof (initcommoncontrolsex );
Icex. dwicc = icc_bar_classes;
Initcommoncontrolsex (& icex );

// Create a toolbar.
Hwndtb = createjavaswex (0, toolbarclassname, null,
Ws_child | ccs_noresize | ccs_adjustable | ccs_nodivider | ccs_noparentalign, 0, 0, 0, 0, hwndparent,
Null, hinst, null );

// Send the tb_buttonstructsize message, which is required
// Backward compatibility.
Sendmessage (hwndtb, tb_buttonstructsize, (wparam) sizeof (tbbutton), 0 );

Szbuf [_ tcslen (szbuf) + 2] = 0; // double-null terminate.

Icut = (INT) sendmessage (hwndtb, tb_addstring, 0, (lparam) (lpstr) szbuf );
Icopy = (INT) sendmessage (hwndtb, tb_addstring, (wparam) 0, (lparam) (lpstr) szbuf );
Ipaste = (INT) sendmessage (hwndtb, tb_addstring, (wparam) 0, (lparam) (lpstr) szbuf );

// Fill the tbbutton array with button information, and add
// Buttons to the toolbar. The buttons on this toolbar have text
// But do not have bitmap images.
TBB [0]. ibitmap = I _imagenone;
TBB [0]. idcommand = 0x1001;
TBB [0]. fsstate = tbstate_enabled;
TBB [0]. fsstyle = btns_button;
TBB [0]. dwdata = 0;
TBB [0]. istring = icut;

TBB [1]. ibitmap = I _imagenone;
TBB [1]. idcommand = 0x1002;
TBB [1]. fsstate = tbstate_enabled;
TBB [1]. fsstyle = btns_button;
TBB [1]. dwdata = 0;
TBB [1]. istring = icopy;

TBB [2]. ibitmap = I _imagenone;
TBB [2]. idcommand = 0x1003;
TBB [2]. fsstate = tbstate_enabled;
TBB [2]. fsstyle = btns_button;
TBB [2]. dwdata = 0;
TBB [2]. istring = ipaste;

Sendmessage (hwndtb, tb_addbuttons, (wparam) 3,
(Lparam) (lptbbutton) & TBB );

Sendmessage (hwndtb, tb_autosize, 0, 0 );

Showwindow (hwndtb, sw_show );
Return hwndtb;
}

Hwnd winapi createrebar (hwnd hwndowner)
{
Rebarinfo RBI;
Rebarbandinfo rbband;
Rect RC;
Hwnd hwndcb, hwndtb, hwndrb;
DWORD dwbtnsize;
Initcommoncontrolsex icex;

Icex. dwsize = sizeof (initcommoncontrolsex );
Icex. dwicc = icc_cool_classes | icc_bar_classes;
Initcommoncontrolsex (& icex );

Hwndrb = createjavaswex (ws_ex_toolwindow,
Rebarclassname,
Null,
Ws_child | ws_visible | ws_clipsiblings |
Ws_clipchildren | ccs_nodivider,
0, 0, 0,
Hwndowner,
Null,
Hinst,
Null );

If (! Hwndrb)
Return NULL;

// Initialize and send the rebarinfo structure.
RBI. cbsize = sizeof (rebarinfo); // required when using this
// Structure.
RBI. fmask = 0;
RBI. himl = (himagelist) NULL;
If (! Sendmessage (hwndrb, rb_setbarinfo, 0, (lparam) & RBI ))
Return NULL;

// Initialize structure members that both bands will share.
Rbband. cbsize = sizeof (rebarbandinfo); // required
Rbband. fmask = rbbim_colors | rbbim_text | rbbim_background |
Rbbim_style | rbbim_child | rbbim_childsize |
Rbbim_size;
Rbband. fstyle = rbbs_childedge | rbbs_fixedbmp | rbbs_break | rbbs_gripperalways;
Rbband. hbmback = loadbitmap (hinst, makeintresource (idb_bitmap1 ));

// Create the combo box control to be added.
Hwndcb = createwindow (L "button", l "button", bs_pushbutton | ws_child, 0,0, 20,20, hwndowner, null, hinst, null); // createcombobox (hwndrb );
// Set values unique to the band with the combo box.
Getwindowrect (hwndcb, & rc );
Rbband. lptext = _ T ("combo box ");
Rbband. hwndchild = hwndcb;
Rbband. cxminchild = 0;
Rbband. cyminchild = RC. Bottom-RC. Top;
Rbband. Cx = 200;

// Add the band that has the combo box.
Sendmessage (hwndrb, rb_insertband, (wparam)-1, (lparam) & rbband );

// Create the Toolbar Control to be added.
Hwndtb = createatoolbar (hwndowner );
// Hwndtb = createwindow (_ T ("button"), _ T ("button"), bs_pushbutton | ws_child, 0, 0, 20, 20, hwndowner, null, hinst, null ); // createcombobox (hwndrb );

// Get the height of the toolbar.
Dwbtnsize = (DWORD) sendmessage (hwndtb, tb_getbuttonsize, 0, 0 );

// Set values unique to the band with the toolbar.
Rbband. lptext = _ T ("tool bar ");
Rbband. hwndchild = hwndtb;
Rbband. cxminchild = 0;
Rbband. cyminchild = hiword (dwbtnsize );
Rbband. Cx = 250;

// Add the band that has the toolbar.
Sendmessage (hwndrb, rb_insertband, (wparam)-1, (lparam) & rbband );

Return (hwndrb );
}

//
// Function: wndproc (hwnd, uint, wparam, lparam)
//
// Purpose: to process messages in the main window.
//
// Wm_command-process application menu
// Wm_paint-draws the Main Window
// Wm_destroy-Send the Exit message and return
//
//
Lresult callback wndproc (hwnd, uint message, wparam, lparam)
{
Int wmid, wmevent;
Paintstruct pS;
HDC;

Switch (Message)
{
Case wm_create:
{
Createrebar (hwnd );
// Createatoolbar (hwnd );
}
Break;
Case wm_command:
Wmid = loword (wparam );
Wmevent = hiword (wparam );
// Select the analysis menu:
Switch (wmid)
{
Case idm_about:
Dialogbox (hinst, makeintresource (idd_aboutbox), hwnd, about );
Break;
Case idm_exit:
Destroywindow (hwnd );
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Break;
Case wm_paint:
HDC = beginpaint (hwnd, & PS );
// Todo: add any drawing code here...
Endpaint (hwnd, & PS );
Break;
Case wm_destroy:
Postquitmessage (0 );
Break;
Default:
Return defwindowproc (hwnd, message, wparam, lparam );
}
Return 0;
}

// Message processing program in the "about" box.
Int_ptr callback about (hwnd hdlg, uint message, wparam, lparam)
{
Unreferenced_parameter (lparam );
Switch (Message)
{
Case wm_initdialog:
Return (int_ptr) true;

Case wm_command:
If (loword (wparam) = idok | loword (wparam) = idcancel)
{
Enddialog (hdlg, loword (wparam ));
Return (int_ptr) true;
}
Break;
}
Return (int_ptr) false;
}

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.