From ba48e50c8f434da26b3f2b705f2c1cf85638e4a0 Mon Sep 17 00:00:00 2001 From: "shixiong.Liu" Date: Mon, 11 Nov 2024 21:09:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0envtools=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- env_tools/EnvHelper.cpp | 89 +++++++++++++++---- env_tools/EnvHelper.h | 13 ++- env_tools/EnvTools.cpp | 49 ++++++++++ env_tools/EnvTools.h | 10 +++ env_tools/EnvTools.ui | 58 +++++++++--- env_tools/{evn_tools.sln => env_tools.sln} | 2 +- .../{evn_tools.vcxproj => env_tools.vcxproj} | 0 ...proj.filters => env_tools.vcxproj.filters} | 0 env_tools/main.cpp | 11 +-- 9 files changed, 193 insertions(+), 39 deletions(-) rename env_tools/{evn_tools.sln => env_tools.sln} (85%) rename env_tools/{evn_tools.vcxproj => env_tools.vcxproj} (100%) rename env_tools/{evn_tools.vcxproj.filters => env_tools.vcxproj.filters} (100%) diff --git a/env_tools/EnvHelper.cpp b/env_tools/EnvHelper.cpp index d9ecb3f..825fff8 100644 --- a/env_tools/EnvHelper.cpp +++ b/env_tools/EnvHelper.cpp @@ -7,31 +7,27 @@ EnvHelper::EnvHelper() { this->_init_env(); + this->_convert_2_map(); } EnvHelper::~EnvHelper() { +} + +std::pair EnvHelper::find(const wstring& name) const +{ + auto ifind = this->_map_env.find(name); + if (ifind == this->_map_env.end()) + { + return {}; + } + return { ifind->first, ifind->second }; } std::map EnvHelper::get_env() const { - std::map result = {}; - for (const wstring& item : _vec_env) { - if (item.empty()) { - continue; - } - if (item[0] == '=') { - continue; - } - std::vector values = _split(item, L'='); - if (values.empty()) - { - continue; - } - result[values[0]] = values[1]; - } - return result; + return this->_map_env; } bool EnvHelper::_init_env() @@ -58,8 +54,25 @@ bool EnvHelper::_init_env() return true; } +void EnvHelper::_convert_2_map() +{ + for (const wstring& item : _vec_env) { + if (item.empty()) { + continue; + } + if (item[0] == '=') { + continue; + } + std::vector values = split(item, L'='); + if (values.empty()) + { + continue; + } + _map_env[values[0]] = values[1]; + } +} -std::vector EnvHelper::_split(const wstring& str, wchar_t delimiter) const { +std::vector EnvHelper::split(const wstring& str, wchar_t delimiter) { std::vector tokens; size_t start = 0; size_t end = str.find(delimiter); @@ -74,3 +87,45 @@ std::vector EnvHelper::_split(const wstring& str, wchar_t delimiter) co tokens.push_back(str.substr(start)); return tokens; } + +bool EnvHelper::set_key(LPCWSTR varName, LPCWSTR varValue) { + HKEY hKey; + LONG result; + + // 打开注册表项 + result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _reg_path.c_str(), 0, KEY_SET_VALUE, &hKey); + if (result != ERROR_SUCCESS) { + return false; + } + + // 设置环境变量 + result = RegSetValueEx(hKey, + varName, + 0, REG_SZ, reinterpret_cast(varValue), (wcslen(varValue) + 1) * sizeof(wchar_t)); + RegCloseKey(hKey); + if (result != ERROR_SUCCESS) { + return false; + } + + // 通知系统环境变量已经更改 + SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0, reinterpret_cast(L"Environment"), SMTO_ABORTIFHUNG, 5000, nullptr); + return true; +} + +bool EnvHelper::del_key(const std::wstring& valueName) { + HKEY hKey; + LONG result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _reg_path.c_str(), 0, KEY_SET_VALUE, &hKey); + + if (result != ERROR_SUCCESS) { + return false; + } + + result = RegDeleteValue(hKey, valueName.c_str()); + if (result != ERROR_SUCCESS) { + RegCloseKey(hKey); + return false; + } + + RegCloseKey(hKey); + return true; +} diff --git a/env_tools/EnvHelper.h b/env_tools/EnvHelper.h index e790d72..189c181 100644 --- a/env_tools/EnvHelper.h +++ b/env_tools/EnvHelper.h @@ -3,12 +3,16 @@ #include #include #include +#include using std::wstring; using std::vector; class EnvHelper { +public: + static std::vector split(const std::wstring& str, wchar_t delimiter); + public: EnvHelper(); ~EnvHelper(); @@ -16,14 +20,17 @@ class EnvHelper EnvHelper& operator=(const EnvHelper& left) = delete; EnvHelper(const EnvHelper& left) = delete; std::map get_env() const; + std::pair find(const wstring& name) const; + bool set_key(LPCWSTR varName, LPCWSTR varValue); + bool del_key(const std::wstring& valueName); private: bool _init_env(); - std::vector _split(const std::wstring& str, wchar_t delimiter) const; + void _convert_2_map(); private: vector _vec_env; - - + std::map _map_env; + const wstring _reg_path = LR"(SYSTEM\CurrentControlSet\Control\Session Manager\Environment)"; }; diff --git a/env_tools/EnvTools.cpp b/env_tools/EnvTools.cpp index c87fe13..c2adc21 100644 --- a/env_tools/EnvTools.cpp +++ b/env_tools/EnvTools.cpp @@ -6,6 +6,7 @@ EnvTools::EnvTools(QWidget* parent) { ui.setupUi(this); this->_init_ui(); + this->_init_signal(); } EnvTools::~EnvTools() @@ -23,3 +24,51 @@ void EnvTools::_init_ui() idx++; } } + +void EnvTools::_init_signal() +{ + connect(ui.tableWidget, SIGNAL(cellClicked(int, int)), this, SLOT(DoCellClicked(int, int))); +} + +void EnvTools::DoCellClicked(int row, int column) +{ + ui.listWidget->clear(); + this->_clear_list(); + QString name = ui.tableWidget->item(row, 0)->text(); + auto target_value = _env_helper.find(name.toStdWString()); + if (target_value.first == L"") + { + return; + } + else + { + ui.label_key->setText(QString::fromStdWString(target_value.first)); + wstring value = target_value.second; + std::vector vec_v = EnvHelper::split(value, L';'); + for (auto& item : vec_v) { + this->ui.listWidget->addItem(QString::fromStdWString(item)); + } + } +} + +void EnvTools::on_btn_add_all_clicked() +{ + +} + +void EnvTools::on_btn_del_all_clicked() +{ + +} + +void EnvTools::on_btn_add_sub_clicked() +{ + +} + +void EnvTools::on_btn_del_sub_clicked() +{ + +} + + diff --git a/env_tools/EnvTools.h b/env_tools/EnvTools.h index bc384c4..b7a4f08 100644 --- a/env_tools/EnvTools.h +++ b/env_tools/EnvTools.h @@ -14,6 +14,16 @@ class EnvTools : public QWidget private: void _init_ui(); + void _init_signal(); + void _clear_list(); + + +protected slots: + void DoCellClicked(int row, int column); + void on_btn_add_all_clicked(); + void on_btn_del_all_clicked(); + void on_btn_add_sub_clicked(); + void on_btn_del_sub_clicked(); private: Ui::EnvToolsClass ui; diff --git a/env_tools/EnvTools.ui b/env_tools/EnvTools.ui index b92dc65..4ca9268 100644 --- a/env_tools/EnvTools.ui +++ b/env_tools/EnvTools.ui @@ -31,20 +31,29 @@ - + - + - Other: + ALL: + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + + Qt::ElideMiddle + true @@ -79,14 +88,14 @@ - + Add - + Del @@ -97,21 +106,37 @@ - + - + + + + 0 + 0 + + - PATH evn: + Key: - + + + color: rgb(255, 0, 0); + + + Path + + + + + @@ -128,14 +153,14 @@ - + Add - + Del @@ -170,9 +195,16 @@ - + + + Save + + + + + - 重启explorer + restart explorer diff --git a/env_tools/evn_tools.sln b/env_tools/env_tools.sln similarity index 85% rename from env_tools/evn_tools.sln rename to env_tools/env_tools.sln index f9e4250..c1e7cee 100644 --- a/env_tools/evn_tools.sln +++ b/env_tools/env_tools.sln @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 16 VisualStudioVersion = 16.0.32602.291 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "evn_tools", "evn_tools.vcxproj", "{9E8A8CE2-EC9C-4041-8C4A-4D17ED31A1DE}" +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "env_tools", "env_tools.vcxproj", "{9E8A8CE2-EC9C-4041-8C4A-4D17ED31A1DE}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/env_tools/evn_tools.vcxproj b/env_tools/env_tools.vcxproj similarity index 100% rename from env_tools/evn_tools.vcxproj rename to env_tools/env_tools.vcxproj diff --git a/env_tools/evn_tools.vcxproj.filters b/env_tools/env_tools.vcxproj.filters similarity index 100% rename from env_tools/evn_tools.vcxproj.filters rename to env_tools/env_tools.vcxproj.filters diff --git a/env_tools/main.cpp b/env_tools/main.cpp index 807cf22..70b0ba5 100644 --- a/env_tools/main.cpp +++ b/env_tools/main.cpp @@ -1,10 +1,11 @@ #include "EnvTools.h" #include -int main(int argc, char *argv[]) +int main(int argc, char* argv[]) { - QApplication a(argc, argv); - EnvTools w; - w.show(); - return a.exec(); + QApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QApplication a(argc, argv); + EnvTools w; + w.show(); + return a.exec(); }