Home > Software engineering >  Vs2010 MFC increase PopupMenu menu error
Vs2010 MFC increase PopupMenu menu error

Time:10-02


I added a in the MFC CMenuView response function, and then add code
Void CMenuView: : OnRButtonDown (UINT nFlags, CPoint point)
{
//TODO: add the message handler code and/or invoke the default
//CMenu menu;
M_Menu. LoadMenu (IDR_Popup);//m_Menu declared in the class (private)
CMenu * pPopup=m_Menu GetSubMenu (0);
ClientToScreen (& amp; Point);
PPopup - & gt; TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, point x, point. The y, this);
The CView: : OnRButtonDown (nFlags, point);
}
Compile no problem, run to the following errors in bold above that, for a great god answer

CodePudding user response:

Of this problem is solved, the reason is I insert a menu (IDR_Popup) but not on the menu, so will go wrong, but then there is a new problem, is I m_Menu defines a global variable, so that after the operation, I right click on the first successful, right-click the second such mistakes again, but I declare a CMenu variable within the response function is normal, what reason is this

CodePudding user response:

Here the problem is solved, if I were in this statement
PPopup - & gt; TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, point x, point. The y, this); And then m_Menu. Detach (); Then all is well,
My guess is that m_Menu is an object that global variables, starting from the right click the mouse for the first time the object is created, and then click next time when once again create conflict, so you should call a destructor, am I right? Hope and correct

CodePudding user response:

Myself to the top

CodePudding user response:

Well, Detach

CodePudding user response:

My vc6 write nothing, also didn't wrong, look 2010 strictly,

CodePudding user response:

Menu. The GetSubMenu (0) - & gt; TrackPopupMenu (TPM_LEFTALIGN | TPM_LEFTBUTTON, pMsg - & gt; Pt. X, pMsg - & gt; Pt. J y, this);
Menu. The DestroyMenu ();

Such written

CodePudding user response:

Congratulations to LZ solving problems by themselves...

CodePudding user response:

That how to manipulate to get the menu to add the black part is not an error?

CodePudding user response:

Before loading the menu to determine whether a c + + object has been associated with Windows menu handle,
Repeat the Attach is complains because it can cause a memory leak,

 if (m_Menu GetSafeHmenu ()==NULL) 
{
M_Menu. LoadMenu (IDR_Popup);//m_Menu declared in the class (private)
}
CMenu * pPopup=m_Menu. GetSubMenu (0);
ClientToScreen (& amp; Point);
PPopup - & gt; TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, point x, point. The y, this);
The CView: : OnRButtonDown (nFlags, point);

CodePudding user response:

Another way to create and associated new menu before,
To Destroy the old first, pay attention not to use detach, causing memory leaks, so go

 void CTestView: : OnRButtonDown (UINT nFlags, CPoint point) 
{
//TODO: add the message handler code and/or invoke the default
//CMenu menu;

M_Menu. DestroyMenu ();
M_Menu. LoadMenu (IDR_POPUP_EDIT);//m_Menu declared in the class (private)
CMenu * pPopup=m_Menu. GetSubMenu (0);
ClientToScreen (& amp; Point);
PPopup - & gt; TrackPopupMenu (TPM_LEFTALIGN | TPM_RIGHTBUTTON, point x, point. The y, this);
The CView: : OnRButtonDown (nFlags, point);
}

CodePudding user response:

These can actually see the source code,
The cause of the error in the ASSERT (m_hMenu==NULL); This line of code,
Because before you Load, m_hMenu is not NULL, assertion error,



_AFXWIN_INLINE BOOL CMenu: : LoadMenu (LPCTSTR lpszResourceName)
{return the Attach (: : LoadMenu (AfxFindResourceHandle (lpszResourceName,
RT_MENU), lpszResourceName)); }

BOOL CMenu: : Attach (HMENU HMENU)
{
ASSERT (m_hMenu==NULL);//only attach once, detach on destroy
If (hMenu==NULL)
{
Return FALSE;
}

//the Capture menu object in first to ensure it does not leak if the map always be allocated/expanded
M_hMenu=hMenu;

CHandleMap * pMap=afxMapHMENU (TRUE);//create the map if not exist
ASSERT (pMap!=NULL);
The pMap - & gt; SetPermanent (m_hMenu, this);
return TRUE;
}


In order to understand the CMenu good c + + and win32 HMENU distinction,
CMenu object resources, release, and HMENU release way is different,
Beginners MFC easy to confuse the two
Detach is just the system menu resource (HEMNU) and c + + object menu link disconnected,
True to release system resources to call the menu: : DestroyMenu

BOOL CMenu: : DestroyMenu ()
{
If (m_hMenu==NULL)
Return FALSE;
Return: : DestroyMenu (Detach ());
}
  • Related