MFC工控项目实例之六CFile添加菜单栏

2024-08-26 09:20

本文主要是介绍MFC工控项目实例之六CFile添加菜单栏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本程序基于前期我的博客文章《MFC工控项目实例之五CFile类读写系统参数》
添加两个对话框如下
在这里插入图片描述
在这里插入图片描述

具体添加菜单栏参考我的博客文章MFC_ CFile类实现下拉菜单读写实例(源码下载)

这里给出相关代码
在 SEAL_PRESSURE.h文件中添加

#include <afxtempl.h>
...
class CProductPara
{
public:union{struct{char	m_strTypeName[24];char	m_strBrand[24];			char	m_strRemark[64];};char len[1024];};
};
class CSEAL_PRESSUREApp : public CWinApp
{
public:...int m_nProductSel;CArray<CProductPara,CProductPara> m_allPara;CString	m_strWorkPath,m_strCurDataPath,m_strDataPath;CString m_strControlCFGFileName;CString	m_strTypeCFGFileName;void LoadTypeCFG(void);void SaveTypeCFG(void);...

在 SEAL_PRESSUREDlg.h文件中添加

class CSEAL_PRESSUREDlg : public CDialog
{
// Construction
public:...CMenu m_menuType;int m_nProductSel;int m_nTypeIndex;CArray<CProductPara,CProductPara> m_allPara;CString	m_strWorkPath;CString m_strControlCFGFileName;CString	m_strTypeCFGFileName;CBitmap m_bmSel,m_bmList;void OnTypeChange(UINT nID);void UpdateButton(void);...}

在TypDlg.h文件中添加

class CTypDlg : public CDialog
{
// Construction
public:CProductPara * m_pPara;BOOL UpdatePara(BOOL);CTypDlg(CWnd* pParent = NULL);   // standard constructorCMenu m_menuType;int m_nProductSel;int m_nTypeIndex;
// Dialog Data//{{AFX_DATA(CTypDlg)enum { IDD = IDD_TYP_CHOICE };CListCtrl	m_ctrlType;//}}AFX_DATA// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CTypDlg)protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:// Generated message map functions//{{AFX_MSG(CTypDlg)virtual BOOL OnInitDialog();afx_msg void OnButton1();afx_msg void OnButton2();//}}AFX_MSGDECLARE_MESSAGE_MAP()
};

在TypData.h文件中添加

class CTypData : public CDialog
{
// Construction
public:CProductPara * m_pPara;BOOL UpdatePara(BOOL);CTypData(CWnd* pParent = NULL);   // standard constructor// Dialog Data//{{AFX_DATA(CTypData)enum { IDD = IDD_TYP_DATA };CString	m_strTypeName;CString	m_strBrand;CString	m_strRemark;// NOTE: the ClassWizard will add data members here//}}AFX_DATA// Overrides// ClassWizard generated virtual function overrides//{{AFX_VIRTUAL(CTypData)protected:virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support//}}AFX_VIRTUAL// Implementation
protected:// Generated message map functions//{{AFX_MSG(CTypData)virtual BOOL OnInitDialog();virtual void OnOK();// NOTE: the ClassWizard will add member functions here//}}AFX_MSGDECLARE_MESSAGE_MAP()
};

TypDlg.cpp文件中代码

// TypDlg.cpp : implementation file
//#include "stdafx.h"
#include "SEAL_PRESSURE.h"#include "TypDlg.h"
#include "TypData.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CTypDlg dialogCTypDlg::CTypDlg(CWnd* pParent /*=NULL*/): CDialog(CTypDlg::IDD, pParent)
{//{{AFX_DATA_INIT(CTypDlg)// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT
}void CTypDlg::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CTypDlg)DDX_Control(pDX, IDC_LIST1, m_ctrlType);//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CTypDlg, CDialog)//{{AFX_MSG_MAP(CTypDlg)ON_BN_CLICKED(IDC_BUTTON1, OnButton1)//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CTypDlg message handlersBOOL CTypDlg::OnInitDialog() 
{CDialog::OnInitDialog();DWORD dwExStyles = m_ctrlType.GetExtendedStyle();m_ctrlType.SetExtendedStyle(dwExStyles |LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT | LVS_EX_FLATSB |LVS_EX_ONECLICKACTIVATE);LVCOLUMN cloumn;int i;cloumn.mask = LVCF_TEXT|LVCF_WIDTH;cloumn.pszText = "型号名称";cloumn.cx = 90;i = m_ctrlType.InsertColumn(0,&cloumn);cloumn.mask = LVCF_TEXT|LVCF_WIDTH;cloumn.pszText = "产品商标";cloumn.cx = 90;i = m_ctrlType.InsertColumn(1,&cloumn);cloumn.pszText = "      备        注";cloumn.cx = 160;i = m_ctrlType.InsertColumn(2,&cloumn);int nItem;LV_ITEM item;item.mask = LVIF_TEXT|LVIF_IMAGE;char buf[255];for(i = 0; i < theApp.m_allPara.GetSize() ; i ++){nItem = m_ctrlType.GetItemCount();item.iItem = nItem;item.iSubItem = 0;sprintf(buf,"%s",theApp.m_allPara[i].m_strTypeName);item.pszText = buf;item.iImage = 0;m_ctrlType.InsertItem(&item);item.iSubItem = 1;sprintf(buf,"%s",theApp.m_allPara[i].m_strBrand);item.pszText = buf;m_ctrlType.SetItem(&item);item.iSubItem = 2;sprintf(buf,"%s",theApp.m_allPara[i].m_strRemark);item.pszText = buf;m_ctrlType.SetItem(&item);}ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);ASSERT(IDM_ABOUTBOX < 0xF000);CMenu* pSysMenu = GetSystemMenu(FALSE);if (pSysMenu != NULL){CString strAboutMenu;strAboutMenu.LoadString(IDS_ABOUTBOX);if (!strAboutMenu.IsEmpty()){pSysMenu->AppendMenu(MF_SEPARATOR);pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);}}// Set the icon for this dialog.  The framework does this automatically//  when the application's main window is not a dialog// TODO: Add extra initialization herereturn TRUE;  // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE
}void CTypDlg::OnButton1() 
{// TODO: Add your control notification handler code hereCTypData dlg;CProductPara paraBuf;	memset(&paraBuf,0,sizeof(CProductPara));dlg.m_pPara = &paraBuf;if(dlg.DoModal() == IDOK){int nItem;if(m_nTypeIndex > -1)nItem = m_nTypeIndex + 1;elsenItem = theApp.m_allPara.GetSize();theApp.m_allPara.InsertAt(nItem,paraBuf);LV_ITEM item;item.mask = LVIF_TEXT;item.iItem = nItem;item.iSubItem = 0;item.pszText = theApp.m_allPara[nItem].m_strTypeName;item.iImage = 0;m_ctrlType.InsertItem(&item);item.iSubItem = 1;item.pszText = theApp.m_allPara[nItem].m_strBrand;m_ctrlType.SetItem(&item);item.iSubItem = 2;item.pszText = theApp.m_allPara[nItem].m_strRemark;m_ctrlType.SetItem(&item);}	
}

