Add menu and status bar to the dialog box

Source: Internet
Author: User

From: http://hi.baidu.com/machh03/blog/item/f320a533fd1672fe1a4cfffe.html/cmtid/f3235f24ff89713a8644f950

 

I have recently developed a player, and I have used a lot of knowledge in this area. Here is a summary:

 

1. Add menu

There are multiple methods

(1: select the menu attribute of the dialog box attribute in resource view.

(2: cmenu m_menu;

M_menu.loadmenu (idr_main_menu );
Setmenu (& m_menu );

Cancel menu setmenu (null );

 

2. Add the status bar

Add/** status bar */to the header file */
Cstatusbar m_wndstatusbar;

. Add

/** Status bar */
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
}

3. wm_menuselect message

In this way, the manually added status bar cannot receive the wm_menuselect message, so we need to add

On_wm_menuselect ()

Response Message Function

/**
* Menu bar Selection
*/
Void cfileplayerdlg: onmenuselect (uint nitemid, uint nflags, hmenu hsystemmenu)
{
Cstring sfilename;
If (nitemid = afx_ids_idlemessage)
{
Sfilename = l "ready ";
Sendmessage (wm_setmessagestring, (wparam) 0, (lparam) (lpctstr) sfilename );
Return;
}
If (nflags & mf_separator)
Return;
If (nflags & (mf_sysmenu | mf_popup ))
Return;

Cstring strstatustext;
Strstatustext. loadstring (nitemid );

M_wndstatusbar.setpanetext (0, strstatustext );//

// Sendmessage (wm_setmessagestring, (wparam) 0, (lparam) (lpctstr) strstatustext );

Return;
}

In this way, the menu prompt is displayed, m_wndstatusbar.setpanetext (0, strstatustext); // display the menu prompt in the first pane of the status bar. Of course, you can specify other panes,

If sendmessage (wm_setmessagestring, (wparam) 0, (lparam) (lpctstr) strstatustext );

You can also send wm_setmessagestring messages,

You need to add the response function of the message yourself.

As follows:

On_message (wm_setmessagestring, onsetmessagestring) // message macro

Message Processing functions

Lresult cfileplayerdlg: onsetmessagestring (wparam, lparam)
{
Lpctstr lpsz = NULL;
Cstring strmessage;

If (lparam! = 0)
{< br> assert (wparam = 0); // can't have both an ID and a string
lpsz = (lpctstr) lparam; // set an explicit string
}< br> else if (wparam! = 0)
{< BR >{< br> lptstr lpsz = strmessage. getbuffer (255);
If (afxloadstring (wparam, lpsz )! = 0)
{< br> lpsz = _ tcschr (lpsz, _ T ('\ n');
If (lpsz! = NULL)
* lpsz = '\ 0';
}< br> else
{< br> trace1 ("Warning: no message line prompt for ID 0x % 04x. \ n ", wparam);
}< br> strmessage. releasebuffer ();
}< br> lpsz = strmessage;
}< br> m_wndstatusbar.setwindowtext (lpsz); // display the menu prompt
return 0l;
}

4. In this way, you can manually add menus to the dialog box and cannot respond to the menu update command. NoOninitmenupopup () function, So you need to implement it yourself

Manually add on_wm_initmenupopup () // The response menu command updates the message.

And add corresponding message processing functions.

 

// Add function commands for processing menu command messages to the dialog box
Void cfileplayerdlg: oninitmenupopup (cmenu * ppopupmenu, uint nindex, bool bsysmenu)
{
Assert (ppopupmenu! = NULL );
// Check the Enabled state of various menu items.

Ccmdui state;
State. m_pmenu = ppopupmenu;
Assert (State. m_pother = NULL );
Assert (State. m_pparentmenu = NULL );

// Determine If menu is popup in top-level menu and set m_pother
// It If so (m_pparentmenu = NULL indicates that it is secondary popup ).
Hmenu hparentmenu;
If (afxgetthreadstate ()-> m_htrackingmenu = ppopupmenu-> m_hmenu)
State. m_pparentmenu = ppopupmenu; // parent = Child for tracking Popup.
Else if (hparentmenu =: getmenu (m_hwnd ))! = NULL)
{
Cwnd * pparent = this;
// Child windows don't have menus -- need to go to the top!
If (pparent! = NULL &&
(Hparentmenu =: getmenu (pparent-> m_hwnd ))! = NULL)
{
Int nindexmax =: getmenuitemcount (hparentmenu );
For (INT nindex = 0; nindex <nindexmax; nindex ++)
{
If (: getsubmenu (hparentmenu, nindex) = ppopupmenu-> m_hmenu)
{
// When popup is found, m_pparentmenu is containing menu.
State. m_pparentmenu = cmenu: fromhandle (hparentmenu );
Break;
}
}
}
}

State. m_nindexmax = ppopupmenu-> getmenuitemcount ();
For (State. m_nindex = 0; State. m_nindex <state. m_nindexmax;
State. m_nindex ++)
{
State. m_nid = ppopupmenu-> getmenuitemid (State. m_nindex );
If (State. m_nid = 0)
Continue; // menu separator or invalid cmd-Ignore it.

Assert (State. m_pother = NULL );
Assert (State. m_pmenu! = NULL );
If (State. m_nid = (uint)-1)
{
// Possibly a popup menu, route to first item of that Popup.
State. m_psubmenu = ppopupmenu-> getsubmenu (State. m_nindex );
If (State. m_psubmenu = NULL |
(State. m_nid = state. m_psubmenu-> getmenuitemid (0) = 0 |
State. m_nid = (uint)-1)
{
Continue; // first item of popup can't be routed.
}
State. doupdate (this, true); // popups are never auto disabled.
}
Else
{
// Normal menu item.
// Auto enable/disable if frame window has m_bautomenuenable
// Set and command is _ not _ A system command.
State. m_psubmenu = NULL;
State. doupdate (this, false );
}

// Adjust for menu deletions and additions.
Uint ncount = ppopupmenu-> getmenuitemcount ();
If (ncount <state. m_nindexmax)
{
State. m_nindex-= (State. m_nindexmax-ncount );
While (State. m_nindex <ncount &&
Ppopupmenu-> getmenuitemid (State. m_nindex) = state. m_nid)
{
State. m_nindex ++;
}
}
State. m_nindexmax = ncount;
}
}

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.