MFC 界面开发:利用 DLL 库实现三相源电压输出控制
首先,在 MFC 的界面上拖拽两个按钮,一个下拉框和一个静态文本框,并为它们分别命名为 'btnStart'、'btnStop'、'cmbVoltage' 和 'txtLog'。
接下来,在类的头文件中声明以下变量:
#include "DLL7000.h" // 包含 DLL 库的头文件
class CMyDlg : public CDialogEx
{
private:
int m_nVoltage; // 存储下拉框选择的电压
HINSTANCE m_hDll; // 存储 DLL 库的句柄
typedef int(*DSPOutputFunc)(int Adjust, int ComPort, int ID, int PhaseWire, float Frequency, float Ua, float Ub, float Uc, float Ia, float Ib, float Ic, float Uab, float Uac, float UIA, float UIB, float UIC, int Wave, DelayTime* pDelayTimey); // 声明 DLL 函数指针类型
DSPOutputFunc m_pDSPOutput; // 存储 DLL 函数指针
public:
// ...
};
然后,在类的实现文件中,初始化下拉框:
void CMyDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
// ...
DDX_Control(pDX, IDC_COMBO_VOLTAGE, m_cmbVoltage);
m_cmbVoltage.AddString(_T("220V"));
m_cmbVoltage.AddString(_T("100V"));
m_cmbVoltage.AddString(_T("57.7V"));
m_cmbVoltage.SetCurSel(0);
// ...
}
接着,为 '开始' 按钮添加点击事件处理函数,调用 DLL 库中的函数输出电压:
void CMyDlg::OnBnClickedBtnStart()
{
// 获取下拉框选择的电压
m_nVoltage = m_cmbVoltage.GetCurSel();
float fVoltage = 0.0f;
switch (m_nVoltage)
{
case 0:
fVoltage = 220.0f;
break;
case 1:
fVoltage = 100.0f;
break;
case 2:
fVoltage = 57.7f;
break;
}
// 调用 DLL 库中的函数输出电压
m_hDll = LoadLibrary(_T("DLL7000.dll")); // 加载 DLL 库
if (m_hDll)
{
m_pDSPOutput = (DSPOutputFunc)GetProcAddress(m_hDll, "DSPOutput"); // 获取 DLL 函数指针
if (m_pDSPOutput)
{
int nRet = m_pDSPOutput(0, 0, 0, 3, 50.0f, fVoltage, fVoltage, fVoltage, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL); // 调用 DLL 函数输出电压
if (nRet == 0)
{
m_txtLog.SetWindowText(_T("输出电压成功!")); // 在静态文本框输出日志
}
else
{
m_txtLog.SetWindowText(_T("输出电压失败!")); // 在静态文本框输出日志
}
}
FreeLibrary(m_hDll); // 释放 DLL 库
}
}
最后,为 '停止' 按钮添加点击事件处理函数,停止输出电压:
void CMyDlg::OnBnClickedBtnStop()
{
// 调用 DLL 库中的函数停止输出电压
if (m_pDSPOutput)
{
int nRet = m_pDSPOutput(0, 0, 0, 3, 50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0, NULL); // 调用 DLL 函数停止输出电压
if (nRet == 0)
{
m_txtLog.SetWindowText(_T("停止输出电压成功!")); // 在静态文本框输出日志
}
else
{
m_txtLog.SetWindowText(_T("停止输出电压失败!")); // 在静态文本框输出日志
}
}
}
原文地址: https://www.cveoy.top/t/topic/oihN 著作权归作者所有。请勿转载和采集!