Skip to content

Commit

Permalink
实现插件功能的选项设置
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongyang219 committed Aug 27, 2021
1 parent 9567d05 commit 87d033a
Show file tree
Hide file tree
Showing 13 changed files with 155 additions and 20 deletions.
26 changes: 26 additions & 0 deletions PluginDemo/DataManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@

CDataManager::CDataManager()
{
//获取模块的路径
HMODULE hModule = reinterpret_cast<HMODULE>(&__ImageBase);
wchar_t path[MAX_PATH];
GetModuleFileNameW(hModule, path, MAX_PATH);
m_module_path = path;
//从配置文件读取配置
LoadConfig();
}

CDataManager::~CDataManager()
{
SaveConfig();
}

CDataManager& CDataManager::Instance()
{
static CDataManager instance;
return instance;
}

void CDataManager::LoadConfig()
{
std::wstring config_path = m_module_path + L".ini";
m_setting_data.show_second = GetPrivateProfileInt(_T("config"), _T("show_second"), 0, config_path.c_str());
}

void CDataManager::SaveConfig() const
{
std::wstring config_path = m_module_path + L".ini";
wchar_t buff[16];
swprintf_s(buff, L"%d", m_setting_data.show_second);
WritePrivateProfileString(_T("config"), _T("show_second"), buff, config_path.c_str());
}
12 changes: 12 additions & 0 deletions PluginDemo/DataManager.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
#pragma once
#include <string>

struct SettingData
{
bool show_second{ false };
};

class CDataManager
{
private:
CDataManager();
~CDataManager();

public:
static CDataManager& Instance();

void LoadConfig();
void SaveConfig() const;

public:
std::wstring m_cur_time;
std::wstring m_cur_date;
SettingData m_setting_data;

private:
std::wstring m_module_path;
};
21 changes: 21 additions & 0 deletions PluginDemo/OptionsDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,28 @@ void COptionsDlg::DoDataExchange(CDataExchange* pDX)


BEGIN_MESSAGE_MAP(COptionsDlg, CDialog)
ON_BN_CLICKED(IDC_SHOW_SECOND_CHECK, &COptionsDlg::OnBnClickedShowSecondCheck)
END_MESSAGE_MAP()


// COptionsDlg 消息处理程序


BOOL COptionsDlg::OnInitDialog()
{
CDialog::OnInitDialog();

// TODO: 在此添加额外的初始化
//初始化控件状态
CheckDlgButton(IDC_SHOW_SECOND_CHECK, m_data.show_second);

return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}


void COptionsDlg::OnBnClickedShowSecondCheck()
{
// TODO: 在此添加控件通知处理程序代码
m_data.show_second = (IsDlgButtonChecked(IDC_SHOW_SECOND_CHECK) != 0);
}
7 changes: 6 additions & 1 deletion PluginDemo/OptionsDlg.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#pragma once

#include "DataManager.h"

// COptionsDlg 对话框

Expand All @@ -16,8 +16,13 @@ class COptionsDlg : public CDialog
enum { IDD = IDD_OPTIONS_DIALOG };
#endif

SettingData m_data;

protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持

DECLARE_MESSAGE_MAP()
public:
virtual BOOL OnInitDialog();
afx_msg void OnBnClickedShowSecondCheck();
};
2 changes: 1 addition & 1 deletion PluginDemo/PluginDemo.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ extern "C" {

__declspec(dllexport) IPluginItem* TMPluginCreateInstance(int index);
__declspec(dllexport) void TMPluginInfoRequired();
__declspec(dllexport) void TMPluginOptions();
__declspec(dllexport) void TMPluginOptions(HWND hParent);

#ifdef __cplusplus
}
Expand Down
69 changes: 64 additions & 5 deletions PluginDemo/PluginDemo.rc
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,15 @@ END
// Dialog
//

IDD_OPTIONS_DIALOG DIALOGEX 0, 0, 310, 178
IDD_OPTIONS_DIALOG DIALOGEX 0, 0, 165, 90
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "ʱ�ں���������"
FONT 9, "΢���ź�", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "ȷ��",IDOK,199,157,50,14
PUSHBUTTON "ȡ��",IDCANCEL,253,157,50,14
DEFPUSHBUTTON "ȷ��",IDOK,50,69,50,14
PUSHBUTTON "ȡ��",IDCANCEL,108,69,50,14
GROUPBOX "ʱ���ʽ",IDC_STATIC,7,7,151,55
CONTROL "��ʾ��",IDC_SHOW_SECOND_CHECK,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,21,63,10
END


Expand All @@ -71,9 +73,9 @@ BEGIN
IDD_OPTIONS_DIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 303
RIGHTMARGIN, 158
TOPMARGIN, 7
BOTTOMMARGIN, 171
BOTTOMMARGIN, 83
END
END
#endif // APSTUDIO_INVOKED
Expand All @@ -93,6 +95,63 @@ END
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// Ӣ��(����) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)

/////////////////////////////////////////////////////////////////////////////
//
// Dialog
//

IDD_OPTIONS_DIALOG DIALOGEX 0, 0, 165, 90
STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Time and date settings"
FONT 9, "Segoe UI", 400, 0, 0x0
BEGIN
DEFPUSHBUTTON "OK",IDOK,50,69,50,14
PUSHBUTTON "Cancel",IDCANCEL,108,69,50,14
GROUPBOX "Time format",IDC_STATIC,7,7,151,55
CONTROL "Show seconds",IDC_SHOW_SECOND_CHECK,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,15,21,63,10
END


