- UID
- 5043
- 积分
- 1347
- 精华
- 贡献
-
- 威望
-
- 活跃度
-
- D豆
-
- 在线时间
- 小时
- 注册时间
- 2002-5-13
- 最后登录
- 1970-1-1
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
先附上流程图:
(1) 创建工程
(2)插入一个对话框
双击.rc文件
右键-》选中添加资源
修改对话框ID:IDD_OPTION_SHEET
在上面添加一个Tab控件, 修改ID:IDC_TAB
(3)为此对话框添加映射类COptionSheet,基类为CAcUiTabMainDialog
(4)再插入一个对话框资源(IDD_TEXT_PAGE)
右击,选“插入dialog”
放置一个文本框
(5)创建文字选项卡的映射类CTextPage
(6)插入第二个选项卡对应的资源对话框(IDD_CONREOL_PAGE)
添加Group Box按钮
内部放置两个单选按钮(ID分别为:IDC_RADIO1和IDC_RADIO2),只勾选第一个按钮的Group选项
添加一个复选框控件
点击一下对话框,修改属性
创建这个选项卡对话框的映射类CControlPage
(7)在COptionSheet类中,为Tab控件添加映射成员变量
点击Tab控件框,右键选择“添加变量”
(8) 在COptionSheet类中添加两个成员变量
CTextPage m_textPage;
CControlPage m_controlPage;
注:上图中的变量名应为:m_textPage
(9)创建IDD_OPTION_SHEET对话框的初始化事件
(10)在命令的实现函数中加入下面的代码,并添加头文件:#include "OptionSheet.h"
这时候 编译程序,并打开CAD,看看是否能弹出对话框
(11)添加一个新类CIniFile(C++常规类) ,用于读写INI文件中的键值
(13)类的实现内容
IniFile.h:
 - #pragma once
- class CIniFile
- {
- public:
- CIniFile();
- //~CIniFile();
- CIniFile(const TCHAR* fileName);
- virtual ~CIniFile();
- //指定字段和键的名称,获得对应的键值
- bool GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, CString &strValue, int bufferLength = 1000) const;
- bool GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, int &nValue) const;
- bool GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, byte &byteValue) const;
- bool GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, bool &bValue) const;
- bool GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, double &dValue) const;
- //指定字段名和键的名称,写入对应的键值
- bool SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, const TCHAR* strValue);
- bool SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, int nValue);
- bool SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, byte byteValue);
- bool SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, bool bValue);
- //decimalplaces小数点位数
- bool SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, double dValue, int decimalPlaces = 2);
- private:
- CString m_strFile;//INI文件的保存位置
- };
IniFile.cpp
 - #include "stdafx.h"