TypData.cpp文件中代码

// TypData.cpp : implementation file
//#include "stdafx.h"
#include "SEAL_PRESSURE.h"
#include "TypData.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CTypData dialogCTypData::CTypData(CWnd* pParent /*=NULL*/): CDialog(CTypData::IDD, pParent)
{//{{AFX_DATA_INIT(CTypData)m_strTypeName = _T("");m_strBrand = _T("");m_strRemark = _T("");// NOTE: the ClassWizard will add member initialization here//}}AFX_DATA_INIT
}void CTypData::DoDataExchange(CDataExchange* pDX)
{CDialog::DoDataExchange(pDX);//{{AFX_DATA_MAP(CTypData)DDX_Text(pDX, IDC_EDIT1, m_strTypeName);DDX_Text(pDX, IDC_EDIT2, m_strBrand);DDX_Text(pDX, IDC_EDIT3, m_strRemark);// NOTE: the ClassWizard will add DDX and DDV calls here//}}AFX_DATA_MAP
}BEGIN_MESSAGE_MAP(CTypData, CDialog)//{{AFX_MSG_MAP(CTypData)// NOTE: the ClassWizard will add message map macros here//}}AFX_MSG_MAP
END_MESSAGE_MAP()/
// CTypData message handlers
BOOL CTypData::OnInitDialog() 
{CDialog::OnInitDialog();UpdatePara(FALSE);
//	UpdatePara(1);return TRUE;  // return TRUE unless you set the focus to a control// EXCEPTION: OCX Property Pages should return FALSE
}BOOL CTypData::UpdatePara(BOOL bUpdate)
{if(m_pPara == NULL)return TRUE;if(bUpdate){if(!UpdateData())return FALSE;sprintf(m_pPara->m_strTypeName,"%s", m_strTypeName);sprintf(m_pPara->m_strRemark,"%s", m_strRemark);sprintf(m_pPara->m_strBrand,"%s", m_strBrand);}else{m_strTypeName = m_pPara->m_strTypeName;m_strRemark = m_pPara->m_strRemark;m_strBrand = m_pPara->m_strBrand;UpdateData(FALSE);}return TRUE;
}void CTypData::OnOK() 
{// TODO: Add extra validation hereif(!UpdatePara(TRUE))return;CDialog::OnOK();UpdateData(TRUE);
}

