前言一、效果展示二、版本、版权信息设置三、版本信息获取与显示四、工程源码
前言
VS版本:VS2019
QT版本:Qt5.12.3(msvc2017_64)
一、效果展示
1.增加版本信息前 VS 增加版本信息后
2.获取版本信息前 VS 获取版本信息后
在VS中,右键项目名称->添加->资源;
在弹出的窗口中,选择Version->新建;
在弹出的版本资源管理器中可以对版本号、公司名称、版权、软件名称等信息进行设置;
保存后退出资源管理器,重新生成可执行文件,可执行文件(.exe)即可显示版本、版权等信息。
三、版本信息获取与显示
需要增加版本资源获取文件VersionUpdate.h;
VersionUpdate.h文件
#pragma comment(lib, "version.lib") #include#include #include class VersionUpdate { public: static QString VersionUpdate::GetVersion() { int nMaxPathName = 4096; // Max length of file name/path TCHAR* pBuffer; UINT nItemLength; void* pData, * lpBuffer; QString sVersion; DWORD dwInfoSize, dwHandle; VS_FIXEDFILEINFO* pFileInfo; // Get the file path and name pBuffer = new TCHAR[nMaxPathName]; GetModuleFileName(NULL, pBuffer, nMaxPathName - 1); // Get File Version Info size dwInfoSize = GetFileVersionInfoSize(pBuffer, &dwHandle); if (dwInfoSize > 0) { pData = new TCHAR[dwInfoSize]; if (GetFileVersionInfo(pBuffer, dwHandle, dwInfoSize, pData)) if (VerQueryValue(pData, _T("\"), &lpBuffer, &nItemLength)) { pFileInfo = (VS_FIXEDFILEINFO*)lpBuffer; sVersion = QString("%1.%2.%3.%4") .arg(pFileInfo->dwProductVersionMS >> 16) .arg(pFileInfo->dwProductVersionMS & 0xFFFF) .arg(pFileInfo->dwProductVersionLS >> 16) .arg(pFileInfo->dwProductVersionLS & 0xFFFF); // Calculate the product version as a number, you can delete the next statement if you don't need it. DWORD dwProductVersion = (pFileInfo->dwProductVersionMS >> 16) * 1000 + (pFileInfo->dwProductVersionMS & 0xFFFF) * 100 + (pFileInfo->dwProductVersionLS >> 16) * 10 + (pFileInfo->dwProductVersionLS & 0xFFFF) * 1; } // Delete the data buffer delete[] pData; } // Get rid of the allocated string buffer delete[] pBuffer; return sVersion; } private: };
在main.cpp中加入这几句代码;
#include "ApplicationVersionUpdate.h" #include//版本号获取 #include "VersionUpdate.h" #define AppTitle (QString)("ApplicationVersionUpdate_V"+VersionUpdate::GetVersion()) int main(int argc, char *argv[]) { QApplication a(argc, argv); ApplicationVersionUpdate w; //设置状态栏显示内容 w.setWindowTitle(AppTitle); w.show(); return a.exec(); }
运行项目,窗框状态栏则显示配置的版本信息。
四、工程源码
github 免费下载CSDN 免积分下载



