我在基于拨号的应用程序的系统菜单中添加了一个项目,项目显示在菜单中,但是当我单击它时,什么也没发生。这是代码:
#define IDM_CLIP 17
BOOL CCalculatorControlDlg::OnInitDialog()
{
CDialog::OnInitDialog();
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
CString strClip;
strClip.LoadString(IDS_CLIPBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
pSysMenu->AppendMenu(MF_STRING, IDM_CLIP, strClip);
}
}
SetIcon(m_hIcon, TRUE);
SetIcon(m_hIcon, FALSE);
m_Calculator1.SetNumFormat("%0.2f");
LOGFONT lf;
m_Calculator1.GetResultWndFont(&lf);
strcpy(lf.lfFaceName, "Brittanic Bold");
m_Calculator1.SetResultsWndFont(&lf);
m_Calculator1.GetButtonFont(&lf);
strcpy(lf.lfFaceName, "Tahoma");
m_Calculator1.SetButtonFont(&lf);
m_Calculator1.SetResultsWndBkClr(RGB(128,128,128));
m_Calculator1.SetResultsWndTxtClr(RGB(255,255,255));
return FALSE;
}
void CCalculatorControlDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if((nID & 0xFFF0) == IDM_CLIP)
{
m_Calculator1.OnEditCopy();
}
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
关于对话框由框架自动添加,我添加了 IDM_CLIP。m_Calculator1.OnEditCopy() 是我的 CCalculatorCtrl 类中的函数,它将文本复制到剪贴板。我还在我的 CalculatorControlDlg.cpp 文件中包含了 CCalculatorCtrl.h 文件,我正在其中编辑系统菜单。这是 OnEditCopy 方法,顺便说一句,它适用于按钮控制......
这是我在 CCalculatorControlDlg 类中调用的方法。
void CCalculatorCtrl::OnEditCopy()
{
if ( !OpenClipboard() )
{
AfxMessageBox( _T("Cannot open the Clipboard") );
return;
}
// Remove the current Clipboard contents
if( !EmptyClipboard() )
{
AfxMessageBox( _T("Cannot empty the Clipboard") );
return;
}
// Get the currently selected data
HGLOBAL hGlob = GlobalAlloc(GMEM_FIXED, 64);
strcpy_s((char*)hGlob, 64, m_strCurrentEntry);
// For the appropriate data formats...
if ( ::SetClipboardData( CF_TEXT, hGlob ) == NULL )
{
CString msg;
msg.Format(_T("Unable to set Clipboard data, error: %d"), GetLastError());
AfxMessageBox( msg );
CloseClipboard();
GlobalFree(hGlob);
return;
}
AfxMessageBox( _T("Copy to clipboard successful!"));
CloseClipboard();
}
我希望我可以理解...在此先感谢。