/////////////////////////////////////////////////////////////////////////////
//
// DESIGNINFO
//

#ifdef APSTUDIO_INVOKED
GUIDELINES DESIGNINFO
BEGIN
IDD_OPTIONS_DIALOG, DIALOG
BEGIN
LEFTMARGIN, 7
RIGHTMARGIN, 158
TOPMARGIN, 7
BOTTOMMARGIN, 83
END
END
#endif // APSTUDIO_INVOKED


/////////////////////////////////////////////////////////////////////////////
//
// AFX_DIALOG_LAYOUT
//

IDD_OPTIONS_DIALOG AFX_DIALOG_LAYOUT
BEGIN
0
END

#endif // Ӣ��(����) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
Expand Down
16 changes: 12 additions & 4 deletions PluginDemo/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@ void TMPluginInfoRequired()
wchar_t buff[128];
swprintf_s(buff, L"%d/%.2d/%.2d", system_time.wYear, system_time.wMonth, system_time.wDay);
CDataManager::Instance().m_cur_date = buff;
swprintf_s(buff, L"%.2d:%.2d:%.2d", system_time.wHour, system_time.wMinute, system_time.wSecond);

if (CDataManager::Instance().m_setting_data.show_second)
swprintf_s(buff, L"%.2d:%.2d:%.2d", system_time.wHour, system_time.wMinute, system_time.wSecond);
else
swprintf_s(buff, L"%.2d:%.2d", system_time.wHour, system_time.wMinute);
CDataManager::Instance().m_cur_time = buff;
}

void TMPluginOptions()
void TMPluginOptions(HWND hParent)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
COptionsDlg dlg;
dlg.DoModal();
COptionsDlg dlg(CWnd::FromHandle(hParent));
dlg.m_data = CDataManager::Instance().m_setting_data;
if (dlg.DoModal() == IDOK)
{
CDataManager::Instance().m_setting_data = dlg.m_data;
}
}
5 changes: 3 additions & 2 deletions PluginDemo/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
//
#define IDD_DIALOG1 101
#define IDD_OPTIONS_DIALOG 101
#define IDC_SHOW_SECOND_CHECK 1001

// Next default values for new objects
//
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 103
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1001
#define _APS_NEXT_CONTROL_VALUE 1002
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
2 changes: 1 addition & 1 deletion TrafficMonitor/PluginManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

typedef IPluginItem* (*pfTMPluginCreateInstance)(int);
typedef void (*pfTMPluginInfoRequired)();
typedef void (*pfTMPluginOptions)();
typedef void (*pfTMPluginOptions)(HWND);

//用于加载和管理插件
class CPluginManager
Expand Down
8 changes: 4 additions & 4 deletions TrafficMonitor/PluginManagerDlg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ BOOL CPluginManagerDlg::OnInitDialog()
switch (plugin.state)
{
case CPluginManager::PluginState::PS_SUCCEED:
status = _T("加载成功");
status = CCommon::LoadText(IDS_PLUGIN_LOAD_SUCCEED);
break;
case CPluginManager::PluginState::PS_MUDULE_LOAD_FAILED:
status.Format(_T("插件模块加载失败,故障代码:%d"), plugin.error_code);
status = CCommon::LoadTextFormat(IDS_PLUGIN_MODULE_LOAD_FAILED, { static_cast<int>(plugin.error_code) });
break;
case CPluginManager::PluginState::PS_FUNCTION_GET_FAILED:
status.Format(_T("函数获取失败,故障代码:%d"), plugin.error_code);
status = CCommon::LoadTextFormat(IDS_PLUGIN_FUNCTION_GET_FAILED, { static_cast<int>(plugin.error_code) });
break;
}
int index = m_list_ctrl.GetItemCount();
Expand Down Expand Up @@ -122,6 +122,6 @@ void CPluginManagerDlg::OnBnClickedOptinsButton()
{
auto plugin = theApp.m_plugins.GetPlugins()[m_item_selected];
if (plugin.TMPluginOptions != nullptr)
plugin.TMPluginOptions();
plugin.TMPluginOptions(m_hWnd);
}
}
4 changes: 2 additions & 2 deletions TrafficMonitor/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void CTest::Test()

void CTest::TestCommand()
{
TestPlugin();
//TestPlugin();
}

void CTest::TestHttpQequest()
Expand Down Expand Up @@ -67,6 +67,6 @@ void CTest::TestPlugin()
{
if (!theApp.m_plugins.GetPlugins().empty())
{
theApp.m_plugins.GetPlugins()[0].TMPluginOptions();
theApp.m_plugins.GetPlugins()[0].TMPluginOptions(theApp.m_pMainWnd->m_hWnd);
}
}
Binary file modified TrafficMonitor/TrafficMonitor.rc
Binary file not shown.
3 changes: 3 additions & 0 deletions TrafficMonitor/resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@
#define IDS_HDD_USAGE 347
#define IDS_FILE_NAME 348
#define IDS_STATUS 349
#define IDS_PLUGIN_LOAD_SUCCEED 350
#define IDS_PLUGIN_MODULE_LOAD_FAILED 351
#define IDS_PLUGIN_FUNCTION_GET_FAILED 352
#define IDC_STATIC_INFO 1001
#define IDC_STATIC1 1002
#define IDC_STATIC_DOWN 1002
Expand Down

0 comments on commit 87d033a

Please sign in to comment.