简体   繁体   English

Win32应用程序设置

[英]Win32 Application Settings

How can I store settings that can later be loaded into my application? 如何存储以后可以加载到我的应用程序中的设置?

For instance, I have an edit control that the user can change the font, font color, and background color of. 例如,我有一个编辑控件,用户可以更改其字体,字体颜色和背景颜色。 When the user changes these, I would like to save them so that when the user opens the application again the fonts and colors will be the same as they left them. 当用户更改这些内容时,我想保存它们,以便当用户再次打开应用程序时,字体和颜色将与它们离开时相同。

I had thought about writing to the windows registry, but not only am I confused as to how to save fonts there, (or even if you could do such a thing), but I also have a portable version that can be run off of a flash-drive. 我曾经考虑过写入windows注册表,但我不仅对如何在那里保存字体感到困惑,(或者即使你可以做这样的事情),但我也有一个可以运行的便携版本闪存驱动器。 Is there any way to save these settings to a file that is in the same directory as the executable? 有没有办法将这些设置保存到与可执行文件位于同一目录中的文件中? If so, how would I go about saving and loading these? 如果是这样,我将如何保存和加载这些?

If there isn't a way to accomplish this using a settings file, how could I use the windows registry to do it? 如果没有办法使用设置文件来完成此操作,我怎么能使用Windows注册表来执行此操作?

Best way IMO is to store it in ini files, for example, one file : app.ini 最好的方式IMO是将其存储在ini文件中,例如,一个文件:app.ini

[AppSettings]
fntBackground=255 255 0
fntFace=Tahome

[LoadDialog]
...

it is quite easy to write it yourself, you can also find ready classes on codeproject, I took a fast search and found this: 你自己编写它很容易,你也可以在codeproject上找到准备好的类,我快速搜索并发现:

http://www.codeproject.com/Articles/5401/CIni http://www.codeproject.com/Articles/5401/CIni

looks promising 看起来很有希

If your application is allowed to write to the folder, then you can use an .ini file as many Windows application do. 如果允许您的应用程序写入该文件夹,则可以像许多Windows应用程序一样使用.ini文件。 You can read and write to the file with GetPrivateProfile/WritePrivateProfile APIs, eg GetPrivateProfileInt 您可以使用GetPrivateProfile/WritePrivateProfile API读取和写入文件,例如GetPrivateProfileInt

If you are using some framework, it is possible that it also provides friendlier version of the API (eg MFC provides CWinApp::GetProfileInt and others). 如果您正在使用某个框架,它可能还提供更友好的API版本(例如,MFC提供CWinApp::GetProfileInt等)。

The easiest way to read config info in Windows is by using GetPrivateProfileString /WritePrivateProfileString functions as Zdeslav Vojkovic said. Zdeslav Vojkovic说,在Windows中读取配置信息的最简单方法是使用GetPrivateProfileString / WritePrivateProfileString函数。

But then you see this: Note This function is provided only for compatibility with 16-bit Windows-based applications. 但是你会看到: 注意此功能仅用于与16位基于Windows的应用程序兼容。 Applications should store initialization information in the registry. 应用程序应将初始化信息存储在注册表中 Which is obsolete nowadays either and often is forbidden. 现在已经过时了,经常被禁止。 Recommendations (but often just simple fashion) require you to use XML files for this. 建议(但通常只是简单的方式)要求您使用XML文件 Then you can use whatever method you like - DOM, SAX, XMLLite . 然后你可以使用你喜欢的任何方法 - DOM,SAX, XMLLite

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM