CMenu menu; CMenu sub_menu1; CMenu sub_menu2; menu.CreateMenu(); sub_menu1.CreatePopupMenu(); sub_menu2.CreatePopupMenu(); sub_menu1.AppendMenu(MF_STRING,ID_DIA_NEW,_T("新建")); sub_menu1.AppendMenu(MF_STRING,ID_DIA_OPEN,_T("打开")); sub_menu1.AppendMenu(MF_SEPARATOR); sub_menu1.AppendMenu(MF_STRING,ID_DIA_EXIT,_T("退出")); sub_menu2.AppendMenu(MF_STRING,ID_DIA_CUT,_T("剪切")); sub_menu2.AppendMenu(MF_STRING,ID_DIA_PASTE,_T("粘贴")); //把子菜单添加到主菜单上去 menu.AppendMenu(MF_POPUP,(UINT)sub_menu1.m_hMenu,_T("&基本操作")); menu.AppendMenu(MF_POPUP,(UINT)sub_menu2.m_hMenu,_T("&编辑")); SetMenu(&menu); menu.Detach();
在对话框上手动创建菜单代码
// TODO: 在此添加控件通知处理程序代码 UINT uiRead; CFile file; char buffer[1024]; CString str; file.Open(L"text.txt",CFile::modeRead|CFile::modeCreate|CFile::modeNoTruncate); do { memset(buffer,0,1024); uiRead = file.Read(buffer,1024); str += CString(buffer); } while (uiRead < 0); file.Close(); GetDlgItem(IDC_EDIT1)->SetWindowText(str);
读取文件代码
// TODO: 在此添加控件通知处理程序代码 CoInitialize(NULL); //初始化COM组件 _ConnectionPtr pConn(__uuidof(Connection)); //实例化一个connection对象pConn _RecordsetPtr pRst(__uuidof(Recordset)); //实例化一个Recordset对象pRst _CommandPtr pCmd(__uuidof(Command)); //实例化一个Command对象pCmd //通过pConn对象连接字符串,连接到ACCESS数据库,这里ACCESS数据库在C根目录下 pConn->Open("Provider=MIcrosoft.Jet.OLEDB.4.0;Data source=Student.mdb","","",adConnectUnspecified); pCmd->put_ActiveConnection(_variant_t((IDispatch*)pConn)); pCmd->CommandText="SELECT * FROM Students"; //通过pCmd对象访问数据库 pRst=pCmd->Execute(NULL,NULL,adCmdText); try{ while(!pRst->rsEOF) { ((CListBox*)GetDlgItem(IDC_LIST1))->AddString((_bstr_t)pRst->GetCollect("StudentName")); //把数据库中学号这列的内容放入IDC_LIST1的LIST控件中 pRst->MoveNext(); //下移一个 } }catch(_com_error e){ MessageBox(_T("FILEDS")); } pRst->Close(); //关闭对象 pConn->Close(); pCmd.Release(); //释放对象 pRst.Release(); pConn.Release(); CoUninitialize(); //卸载COM组件
链接数据库,头文件要先引入
#import "C:Program FilesCommon FilesSystemadomsadox.dll" #import "C:Program FilesCommon FilesSystemadomsado15.dll" no_namespace rename("EOF","rsEOF")//不要命名空间,重命//名EOF为rsEOF