First, how to add menus to Dialog-based programs
[1.1] Add the menu (idR_Menu1) resource and add the menu item required.
[1.2] Edit the attribute of dialog resource IDD_dlgmenutoolbar_dialog, select IDR_MENU1 in the Properties dialog box.
[1.3] If you don't want to set the menu directly in the dialog property, you can use the code to dynamically generate in the program.
[1.3.1] Add a member variable in the CDLGMenutoolBardlg class name CMenu M_Menu
Add the following code in cdlgmenutoolbardlg :: OnNitDialog (): // Load menu
M_Menu.LoadMenu (idR_Menu1);
// Set the current menu
Setmenu (& m_Menu);
// You can use SetMenu (NULL) when you don't need a menu; to cancel the current menu
Second, how to add a toolbar to Dialog-based program
[2.1] Add the toolbar (idR_toolbar1) resource and draw each button.
[2.2] Add member variables in the CDLGMenutoolBardlg class sound name CToolbar M_WndToolbar;
[2.3] Add a flat toolbar to cdlgmenutoolbardlg :: onInitDialog () Add a flat toolbar
IF (! m_wndtoolbar.createex (this, tbstyle_flat, ws_child | ws_visible | cbrs_align_top | CBRS_GRIPPER | CBRS_ToolTIPS,
CRECT (4, 4, 0)) ||! M_wndtoolbar.loadtoolbar (idR_toolbar1)))
{
Trace0 ("Failed to Create Toolbar / N);
Return False;
}
m_wndtoolbar.showwindow (SW_SHOW);
Repositionbars (AFX_IDW_CONTROLBAR_FIRST, AFX_IDW_CONTROLBAR_LAST, 0);
Third, add tool tips to the toolbar
[3.1] Handmade the definition of message mapping functions in the CDLGMenutoolBardlg class definition, as follows of the black body part // {{AFX_MSG (CDLGMenutoolBardlg)
Virtual Bool OnInitdialog ();
AFX_MSG void onpaint ();
AFX_MSG BOOL ONTOOLTIPTEXT (UINT, NMHDR * PNMHDR, LRESULT * PRESULT);
//}} AFX_MSG
Declare_message_map ()
[3.2] Implementation code / / Toolbar prompt to add functions to cdlgmenutoolbardlg.cpp
Bool CDLGMENUTOOLBARDLG :: ONTOLTIPTEXT (UINT, NMHDR * PNMHDR, LRESULT * PRESULT)
{
AskERT (pnmhdr-> code == ttn_needtexta || pnmhdr-> code == ttn_needtextw);
// unicode message
Tooltiptexta * pttta = (tooltiptexta *) PNMHDR;
Tooltiptextw * ptttw = (tooltiptextw *) pnmhdr;
// tchar szfulltext [512];
CString Strtiptext;
UINT NID = PNMHDR-> IDFROM;
IF (pnmhdr-> code == ttn_needtexta && (PTTTA-> UFLAGS & TTF_IDISHWND) || PNMHDR-> Code == TTN_NeedTextw && (PTTW-> uflags & ttf_idishwnd))
{
// idFrom is the HWnd of the toolbar
NID = :: getdlgctrlid ((hwnd) NID);
}
IF (NID! = 0) // is not separator
{
Strtiptext.LoadString (NID);
Strtiptext = start.mid (strtiptext.find ('/ n', 0) 1);
#ifndef _unicore
IF (pnmhdr-> code == ttn_needtexta)
{
LSTRCPYN (PTTTA-> Sztext, Strtiptext, SizeOf (PTTTA-> Sztext));
}
Else
{
_mbstowcsz (ptttw-> sztext, strtiptext, sizeof (ptttw-> sztext));
}
#ELSE
IF (pnmhdr-> code == ttn_needtexta)
{
_WCSTombsz (PTTTA-> Sztext, Strtiptext, SizeOf (PTTTA-> Sztext));
}
Else
{
LSTRCPYN (PTTTW-> Sztext, Strtiptext, SizeOf (PTTTW-> Sztext));
}
#ENDIF
* PRESULT = 0;
/ / Make the toolbar prompt window in the top
:: SetWindowPos (PNMHDR-> HWndFrom, Hwnd_top, 0, 0, 0, 0, SWP_NOACTIVATE |
SWP_NOSize | SWP_NOMOVE | SWP_NOOWNERZORDER;
Return True;
}
Return True;
}
[3.3] Add a message mapping in CDLGMenutoolBardlg.cpp, please see the black body part of the following code Begin_Message_Map (CDLGMenutoolBardlg, CDIALOG)
// {{AFX_MSG_MAP (CDLGMENUTOOLBARDLG)
ON_WM_PAINT ()
ON_NOTIFY_EX (TTN_NeedText, 0, ONTooltiptext)
//}} AFX_MSG_MAP
END_MESSAGE_MAP ()
Fourth, other
In order to make your program look more cool, you can modify the code in cdlgmenutoolbardlg :: onpaint () to implement Dialog fill color.
CPAINTDC DC (this);
CRECT RECT;
GetClientRect (Rect);
Dc.fillsolidRect (RECT, RGB (60, 110, 170);