- #include "IniFile.h"
- CIniFile::CIniFile()
- {
- }
- CIniFile::~CIniFile()
- {
- }
- CIniFile::CIniFile(const TCHAR* fileName)
- {
- m_strFile = fileName;
- }
- //指定字段和键的名称,获得对应的键值
- bool CIniFile::GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, CString &strValue, int bufferLength) const
- {
- assert(m_strFile.GetLength() > 0);
- CString strDefault = TEXT("NotExist");//如果找不到对应的键,则返回该值
- CString strTemp;
- DWORD dwCharacters = ::GetPrivateProfileString(strFieldName,
- strKeyName,
- strDefault,
- strTemp.GetBuffer(bufferLength),
- bufferLength,
- m_strFile);
- strTemp.ReleaseBuffer();
- //注意GetPrivateProfileString函数的错误形式
- if (strTemp.Compare(strDefault) == 0)
- {
- return false;
- }
- else
- {
- strValue = strTemp;
- return true;
- }
- }
- bool CIniFile::GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, int &nValue) const
- {
- CString strValue = TEXT("");
- if (GetValueOfKey(strFieldName, strKeyName, strValue))
- {
- nValue = _ttoi(strValue);
- return true;
- }
- else
- {
- return false;
- }
- }
- bool CIniFile::GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, byte &byteValue) const
- {
- CString strValue = TEXT("");
- if (GetValueOfKey(strFieldName, strKeyName, strValue))
- {
- byteValue = (byte)(_ttoi(strValue));
- return true;
- }
- else
- {
- return false;
- }
- }
- bool CIniFile::GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, bool &bValue) const
- {
- CString strValue = TEXT("");
- if (GetValueOfKey(strFieldName, strKeyName, strValue))
- {
- bValue = bool(_ttoi(strValue) == 1);
- return true;
- }
- else
- {
- return false;
- }
- }
- bool CIniFile::GetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, double &dValue) const
- {
- CString strValue = TEXT("");
- if (GetValueOfKey(strFieldName, strKeyName, strValue))
- {
- //dValue = _tstof(strValue)//CString转double
- TCHAR* szStop = NULL;
- dValue = _tcstod(strValue, &szStop);//CString转double
- return true;
- }
- else
- {
- return false;
- }
- }
- //指定字段名和键的名称,写入对应的键值
- bool CIniFile::SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, const TCHAR* strValue)
- {
- //防止在调用函数之前m_strFile未被初始化
- if (m_strFile.IsEmpty())
- {
- AfxMessageBox(TEXT("在调用函数SetValueOfKey时,m_strFile未被赋值,异常退出"));
- return false;
- }
- BOOL bRet = ::WritePrivateProfileString(strFieldName,
- strKeyName,
- strValue,
- m_strFile);
- if (bRet)
- {
- return true;
- }
- else
- return false;
- }
- bool CIniFile::SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, int nValue)
- {
- CString strValue = TEXT("");
- strValue.Format(TEXT("%d"), nValue);//returns the string format
- return SetValueOfKey(strFieldName, strKeyName, strValue);
- }
- bool CIniFile::SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, byte byteValue)
- {
- CString strValue = TEXT("");
- strValue.Format(TEXT("%u"), byteValue);
- return SetValueOfKey(strFieldName, strKeyName, strValue);
- }
- bool CIniFile::SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, bool bValue)
- {
- CString strValue = TEXT("");
- strValue.Format(TEXT("%d"), bValue);
- return SetValueOfKey(strFieldName, strKeyName, strValue);
- }
- //decimalplaces小数点位数
- bool CIniFile::SetValueOfKey(const TCHAR* strFieldName, const TCHAR* strKeyName, double dValue, int decimalPlaces)
- {
- assert(decimalPlaces >= 0);
- CString strFormat = TEXT("");
- strFormat.Format(TEXT("%%0.%df"), decimalPlaces);
- CString strValue = TEXT("");
- strValue.Format(strFormat, dValue);
- return SetValueOfKey(strFieldName, strKeyName, strValue);
- }
(13)为CTextPage窗体中的文字高度的文本框映射double类型的成员变量m_textHeight
同理,为文字样式组合框映射CComboBox成员变量m_cboTextStyle
(14)创建CTextPage窗体的初始化事件
填充文字样式组合框,从INI文件中加载默认的参数值:
 - //填充文字样式组合框
- std::vector<CString> textStyles;
- CTextStyleUtil::GetAll(textStyles);
- for (int i = 0; i < textStyles.size(); i++)
- {
- m_cboTextStyle.AddString(textStyles);
- }
- if (m_cboTextStyle.GetCount() > 0)
- {
- m_cboTextStyle.SetCurSel(0);
- }
- //从INI文件中加载参数值
- CIniFile iniFile(CAppDirectoryUtil::GetCurrentDirectory() + TEXT("\\OptionSheet.ini"));
- CString field = TEXT("OptionSheet");
- iniFile.GetValueOfKey(field, TEXT("textHeight"), m_textHeight);
- CString strTextStyle;
- iniFile.GetValueOfKey(field, TEXT("textStyle"), strTextStyle);
- //设置组合框的当前选择项
- for (int i = 0; i < m_cboTextStyle.GetCount(); i++)
- {
- CString strItem;
- m_cboTextStyle.GetLBText(i, strItem);//retrieves a string from the list box of a combo box
- if (strItem.CompareNoCase(strTextStyle) == 0)//Zero if the strings are identical (ignoring case),
- {
- m_cboTextStyle.SetCurSel(i);
- break;
- }
- }
- UpdateData(FALSE);
添加类AppDirectoryUtil
类AppDirectoryUtil的实现
AppDirectoryUtil.h
 - // AppDirectoryUtil.h: interface for the CAppDirectoryUtil class.