SEAL_PRESSURE.cpp文件代码

// SEAL_PRESSURE.cpp : Defines the class behaviors for the application.
//#include "stdafx.h"
#include "SEAL_PRESSURE.h"
#include "SEAL_PRESSUREDlg.h"#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif/
// CSEAL_PRESSUREAppBEGIN_MESSAGE_MAP(CSEAL_PRESSUREApp, CWinApp)//{{AFX_MSG_MAP(CSEAL_PRESSUREApp)// NOTE - the ClassWizard will add and remove mapping macros here.//    DO NOT EDIT what you see in these blocks of generated code!//}}AFX_MSGON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()/
// CSEAL_PRESSUREApp constructionCSEAL_PRESSUREApp::CSEAL_PRESSUREApp()
{// TODO: add construction code here,// Place all significant initialization in InitInstanceTCHAR exeFullPath[MAX_PATH];GetModuleFileName(NULL,exeFullPath,MAX_PATH);m_Path = exeFullPath;for(int i = m_Path.GetLength() - 1; i > 0 ; i --){if(m_Path.GetAt(i) == '\\')break;}SetCurrentDirectory(m_Path);m_Path = m_Path.Left(i);m_DataPath = m_Path + "\\DATA\\";m_TempPath = m_Path + "\\TEMP\\";m_LibPath = m_Path + "\\LIB\\";	CreateDirectory(m_LibPath,FALSE);CreateDirectory(m_DataPath,FALSE);CreateDirectory(m_TempPath,FALSE);m_CFGFileName = m_Path + "\\CFG.PAR";m_strTypeCFGFileName = m_Path + "\\TYPE.CFG";m_strDataPath = m_Path + "\\DATA\\";CreateDirectory(m_strDataPath,FALSE);}/
// The one and only CSEAL_PRESSUREApp objectCSEAL_PRESSUREApp theApp;/
// CSEAL_PRESSUREApp initializationBOOL CSEAL_PRESSUREApp::InitInstance()
{AfxEnableControlContainer();// Standard initialization// If you are not using these features and wish to reduce the size//  of your final executable, you should remove from the following//  the specific initialization routines you do not need.#ifdef _AFXDLLEnable3dControls();			// Call this when using MFC in a shared DLL
#elseEnable3dControlsStatic();	// Call this when linking to MFC statically
#endifLoadCFGFile();LoadTypeCFG();CSEAL_PRESSUREDlg dlg;m_pMainWnd = &dlg;int nResponse = dlg.DoModal();if (nResponse == IDOK){// TODO: Place code here to handle when the dialog is//  dismissed with OK}else if (nResponse == IDCANCEL){// TODO: Place code here to handle when the dialog is//  dismissed with Cancel}// Since the dialog has been closed, return FALSE so that we exit the//  application, rather than start the application's message pump.SaveCFGFile();SaveTypeCFG();return FALSE;
}void CSEAL_PRESSUREApp::LoadCFGFile()
{CFile file;if(file.Open(m_CFGFileName,CFile::modeRead)){if(!file.Read(&m_sys_data,sizeof(m_sys_data))){memset(&m_sys_data,0,sizeof(m_sys_data));}file.Close();}else{memset(&m_sys_data,0,sizeof(m_sys_data));}
}void CSEAL_PRESSUREApp::SaveCFGFile()
{CFile file;if(file.Open(m_CFGFileName,CFile::modeCreate|CFile::modeWrite)){file.Write(&m_sys_data,sizeof(m_sys_data));file.Close();}
}void CSEAL_PRESSUREApp::SaveTypeCFG()
{CFile file;CProductPara paraBuf;file.Open(m_strTypeCFGFileName,CFile::modeCreate|CFile::modeWrite);for(int i = 0 ; i < theApp.m_allPara.GetSize() ; i ++){paraBuf = m_allPara[i];file.Write(&paraBuf,sizeof(m_allPara[i]));}file.Close();
}void CSEAL_PRESSUREApp::LoadTypeCFG()
{
CFile file;int nProductCount = 0;CProductPara paraBuf;if(file.Open(m_strTypeCFGFileName,CFile::modeRead)){nProductCount = file.GetLength() / sizeof(CProductPara);for(int i = 0 ; i < nProductCount ; i ++){file.Read(&paraBuf,sizeof(m_allPara[i]));m_allPara.Add(paraBuf);}file.Close();}if(nProductCount ==0){memset(&paraBuf,0,sizeof(CProductPara));strcpy(paraBuf.m_strTypeName,"默认值");strcpy(paraBuf.m_strRemark,"默认值");strcpy(paraBuf.m_strBrand,"默认值");m_allPara.Add(paraBuf);}
}

在SEAL_PRESSUREDlg.cpp文件中添加


BOOL CSEAL_PRESSUREDlg::OnInitDialog()
{CDialog::OnInitDialog();...m_menuType.LoadMenu(IDR_MENU1);	return TRUE;  // return TRUE  unless you set the focus to a control
}
void CSEAL_PRESSUREDlg::OnTypChoice() 
{// TODO: Add your control notification handler code here
//	CTypDlg dlg;
//	dlg.DoModal();int	m_nActivePlace = 0;
int	ID_DEF_PRODUCT =5000;CRect rect;GetDlgItem(IDC_TYP_CHOICE)->GetWindowRect(&rect);while(m_menuType.GetSubMenu(0)->GetMenuItemCount() >2){m_menuType.GetSubMenu(0)->RemoveMenu(2,MF_BYPOSITION);}for(int i = 0 ; i < theApp.m_allPara.GetSize() -1; i ++){CString str;if(strlen(theApp.m_allPara[i + 1].m_strBrand) > 0)str.Format("%s(%s)",theApp.m_allPara[i + 1].m_strTypeName,theApp.m_allPara[i + 1].m_strBrand);elsestr.Format("%s",theApp.m_allPara[i + 1].m_strTypeName,theApp.m_allPara[i + 1].m_strBrand);m_menuType.GetSubMenu(0)->AppendMenu(MF_STRING,ID_DEF_PRODUCT + i,str);}if(theApp.m_nProductSel > 0)m_menuType.GetSubMenu(0)->CheckMenuItem(2 + theApp.m_nProductSel - 1,MF_CHECKED|MF_BYPOSITION);m_menuType.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN |TPM_RIGHTBUTTON,rect.left,rect.bottom,this);
}

