应该不是代码问题,可能和运行环境有关系,需要现场调试一下就能解决。
如果是指菜单少了两个的话,你可能没有保存资源文件,又或者修改后编译出错直接运行了修改前生成的exe
嗯,可以往下面的三个方向找找看:
1、检查对话框是否已经被添加到菜单中,可以加个判断,如果对话框已经添加到菜单中,则打印“对话框已添加到菜单中”,否则打印“对话框未添加到菜单中”。如下面示例代码:
if menu.add_cascade({'menu': ['File', 'Exit']}, menu_item):
print('对话框已添加到菜单中')
else:
print('对话框未添加到菜单中')
2、检查对话框是否已经被添加到view类中(同样可以加个判断的方式)
3、检查对话框是否已经被添加到调试模式(在应用程序中添加一个断点,当程序执行到断点时,程序将暂停,并打开一个交互式调试器。在调试器中,查看对话框是否已经添加到菜单中)
按钮下拉菜单 Demo 源代码:
#define ID_MENU_SECTIONACTION_PNTSELS 32799
#define ID_MENU_SECTIONACTION_GCS 32800
#define ID_MENU_SECTIONACTION_GCN 32801
#define ID_MENU_SECTIONACTION_GCE 32802
#define ID_MENU_SECTIONACTION_GCW 32803
#define ID_MENU_SECTIONACTION_GCU 32804
#define ID_MENU_SECTIONACTION_GCUL 32805
#define ID_MENU_SECTIONACTION_CLS 32806
public:
afx_msg void OnBnClickedBtnSectionAction();
//执行添加一个点选剖切面
void BeginOnePointSection(string sActType);
public:
CMFCMenuButton m_BtnMenuSectionAction;
CMenu m_MenuActions;
// 下拉按钮的状态
map<int, StringWide> m_mapId2MenuName;
int m_nCurrentClickId = -1;
int m_nCurrentClickIndex = -1;
void CSectionsObjectManagerDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MFCMENUBUTTON_SECTIONLIST, m_BtnMenuSectionAction);
}
BOOL CSectionsObjectManagerDlg::OnInitDialog()
{
__super::OnInitDialog();
//...
//下拉按钮
m_mapId2MenuName[ID_MENU_SECTIONACTION_PNTSELS] = L"PointSelSection";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCS] = L"GlobalCuttingS";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCN] = L"GlobalCuttingN";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCE] = L"GlobalCuttingE";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCW] = L"GlobalCuttingW";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCU] = L"GlobalCuttingEL";
m_mapId2MenuName[ID_MENU_SECTIONACTION_GCUL] = L"GlobalCutting-EL";
m_mapId2MenuName[ID_MENU_SECTIONACTION_CLS] = L"CenterLineSection";
string flag("Sections Manager");
m_MenuActions.LoadMenu(IDR_MENU_SECTIONACTIONS);
for (auto Item : m_mapId2MenuName)
{
m_MenuActions.ModifyMenuW(Item.first, MF_BYCOMMAND | MF_STRING, Item.first, tr(Item.second.c_str(), flag));
}
m_BtnMenuSectionAction.m_hMenu = m_MenuActions.GetSubMenu(0)->GetSafeHmenu(); // 将CMFCMenuButton和Menu IDR_MENU1关联
m_BtnMenuSectionAction.SizeToContent();
m_BtnMenuSectionAction.m_bOSMenu = FALSE;
m_BtnMenuSectionAction.SetWindowTextW(tr(L"PointSelSection", flag));
m_nCurrentClickId = ID_MENU_SECTIONACTION_PNTSELS;
m_BtnMenuSectionAction.m_nMenuResult = m_nCurrentClickId;
m_nCurrentClickIndex = 0;
}
void CSectionsObjectManagerDlg::OnBnClickedBtnSectionAction()
{
int nCurrIndex = -1;
int nCurrId = -1;
if (m_BtnMenuSectionAction.m_nMenuResult == 0)
{
nCurrId = m_nCurrentClickId;
nCurrIndex = m_nCurrentClickIndex;
}
else
{
nCurrId = m_BtnMenuSectionAction.m_nMenuResult;
nCurrIndex = m_BtnMenuSectionAction.m_nMenuResult - ID_MENU_SECTIONACTION_PNTSELS;
}
if (nCurrId <= 0 || m_mapId2MenuName.find(nCurrId) == m_mapId2MenuName.end())
return;
CString sText = m_mapId2MenuName[nCurrId].c_str();
if (sText.GetLength() > 0)
{
m_BtnMenuSectionAction.SetWindowText(tr(sText.GetString(), "Sections Manager"));
m_BtnMenuSectionAction.SetFocus();
m_BtnMenuSectionAction.RedrawWindow();
m_BtnMenuSectionAction.Invalidate();
m_BtnMenuSectionAction.UpdateData(FALSE);
}
m_nCurrentClickIndex = nCurrIndex;
m_nCurrentClickId = nCurrId;
// 执行相应的动作
BeginOnePointSection(StringWideToUtf8(sText.GetString()));
}
代码怎么写的
你在资源里添加对话框资源
调出classwizard,创建好对应的对话框类
然后再在菜单上添加菜单项目,设置好ID,在classwizard映射成员函数
里面编写代码
Dialog1 dlg; //假设你的对话框类叫做 Dialog1
dlg.DoModal();
文件位置变了,导致错误...
以下答案由GPT-4.5Plus大模型(语音)与博主A努力中.......共同编写:
根据你的描述,这很有可能是由于你在VS2019的菜单中添加对话框所导致的。VS2019的菜单是通过 .vsct 和 .vsmenu 文件配置的,如果直接在代码中修改,很容易导致IDE出现问题。
要在VS2019中添加自定义菜单项和对话框,正确的方式是:
一个简单的示例:
.vsct 文件:
xml
<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<Commands package="guidMyPackage">
<Buttons>
<Button guid="guidMyCommandSet" id="MyCommandId" priority="0x0100" type="Button">
<Parent guid="guidSHLMainMenu" id="IDM_VS_MENU_TOOLS"/>
<Icon guid="guidImages" id="bmpPic1" />
<Strings>
<ButtonText>My Custom Tool</ButtonText>
</Strings>
</Button>
</Buttons>
</Commands>
</CommandTable>
.vsmenu 文件:
[$rootkey$\Menus\{5EFC7975-14BC-11CF-9B2B-00AA00573819}\Commands\{5EFC7975-14BC-11CF-9B2B-00AA00573819}\{guidMyPackage}]
"Strings"="MyPackage"
[$rootkey$\Menus\{5EFC7975-14BC-11CF-9B2B-00AA00573819}\Commands\{5EFC7975-14BC-11CF-9B2B-00AA00573819}\{guidMyPackage}\MyCommandSet.MyCommandId]
"Strings"="My Custom Tool|MyCommandId"
代码:
csharp
[Guid("guidMyPackage")]
public class MyPackage
{
public static MyPackage Instance = new MyPackage();
public const string guidMyCommandSet = "guidMyCommandSet";
public const uint MyCommandId = 0x0100;
[Command(guidMyCommandSet, MyCommandId)]
public void OnMyCommand()
{
MessageBox.Show("Hello from My Custom Tool!");
}
}
希望这个说明能帮助你解决在VS2019中添加菜单项与对话框的问题。