- ////////////////////////////////////////////////////////////////////////
- #if !defined(AFX_APPDIRECTORYUTIL_H__DD493023_982A_4370_8A6F_C271F5FD388F__INCLUDED_)
- #define AFX_APPDIRECTORYUTIL_H__DD493023_982A_4370_8A6F_C271F5FD388F__INCLUDED_
- #if _MSC_VER > 1000
- #pragma once
- #endif // _MSC_VER > 1000
- class CAppDirectoryUtil
- {
- public:
- CAppDirectoryUtil();
- // 功能: 获得当前的ARX文件所在的路径
- static CString GetCurrentDirectory(HMODULE hInstance = _hdllInstance);
- // 功能: 获得当前的ARX文件所在的文件夹的上级目录
- static CString GetParentDirectory(HMODULE hInstance = _hdllInstance);
- ~CAppDirectoryUtil();
- };
#endif // !defined(AFX_APPDIRECTORYUTIL_H__DD493023_982A_4370_8A6F_C271F5FD388F__INCLUDED_)
AppDirectoryUtil.cpp
#include "stdafx.h"
#include "AppDirectoryUtil.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CAppDirectoryUtil::CAppDirectoryUtil()
{
}
CAppDirectoryUtil::~CAppDirectoryUtil()
{
}
CString CAppDirectoryUtil::GetCurrentDirectory(HMODULE hInstance)
{
TCHAR szPath[256];
GetModuleFileName(hInstance, szPath, sizeof(szPath));
*(_tcsrchr(szPath, '\\')) = 0; // 将最后一个\所在的位置修改为\0
CString strResult = szPath;
return strResult;
}
CString CAppDirectoryUtil::GetParentDirectory(HMODULE hInstance)
{
TCHAR szPath[256];
GetModuleFileName(hInstance, szPath, sizeof(szPath));
*(_tcsrchr(szPath, '\\')) = 0; // 将最后一个\所在的位置设置为\0
*(_tcsrchr(szPath, '\\')) = 0; // 继续将最后一个\所在的位置设计为\0
CString strResult = szPath;
return strResult;
}
添加C++常规类:CTextStyleUtil
CTextStyleUtil.h:
#if !defined(AFX_TEXTSTYLEUTIL_H__F392A987_01EA_4AD0_BCE3_C39921CAC013__INCLUDED_)
#define AFX_TEXTSTYLEUTIL_H__F392A987_01EA_4AD0_BCE3_C39921CAC013__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vector>
class CTextStyleUtil
{
public:
CTextStyleUtil();
virtual ~CTextStyleUtil();
// 获得某个文字样式的ID
static AcDbObjectId GetAt(const TCHAR* name);
// 获得文字样式名称列表
static void GetAll(std::vector<CString> &textStyles);
// 创建一种文字样式
static AcDbObjectId Add(const TCHAR* name, const TCHAR* fontFileName = TEXT("txt.shx"),
const TCHAR* bigFontFileName = TEXT("gbcbig.shx"));
//~CTextStyleUtil();
};
#endif // !defined(AFX_TEXTSTYLEUTIL_H__F392A987_01EA_4AD0_BCE3_C39921CAC013__INCLUDED_)
CTextStyleUtil.cpp:
 - #include "stdafx.h"
- #include "TextStyleUtil.h"
- #include <dbsymtb.h>
- #include <acutmem.h>
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CTextStyleUtil::CTextStyleUtil()
- {
- }
- CTextStyleUtil::~CTextStyleUtil()
- {
- }
- AcDbObjectId CTextStyleUtil::GetAt(const TCHAR* name)
- {
- AcDbObjectId textStyleId;
- if (_tcslen(name) > 0)//Unicode字符串的长度
- {
- AcDbTextStyleTable* pTextStyleTable = NULL;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable, AcDb::kForRead);
- pTextStyleTable->getAt(name, textStyleId); // 如果不存在,textStyleId不会被赋值
- pTextStyleTable->close();
- }
- return textStyleId;
- }
- void CTextStyleUtil::GetAll(std::vector<CString> &textStyles)
- {
- textStyles.clear();
- AcDbTextStyleTable *pTextStyleTbl = NULL;
- acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTbl, AcDb::kForRead);
- AcDbTextStyleTableIterator *pIt = NULL;
- pTextStyleTbl->newIterator(pIt);
- for (; !pIt->done(); pIt->step())
- {
- AcDbTextStyleTableRecord *pRcd = NULL;
- if (pIt->getRecord(pRcd, AcDb::kForRead) == Acad::eOk)
- {
- TCHAR *szName = NULL;
- pRcd->getName(szName);
- if (_tcslen(szName) > 0) // 过滤掉名称为空的文字样式
- {
- textStyles.push_back(szName);
- }
- acutDelString(szName);
- pRcd->close();
- }
- }
- delete pIt;
- pTextStyleTbl->close();
- }
- AcDbObjectId CTextStyleUtil::Add(const TCHAR* name, const TCHAR* fontFileName, const TCHAR* bigFontFileName)
- {
- Acad::ErrorStatus es;
- AcDbTextStyleTable* pTextStyleTable = NULL;
- es = acdbHostApplicationServices()->workingDatabase()->getSymbolTable(pTextStyleTable, AcDb::kForWrite);
- AcDbTextStyleTableRecord* pTextStyleRecord = new AcDbTextStyleTableRecord();
- es = pTextStyleRecord->setName(name);
- es = pTextStyleRecord->setBigFontFileName(bigFontFileName); // 大字体文件
- es = pTextStyleRecord->setFileName(fontFileName); // 字体文件
- es = pTextStyleRecord->setXScale(1.0); // 文字高宽比(一般这里都设置为1,在文字属性中决定高宽比)
- es = pTextStyleTable->add(pTextStyleRecord);
- AcDbObjectId styleId = pTextStyleRecord->objectId();
- pTextStyleTable->close();
- pTextStyleRecord->close();
- return styleId;
- }
在TextPage.cpp文件中添加以下头文件:
 - #include "AppDirectoryUtil.h"
