c++构建器应用程序设置
C++ builder application settings?
我想知道如何在c++ builder中保存xml或ini文件中应用程序的设置。知道visual studio在"设置"中有这些功能,我正在寻找c++ builder中相同的功能。
用什么方法来解决这个问题
当使用c++ Builder创建VCL应用程序时,您可以使用ini文件来保存下次启动应用程序时想要恢复的设置和值。
首先,包含iniffiles .hpp头文件。
#include <IniFiles.hpp>
保存设置和值,创建一个新的TIniFile并在OnClose事件中写入。
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
bool booleanValueToSave = true;
int integerValueToSave = 42;
TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
ini->WriteString("SectionName", "KeyName", "Value to save as KeyName");
ini->WriteBool("SectionName", "AnotherKeyName", booleanValueToSave);
ini->WriteInteger("SectionName", "YetAnotherKeyName", integerValueToSave);
// To save something like the window size and position
ini->WriteInteger("Settings", "WindowState", Form1->WindowState);
if (Form1->WindowState == wsNormal)
{
ini->WriteInteger("Settings", "MainFrm Top", Form1->Top);
ini->WriteInteger("Settings", "MainFrm Left", Form1->Left);
ini->WriteInteger("Settings", "MainFrm Height", Form1->Height);
ini->WriteInteger("Settings", "MainFrm Width", Form1->Width);
}
delete ini;
}
别忘了删除!!
这里的代码将创建一个与可执行文件同名但扩展名为.ini的ini文件。将有两个标题,"SectionName"answers"Settings"。在标题下,您将看到键值对,如"AnotherKeyName=true"answers"YetAnotherKeyName=42"。"
然后,为了在应用程序启动时恢复值,创建一个新的TIniFile并在OnCreate事件中从中读取。
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TWindowState ws;
int integerValueToRestore;
bool booleanValueToRestore;
int someDefaultIntegerValueIfTheKeyDoesntExist = 7;
bool someDefaultBooleanValueIfTheKeyDoesntExist = false;
TIniFile *ini = new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
integerValueToRestore = ini->ReadInteger("SectionName", "YetAnotherKeyName", someDefaultIntegerValueIfTheKeyDoesntExist);
booleanValueToRestore = ini->ReadBool("SectionName", "AnotherKeyName", someDefaultBooleanValueIfTheKeyDoesntExist);
// To restore the window size and position you saved on FormClose
ws = (TWindowState)ini->ReadInteger("Settings", "WindowState", wsNormal);
if (ws == wsMinimized)
ws = wsNormal;
if (ws == wsNormal)
{
Form1->Top = ini->ReadInteger("Settings", "MainFrm Top", 10);
Form1->Left = ini->ReadInteger("Settings", "MainFrm Left", 10);
Form1->Height = ini->ReadInteger("Settings", "MainFrm Height", 730);
Form1->Width = ini->ReadInteger("Settings", "MainFrm Width", 1028);
}
Form1->WindowState = ws;
delete ini;
}
希望对你有帮助。
相关文章:
- 设置我的应用程序 API 感知并防止系统使其模糊和错误定位
- 我可以对 2 个或更多应用程序进行 QSettings 设置吗?
- 如何设置Qt应用程序以使用类Unix系统的实际环境变量?
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 如何将从控制台应用程序C++回调设置为C++ COM DLL
- 使用代码在 AWS 开发工具包C++控制台应用程序上设置凭证
- 如何使用CMake在Windows上将应用程序入口点设置为main()C++?
- 为没有qmake/VS的Qt Windows应用程序设置.exe图标
- 如何在MFC对话框应用程序中默认将复选框设置为选中?
- 如何在C++应用程序中设置"Select Precision"光标?
- 如何在应用程序进行大量printf时,在固定输入的终端中设置最后一行
- 如何以编程方式设置应用程序EXE文件的图标
- 如何在OS X上的Qt应用程序中设置应用程序图标,足以进行分发
- 使用CMake设置应用程序图标
- 如何在Qt中设置应用程序图标
- 以编程方式设置应用程序的处理器相关性
- 没有找到为windows应用程序设置应用程序图标的问题.rc文件
- 如何在Mac上设置c++应用程序名称和图标
- 以程序方式设置应用程序的播放设备
- Qt5.6设置应用程序图标(Linux)