这篇关于MFC工控项目实例之六CFile添加菜单栏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1108165

相关文章

配置springboot项目动静分离打包分离lib方式

《配置springboot项目动静分离打包分离lib方式》本文介绍了如何将SpringBoot工程中的静态资源和配置文件分离出来,以减少jar包大小,方便修改配置文件,通过在jar包同级目录创建co... 目录前言1、分离配置文件原理2、pom文件配置3、使用package命令打包4、总结前言默认情况下,

python实现简易SSL的项目实践

《python实现简易SSL的项目实践》本文主要介绍了python实现简易SSL的项目实践,包括CA.py、server.py和client.py三个模块,文中通过示例代码介绍的非常详细,对大家的学习... 目录运行环境运行前准备程序实现与流程说明运行截图代码CA.pyclient.pyserver.py参

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

C语言小项目实战之通讯录功能

《C语言小项目实战之通讯录功能》:本文主要介绍如何设计和实现一个简单的通讯录管理系统,包括联系人信息的存储、增加、删除、查找、修改和排序等功能,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录功能介绍:添加联系人模块显示联系人模块删除联系人模块查找联系人模块修改联系人模块排序联系人模块源代码如下

SpringBoot项目中Maven剔除无用Jar引用的最佳实践

《SpringBoot项目中Maven剔除无用Jar引用的最佳实践》在SpringBoot项目开发中,Maven是最常用的构建工具之一,通过Maven,我们可以轻松地管理项目所需的依赖,而,... 目录1、引言2、Maven 依赖管理的基础概念2.1 什么是 Maven 依赖2.2 Maven 的依赖传递机

Vue项目中Element UI组件未注册的问题原因及解决方法

《Vue项目中ElementUI组件未注册的问题原因及解决方法》在Vue项目中使用ElementUI组件库时,开发者可能会遇到一些常见问题,例如组件未正确注册导致的警告或错误,本文将详细探讨这些问题... 目录引言一、问题背景1.1 错误信息分析1.2 问题原因二、解决方法2.1 全局引入 Element

java图像识别工具类(ImageRecognitionUtils)使用实例详解

《java图像识别工具类(ImageRecognitionUtils)使用实例详解》:本文主要介绍如何在Java中使用OpenCV进行图像识别,包括图像加载、预处理、分类、人脸检测和特征提取等步骤... 目录前言1. 图像识别的背景与作用2. 设计目标3. 项目依赖4. 设计与实现 ImageRecogni