- #include "IniFile.h"
- #include <vector>
- #include <string.h>
- #include "TextStyleUtil.h"
这时候编译会出错:
需要强制类型转换:
(15)在CTextPage类中添加公有成员函数SaveProfiles,用于将用户在控件中输入的参数值保存到INI中
函数的代码:
 - bool CTextPage::SaveProfile()
- {
- if (!UpdateData())
- {
- return false;
- }
- //保存参数值
- CIniFile iniFile(CAppDirectoryUtil::GetCurrentDirectory() +
- TEXT("\\OptionSheet.ini"));
- CString field = TEXT("OptionSheet");
- iniFile.SetValueOfKey(field, TEXT("textHeight"), m_textHeight);
- CString strTextStyle;
- m_cboTextStyle.GetLBText(m_cboTextStyle.GetCurSel(), strTextStyle);
- iniFile.SetValueOfKey(field, TEXT("textStyle"), strTextStyle);
- return false;
- }
(16)对CControlPage窗体中的单选按钮映射int类型的成员变量m_nRadio1
对CControlPage窗体中的复选框按钮映射BOOL类型的成员变量m_bCheck1
这是编译发现出错:
强制类型转换:
(17)创建CControlPage窗体的初始化事件
 - BOOL CControlPage::OnInitDialog()
- {
- CAcUiTabChildDialog::OnInitDialog();
- // TODO: 在此添加额外的初始化
- //从INI文件中加载参数值
- CIniFile iniFile(CAppDirectoryUtil::GetCurrentDirectory() +
- TEXT("\\OptionSheet.ini"));
- CString field = TEXT("OptionSheet");
- iniFile.GetValueOfKey(field, TEXT("nRadio1"), m_nRadio1);
- iniFile.GetValueOfKey(field, TEXT("bCheck1"), m_bCheck1);
- UpdateData(FALSE);
- return TRUE; // return TRUE unless you set the focus to a control
- // 异常: OCX 属性页应返回 FALSE
- }
添加头文件
 - #include "AppDirectoryUtil.h"
- #include "IniFile.h"
- (18)在CControlPage 类中添加函数SaveProfiles
- bool CControlPage::SaveProfiles()
- {
- CIniFile iniFile(CAppDirectoryUtil::GetCurrentDirectory() +
- TEXT("\\OptionSheet.ini"));
- CString field = TEXT("OptionSheet");
- iniFile.SetValueOfKey(field, TEXT("nRadio1"), m_nRadio1);
- iniFile.SetValueOfKey(field, TEXT("bCheck1"), m_bCheck1);
- return false;
- }
(19)为COptionSheet窗体添加OnOK消息处理函数
 - void COptionSheet::OnOK()
- {
- // TODO: 在此添加专用代码和/或调用基类
- if (!m_textPage.SaveProfile() || !m_controlPage.SaveProfiles())
- {
- return;
- }
- CAcUiTabMainDialog::OnOK();
- }
效果:
项目源代码:在ObjectARX中使用MFC-标签式对话框 项目源代码
参考资料:
《AutoCAD ObjectARX(VC)开发基础与实例教程》
---------------------
作者:李燕良
来源:CSDN
原文:https://blog.csdn.net/qq_40416052/article/details/81666093
版权声明:本文为博主原创文章,转载请附上博文链接!
|
|