You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

859 lines
25 KiB
C++

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

// CcdDataImageProcessingDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "CcdDataImageProcessing.h"
#include "CcdDataImageProcessingDlg.h"
#include "afxdialogex.h"
// 定义文件路径宏
#define FILE_CCD_IMAGE_INFO_PATH _T("ccd_image_info.csv")
#define FILE_CCD_DATAT_PATH _T("ccd_data_path.csv")
// 控件ID映射
const int minEnergyCtrlIds[] = { IDC_MIN_ENERGY_EDIT1, IDC_MIN_ENERGY_EDIT2, IDC_MIN_ENERGY_EDIT3,
IDC_MIN_ENERGY_EDIT4, IDC_MIN_ENERGY_EDIT5, IDC_MIN_ENERGY_EDIT6,
IDC_MIN_ENERGY_EDIT7, IDC_MIN_ENERGY_EDIT8, IDC_MIN_ENERGY_EDIT9, IDC_MIN_ENERGY_EDIT10 };
const int maxEnergyCtrlIds[] = { IDC_MAX_ENERGY_EDIT1, IDC_MAX_ENERGY_EDIT2, IDC_MAX_ENERGY_EDIT3,
IDC_MAX_ENERGY_EDIT4, IDC_MAX_ENERGY_EDIT5, IDC_MAX_ENERGY_EDIT6,
IDC_MAX_ENERGY_EDIT7, IDC_MAX_ENERGY_EDIT8, IDC_MAX_ENERGY_EDIT9, IDC_MAX_ENERGY_EDIT10 };
const int proportionEnergyCtrlIds[] = { IDC_PROPORTION_ENERGY_EDIT1, IDC_PROPORTION_ENERGY_EDIT2, IDC_PROPORTION_ENERGY_EDIT3,
IDC_PROPORTION_ENERGY_EDIT4, IDC_PROPORTION_ENERGY_EDIT5, IDC_PROPORTION_ENERGY_EDIT6,
IDC_PROPORTION_ENERGY_EDIT7, IDC_PROPORTION_ENERGY_EDIT8, IDC_PROPORTION_ENERGY_EDIT9, IDC_PROPORTION_ENERGY_EDIT10 };
const int colorComboCtrlIds[] = { IDC_COLOR_COMBO1, IDC_COLOR_COMBO2, IDC_COLOR_COMBO3,
IDC_COLOR_COMBO4, IDC_COLOR_COMBO5, IDC_COLOR_COMBO6,
IDC_COLOR_COMBO7, IDC_COLOR_COMBO8, IDC_COLOR_COMBO9, IDC_COLOR_COMBO10 };
const int energyComboCtrlIds[] = { IDC_ENERGY_COMBO1, IDC_ENERGY_COMBO2, IDC_ENERGY_COMBO3,
IDC_ENERGY_COMBO4, IDC_ENERGY_COMBO5, IDC_ENERGY_COMBO6,
IDC_ENERGY_COMBO7, IDC_ENERGY_COMBO8, IDC_ENERGY_COMBO9, IDC_ENERGY_COMBO10 };
const TCHAR* colorNames[] = {
_T("Red"), _T("Orange"), _T("Yellow"), _T("Green"), _T("Cyan"), _T("Blue"),
_T("Purple"), _T("Grey"), _T("Pink"), _T("Black"), _T("White"), _T("Brown")
};
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//ColorSchemeManager m_colorSchemeManager;
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
#ifdef AFX_DESIGN_TIME
enum { IDD = IDD_ABOUTBOX };
#endif
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CCcdDataImageProcessingDlg 对话框
CCcdDataImageProcessingDlg::CCcdDataImageProcessingDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(IDD_CCDDATAIMAGEPROCESSING_DIALOG, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_SumEnergy = 0;
m_NumEnergy = 0;
m_Scheme1 = 0;
m_Scheme2 = 0;
m_Scheme3 = 0;
m_Scheme4 = 0;
m_Scheme5 = 0;
m_Scheme6 = 0;
m_Scheme7 = 0;
m_Scheme8 = 0;
m_Scheme9 = 0;
m_Scheme10 = 0;
}
void CCcdDataImageProcessingDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT1, m_MinEnergy1);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT1, m_MaxEnergy1);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT2, m_MinEnergy2);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT2, m_MaxEnergy2);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT3, m_MinEnergy3);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT3, m_MaxEnergy3);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT4, m_MinEnergy4);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT4, m_MaxEnergy4);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT5, m_MinEnergy5);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT5, m_MaxEnergy5);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT6, m_MinEnergy6);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT6, m_MaxEnergy6);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT7, m_MinEnergy7);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT7, m_MaxEnergy7);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT8, m_MinEnergy8);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT8, m_MaxEnergy8);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT9, m_MinEnergy9);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT9, m_MaxEnergy9);
DDX_Control(pDX, IDC_MIN_ENERGY_EDIT10, m_MinEnergy10);
DDX_Control(pDX, IDC_MAX_ENERGY_EDIT10, m_MaxEnergy10);
DDX_Control(pDX, IDC_ENERGY_COMBO1, m_bEnergy1);
DDX_Control(pDX, IDC_ENERGY_COMBO2, m_bEnergy2);
DDX_Control(pDX, IDC_ENERGY_COMBO3, m_bEnergy3);
DDX_Control(pDX, IDC_ENERGY_COMBO4, m_bEnergy4);
DDX_Control(pDX, IDC_ENERGY_COMBO5, m_bEnergy5);
DDX_Control(pDX, IDC_ENERGY_COMBO6, m_bEnergy6);
DDX_Control(pDX, IDC_ENERGY_COMBO7, m_bEnergy7);
DDX_Control(pDX, IDC_ENERGY_COMBO8, m_bEnergy8);
DDX_Control(pDX, IDC_ENERGY_COMBO9, m_bEnergy9);
DDX_Control(pDX, IDC_ENERGY_COMBO10, m_bEnergy10);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT1, m_ProportionEnergy1);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT2, m_ProportionEnergy2);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT3, m_ProportionEnergy3);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT4, m_ProportionEnergy4);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT5, m_ProportionEnergy5);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT6, m_ProportionEnergy6);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT7, m_ProportionEnergy7);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT8, m_ProportionEnergy8);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT9, m_ProportionEnergy9);
DDX_Control(pDX, IDC_PROPORTION_ENERGY_EDIT10, m_ProportionEnergy10);
DDX_Control(pDX, IDC_COLOR_COMBO1, m_Color1);
DDX_Control(pDX, IDC_COLOR_COMBO2, m_Color2);
DDX_Control(pDX, IDC_COLOR_COMBO3, m_Color3);
DDX_Control(pDX, IDC_COLOR_COMBO4, m_Color4);
DDX_Control(pDX, IDC_COLOR_COMBO5, m_Color5);
DDX_Control(pDX, IDC_COLOR_COMBO6, m_Color6);
DDX_Control(pDX, IDC_COLOR_COMBO7, m_Color7);
DDX_Control(pDX, IDC_COLOR_COMBO8, m_Color8);
DDX_Control(pDX, IDC_COLOR_COMBO9, m_Color9);
DDX_Control(pDX, IDC_COLOR_COMBO10, m_Color10);
DDX_Control(pDX, IDC_CCD_DATA_PATH_EDIT, m_CddDataPath);
DDX_Control(pDX, IDC_PIC_CTRL, m_PicCtrl);
}
BEGIN_MESSAGE_MAP(CCcdDataImageProcessingDlg, CDialogEx)
ON_WM_SYSCOMMAND()
// ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_IMAGE_INFO_SAVE_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedImageInfoSaveButton)
ON_BN_CLICKED(IDC_DLG_MIN_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedDlgMinButton)
ON_BN_CLICKED(IDC_DLG_MAX_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedDlgMaxButton)
ON_BN_CLICKED(IDC_IMAGE_GENERATE_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedImageGenerateButton)
ON_BN_CLICKED(IDC_SLECT_PATH_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedSlectPathButton)
ON_WM_PAINT()
ON_BN_CLICKED(IDC_SAVE_IMAGE_BUTTON, &CCcdDataImageProcessingDlg::OnBnClickedSaveImageButton)
ON_WM_TIMER()
END_MESSAGE_MAP()
// CCcdDataImageProcessingDlg 消息处理程序
BOOL CCcdDataImageProcessingDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。 当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
OnInitEnergyCombo();
OnInitEnergyInfo();
OnInitCcdDataPath();
OnInitAll();
SetTimer(0, 500, NULL);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CCcdDataImageProcessingDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCcdDataImageProcessingDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void CCcdDataImageProcessingDlg::OnBnClickedImageInfoSaveButton()
{
// 创建一个文件来保存信息
CString strFilePath = FILE_CCD_IMAGE_INFO_PATH;
CString ccdFilePath = FILE_CCD_DATAT_PATH;
// 创建 CStdioFile 对象,确保文件以 UTF-8 编码打开
CStdioFile file,ccdDataFilePath;
if (!file.Open(strFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeText)) {
AfxMessageBox(_T("无法创建文件!"));
return;
}
if (!ccdDataFilePath.Open(ccdFilePath, CFile::modeCreate | CFile::modeWrite | CFile::typeText)) {
AfxMessageBox(_T("无法创建文件!"));
return;
}
// 写入表头(列名),使用逗号分隔
file.WriteString(_T("Plan,MinEnergy,MaxEnergy,Color,TF,Proportion(%)\n"));
// 映射控件ID
CString cddDataPathInfo;
GetDlgItemText(IDC_CCD_DATA_PATH_EDIT, cddDataPathInfo);
ccdDataFilePath.WriteString(cddDataPathInfo);
// 循环处理每一组控件
for (int i = 0; i < 10; ++i) {
CString strPlan;
strPlan.Format(_T("%d"), i + 1);
// 获取编辑框内容
CString minEnergy, maxEnergy, proportionEnergy, energyCombo, colorCombo;
GetDlgItemText(minEnergyCtrlIds[i], minEnergy);
GetDlgItemText(maxEnergyCtrlIds[i], maxEnergy);
GetDlgItemText(proportionEnergyCtrlIds[i], proportionEnergy);
// 获取颜色组合框内容
CComboBox* pColorComboBox = (CComboBox*)GetDlgItem(colorComboCtrlIds[i]);
int colorSel = pColorComboBox->GetCurSel(); // 获取当前选择的索引
if (colorSel != CB_ERR) {
pColorComboBox->GetLBText(colorSel, colorCombo); // 获取颜色名称
}
else {
colorCombo = _T(""); // 如果没有选择,设置为空
}
// 获取TF组合框内容
CComboBox* pComboBox = (CComboBox*)GetDlgItem(energyComboCtrlIds[i]);
int energySel = pComboBox->GetCurSel(); // 获取当前选择的索引
if (energySel != CB_ERR) {
pComboBox->GetLBText(energySel, energyCombo); // 获取True/False
}
else {
energyCombo = _T(""); // 如果没有选择,设置为空
}
// 按照要求写入数据(包括颜色列),使用逗号分隔
file.WriteString(strPlan + _T(",") + minEnergy + _T(",") + maxEnergy +
_T(",") + colorCombo + _T(",") + energyCombo +
_T(",") + proportionEnergy + _T("\n"));
}
// 提示保存成功
//AfxMessageBox(_T("CCD图像颜色方案配比信息已保存"),MB_TOPMOST);
}
void CCcdDataImageProcessingDlg::OnBnClickedDlgMinButton()
{
// 最小化窗口并显示在任务栏
ShowWindow(SW_MINIMIZE);
}
void CCcdDataImageProcessingDlg::OnBnClickedDlgMaxButton()
{
}
void CCcdDataImageProcessingDlg::OnInitEnergyCombo()
{
// 初始化ComboBox控件添加True和False选项
CComboBox* energyCombos[] = {
&m_bEnergy1, &m_bEnergy2, &m_bEnergy3, &m_bEnergy4, &m_bEnergy5,
&m_bEnergy6, &m_bEnergy7, &m_bEnergy8, &m_bEnergy9, &m_bEnergy10
};
// 循环初始化所有Energy ComboBox控件
for (int i = 0; i < 10; ++i)
{
TRACE(_T("Initializing ComboBox %d\n"), i); // 打印调试信息
energyCombos[i]->AddString(_T("True"));
energyCombos[i]->AddString(_T("False"));
energyCombos[i]->SetCurSel(0); // 默认选择False
energyCombos[i]->ModifyStyle(CBS_SIMPLE, CBS_DROPDOWNLIST); // 设置为下拉列表框模式,不能编辑
}
// 初始化颜色 ComboBox控件
CComboBox* colorCombos[] = {
&m_Color1, &m_Color2, &m_Color3, &m_Color4, &m_Color5,
&m_Color6, &m_Color7, &m_Color8, &m_Color9, &m_Color10
};
// 循环初始化所有颜色 ComboBox控件
for (int i = 0; i < 10; ++i)
{
// 添加颜色名称到每个颜色控件
for (int j = 0; j < sizeof(colorNames) / sizeof(colorNames[0]); ++j)
{
colorCombos[i]->AddString(colorNames[j]);
}
colorCombos[i]->SetCurSel(0); // 默认选择第一个颜色
colorCombos[i]->ModifyStyle(0, CBS_DROPDOWNLIST); // 设置为下拉列表框模式,不能编辑
}
}
void CCcdDataImageProcessingDlg::OnInitEnergyInfo()
{
// 初始化 CEdit 控件,将其内容设置为 0
m_MinEnergy1.SetWindowText(_T("0"));
m_MaxEnergy1.SetWindowText(_T("0"));
m_MinEnergy2.SetWindowText(_T("0"));
m_MaxEnergy2.SetWindowText(_T("0"));
m_MinEnergy3.SetWindowText(_T("0"));
m_MaxEnergy3.SetWindowText(_T("0"));
m_MinEnergy4.SetWindowText(_T("0"));
m_MaxEnergy4.SetWindowText(_T("0"));
m_MinEnergy5.SetWindowText(_T("0"));
m_MaxEnergy5.SetWindowText(_T("0"));
m_MinEnergy6.SetWindowText(_T("0"));
m_MaxEnergy6.SetWindowText(_T("0"));
m_MinEnergy7.SetWindowText(_T("0"));
m_MaxEnergy7.SetWindowText(_T("0"));
m_MinEnergy8.SetWindowText(_T("0"));
m_MaxEnergy8.SetWindowText(_T("0"));
m_MinEnergy9.SetWindowText(_T("0"));
m_MaxEnergy9.SetWindowText(_T("0"));
m_MinEnergy10.SetWindowText(_T("0"));
m_MaxEnergy10.SetWindowText(_T("0"));
// 初始化 CEdit 控件中的配比值为 0
m_ProportionEnergy1.SetWindowText(_T("0"));
m_ProportionEnergy2.SetWindowText(_T("0"));
m_ProportionEnergy3.SetWindowText(_T("0"));
m_ProportionEnergy4.SetWindowText(_T("0"));
m_ProportionEnergy5.SetWindowText(_T("0"));
m_ProportionEnergy6.SetWindowText(_T("0"));
m_ProportionEnergy7.SetWindowText(_T("0"));
m_ProportionEnergy8.SetWindowText(_T("0"));
m_ProportionEnergy9.SetWindowText(_T("0"));
m_ProportionEnergy10.SetWindowText(_T("0"));
}
void CCcdDataImageProcessingDlg::OnInitCcdDataPath()
{
// 这里FILE_CCD_DATAT_PATH是宏定义你可以替换为具体的路径
CString strFilePath = FILE_CCD_DATAT_PATH;
CStdioFile file;
CString filePath;
// 打开文件进行读取
if (!file.Open(strFilePath, CFile::modeRead | CFile::typeText))
{
AfxMessageBox(_T("无法打开文件!"));
return;
}
// 读取第一行的数据
if (file.ReadString(filePath))
{
// 将读取到的数据设置到控件中
m_CddDataPath.SetWindowText(filePath);
}
else
{
AfxMessageBox(_T("文件为空或无法读取数据"));
}
// 关闭文件
file.Close();
}
void CCcdDataImageProcessingDlg::UpProportionEnergy()
{
// 获取总数
int totalEnergy = m_NumEnergy; // 假设 m_NumEnergy 存储总数
// 遍历 m_proportionEnergy 中的每个键值对
for (const auto& pair : m_proportionEnergy) {
int key = pair.first; // 获取键控件ID
int value = pair.second; // 获取值(每种能量的数量)
// 计算比例
double proportion = (static_cast<double>(value) * 100) / totalEnergy;
// 格式化比例为字符串
CString strProportion;
strProportion.Format(_T("%.2f%%"), proportion);
// 获取控件 ID 对应的控件指针
SetDlgItemText(key, strProportion); // 将比例设置到控件中
}
}
void CCcdDataImageProcessingDlg::OnInitAll()
{
// 定义文件路径
CString strFilePath = FILE_CCD_IMAGE_INFO_PATH;
// 检查文件是否存在
if (FileExists(strFilePath))
{
// 文件存在,从文件中加载数据
LoadEnergyDataFromFile(strFilePath);
}
}
// 检查文件是否存在的辅助函数
BOOL CCcdDataImageProcessingDlg::FileExists(const CString& strFilePath)
{
DWORD dwFileAttrib = GetFileAttributes(strFilePath);
return (dwFileAttrib != INVALID_FILE_ATTRIBUTES && !(dwFileAttrib & FILE_ATTRIBUTE_DIRECTORY));
}
void CCcdDataImageProcessingDlg::LoadEnergyDataFromFile(const CString& strFilePath)
{
CStdioFile file;
CString line;
CString minEnergy, maxEnergy, proportionEnergy, colorCombo, energyCombo;
// 打开文件进行读取
if (!file.Open(strFilePath, CFile::modeRead | CFile::typeText))
{
AfxMessageBox(_T("无法打开文件!"));
return;
}
// 跳过表头
file.ReadString(line);
// 循环读取每一行并更新控件
for (int i = 0; i < 10; ++i)
{
// 读取每一行数据
if (file.ReadString(line))
{
// 分析行内容,假设以逗号分隔
CStringArray fields;
int start = 0;
int end = line.Find(_T(','));
// 解析每一行的数据
while (end != -1)
{
fields.Add(line.Mid(start, end - start));
start = end + 1;
end = line.Find(_T(','), start);
}
fields.Add(line.Mid(start));
// 设置控件的值
if (fields.GetSize() >= 6)
{
// 设置MinEnergy和MaxEnergy
SetDlgItemText(minEnergyCtrlIds[i], fields[1]);
SetDlgItemText(maxEnergyCtrlIds[i], fields[2]);
// 设置Proportion
SetDlgItemText(proportionEnergyCtrlIds[i], fields[5]);
// 设置颜色
CComboBox* pColorCombo = (CComboBox*)GetDlgItem(colorComboCtrlIds[i]);
int colorIndex = pColorCombo->FindStringExact(-1, fields[3]);
if (colorIndex != CB_ERR)
{
pColorCombo->SetCurSel(colorIndex);
}
// 设置TF
CComboBox* pEnergyCombo = (CComboBox*)GetDlgItem(energyComboCtrlIds[i]);
int tfIndex = pEnergyCombo->FindStringExact(-1, fields[4]);
if (tfIndex != CB_ERR)
{
pEnergyCombo->SetCurSel(tfIndex);
}
}
}
}
// 关闭文件
file.Close();
}
void CCcdDataImageProcessingDlg::OnBnClickedSlectPathButton()
{
// 打开文件选择对话框
CFileDialog fileDlg(TRUE, _T("csv"), NULL, OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST, _T("Text Files (*.csv)|*.csv|All Files (*.*)|*.*||"));
// 显示文件选择对话框
if (fileDlg.DoModal() == IDOK)
{
// 获取选择的文件路径
CString filePath = fileDlg.GetPathName();
// 设置路径到 m_CddDataPath 控件
m_CddDataPath.SetWindowText(filePath);
}
}
void CCcdDataImageProcessingDlg::OnBnClickedImageGenerateButton()
{
CWaitCursor wait;
CString cddDataPathInfo;
GetDlgItemText(IDC_CCD_DATA_PATH_EDIT, cddDataPathInfo);
m_colorSchemeManager.addScheme("Scheme1"); // 添加多个颜色方案
//---获取所有方案参数--------------------------------------------
// 循环处理每一组控件
for (int i = 0; i < 10; ++i) {
CString strPlan;
strPlan.Format(_T("%d"), i + 1);
// 获取编辑框内容
CString minEnergy, maxEnergy, proportionEnergy, energyCombo, colorCombo;
GetDlgItemText(minEnergyCtrlIds[i], minEnergy);
GetDlgItemText(maxEnergyCtrlIds[i], maxEnergy);
//GetDlgItemText(proportionEnergyCtrlIds[i], proportionEnergy);
// 获取颜色组合框内容
CComboBox* pColorComboBox = (CComboBox*)GetDlgItem(colorComboCtrlIds[i]);
int colorSel = pColorComboBox->GetCurSel(); // 获取当前选择的索引
if (colorSel != CB_ERR) {
pColorComboBox->GetLBText(colorSel, colorCombo); // 获取颜色名称
}
else {
colorCombo = _T(""); // 如果没有选择,设置为空
}
// 获取TF组合框内容
CComboBox* pComboBox = (CComboBox*)GetDlgItem(energyComboCtrlIds[i]);
int energySel = pComboBox->GetCurSel(); // 获取当前选择的索引
if (energySel != CB_ERR) {
pComboBox->GetLBText(energySel, energyCombo); // 获取True/False
}
else {
energyCombo = _T(""); // 如果没有选择,设置为空
}
// 转换最小能量和最大能量
float minEnergyValue = _tstof(minEnergy); // 转换为 float
float maxEnergyValue = _tstof(maxEnergy); // 转换为 float
// 设置颜色
COLORREF color = RGB_WHITE; // 默认颜色为白色
if (colorCombo == colorNames[0]) { // Red
color = RGB_RED;
}
else if (colorCombo == colorNames[1]) { // Orange
color = RGB_ORANGE;
}
else if (colorCombo == colorNames[2]) { // Yellow
color = RGB_YELLOW;
}
else if (colorCombo == colorNames[3]) { // Green
color = RGB_GREEN;
}
else if (colorCombo == colorNames[4]) { // Cyan
color = RGB_CYAN;
}
else if (colorCombo == colorNames[5]) { // Blue
color = RGB_BLUE;
}
else if (colorCombo == colorNames[6]) { // Purple
color = RGB_PURPLE;
}
else if (colorCombo == colorNames[7]) { // Grey
color = RGB_GREY;
}
else if (colorCombo == colorNames[8]) { // Pink
color = RGB_PINK;
}
else if (colorCombo == colorNames[9]) { // Black
color = RGB_BLACK;
}
else if (colorCombo == colorNames[10]) { // White
color = RGB_WHITE;
}
else if (colorCombo == colorNames[11]) { // Brown
color = RGB_BROWN;
}
// 根据选择的能量区间添加范围
if (energyCombo == _T("True")) {
m_colorSchemeManager.schemes["Scheme1"].addRange(minEnergyValue, maxEnergyValue, color, proportionEnergyCtrlIds[i]); // 添加颜色范围
m_proportionEnergy[proportionEnergyCtrlIds[i]] = 0;
}
}
//---画图--------------------------------------------------------
m_colorSchemeManager.setCurrentScheme("Scheme1"); // 设置当前颜色方案为 Scheme1
gCsvToImg->m_colorSchemeManager = m_colorSchemeManager;
gCsvToImg->ReadCsv2Img(cddDataPathInfo);
auto Db2CString=[](float val)
{
CString str;
str.Format("%.3f", val);
return str;
};
auto &currentScheme = gCsvToImg->m_colorSchemeManager.currentScheme;
//CEdit 控件中的占比值 (没全部启用时会抛异常)
try
{
m_ProportionEnergy1.SetWindowText(Db2CString(currentScheme->ranges.at(0).dPercent));
m_ProportionEnergy2.SetWindowText(Db2CString(currentScheme->ranges.at(1).dPercent));
m_ProportionEnergy3.SetWindowText(Db2CString(currentScheme->ranges.at(2).dPercent));
m_ProportionEnergy4.SetWindowText(Db2CString(currentScheme->ranges.at(3).dPercent));
m_ProportionEnergy5.SetWindowText(Db2CString(currentScheme->ranges.at(4).dPercent));
m_ProportionEnergy6.SetWindowText(Db2CString(currentScheme->ranges.at(5).dPercent));
m_ProportionEnergy7.SetWindowText(Db2CString(currentScheme->ranges.at(6).dPercent));
m_ProportionEnergy8.SetWindowText(Db2CString(currentScheme->ranges.at(7).dPercent));
m_ProportionEnergy9.SetWindowText(Db2CString(currentScheme->ranges.at(8).dPercent));
m_ProportionEnergy10.SetWindowText(Db2CString(currentScheme->ranges.at(9).dPercent));
}
catch (...){}
UpdateWindow();
}
// 根据能量值设置颜色
COLORREF CCcdDataImageProcessingDlg::GetColorFromEnergy(float energy)
{
if (m_colorSchemeManager.currentScheme == nullptr) {
return RGB_WHITE;
}
return m_colorSchemeManager.currentScheme->getColor(energy);
}
//void CCcdDataImageProcessingDlg::OnPaint()
//{
//
//
// gCsvToImg->ShowLastImg(&m_PicCtrl);
// return;
//
// CPaintDC dc(this); // 设备上下文
// CRect clientRect;
// GetClientRect(&clientRect); // 获取客户区域矩形
//
// // 设置背景色
// dc.FillSolidRect(clientRect, RGB(255, 255, 255));
//
// // 设置字体
// CFont font;
// font.CreatePointFont(120, _T("Arial"));
// dc.SelectObject(&font);
//
// // 计算矩阵的行数和列数
// int numRows = m_data.size();
// int numCols = (numRows > 0) ? m_data[0].size() : 0;
//
// // 如果数据为空,提前返回
// if (numRows == 0 || numCols == 0) {
// dc.TextOut(10, 10, _T("No data available"));
// return;
// }
//
// // 获取窗口的宽度和高度
// int maxImageWidth = clientRect.Width(); // 使用窗口的宽度
// int maxImageHeight = clientRect.Height(); // 使用窗口的高度
//
// // 计算每个数据块的大小(正方形)
// float pointSize;
// if (numCols > 0) {
// // 根据列数和窗口宽度动态计算每个数据块的大小
// float maxPointSizeWidth = static_cast<float>(maxImageWidth) / numCols; // 计算每个数据块在宽度上的最大大小
// pointSize = static_cast<int>(maxPointSizeWidth); // 选择宽度限制的大小
// }
// else {
// pointSize = 10; // 默认大小
// }
//
// // 计算最大高度可用空间,动态计算每个数据块的高度
// int pointSizeHeight;
// if (numRows > 0) {
// // 根据行数和窗口高度动态计算每个数据块的大小
// float maxPointSizeHeight = static_cast<float>(maxImageHeight) / numRows; // 计算每个数据块在高度上的最大大小
// pointSizeHeight = static_cast<int>(maxPointSizeHeight); // 选择高度限制的大小
// }
// else {
// pointSizeHeight = 10; // 默认大小
// }
//
// // 取最小值,确保数据点能够适应窗口
// if (pointSize > pointSizeHeight)pointSize = pointSizeHeight;
//
// // 如果计算出的 pointSize 太小,设置一个最小值
// if (pointSize < 0.5) {
// pointSize = 0.5; // 确保每个数据点至少为 1
// }
//
// // 计算偏移量,使图像右贴右边,上边贴上边
// int offsetX = 0;
// int offsetY = 0;
//
// // 遍历并绘制每个数据点
// for (int row = 0; row < numRows; ++row) {
// for (int col = 0; col < numCols; ++col) {
// // 确保每个元素都有效
// if (row < m_data.size() && col < m_data[row].size()) {
// try {
// float energy = std::stof(m_data[row][col]); // 将字符串转换为浮动数
//
// // 数据统计
// if (energy < m_minEnergy) m_minEnergy = energy; // 更新最小值
// if (energy > m_maxEnergy) m_maxEnergy = energy; // 更新最大值
//
// m_NumEnergy++;
//
// // 获取能量值对应的颜色
// COLORREF color = GetColorFromEnergy(energy);
// dc.SetDCBrushColor(color); // 设置画刷颜色
//
// // 计算当前数据点位置(添加偏移量)
// float scaledX = col * pointSize + offsetX;
// float scaledY = row * pointSize + offsetY;
//
// // 绘制正方形数据块
// CRect rect(scaledX, scaledY, scaledX + pointSize, scaledY + pointSize);
// dc.FillSolidRect(rect, color);
// }
// catch (const std::invalid_argument& e) {
// // 处理无效的字符串,设置默认值或跳过
// continue; // 如果数据无法转换为 float跳过当前元素
// }
// }
// }
// }
//
// // 显示最大最小能量值
// CString str;
// str.Format(_T("Min Energy: %.2f, Max Energy: %.2f"), m_minEnergy, m_maxEnergy);
// dc.TextOut(0, 1300, str);
// UpProportionEnergy();
//}
void CCcdDataImageProcessingDlg::OnBnClickedSaveImageButton()
{
// 1. 使用文件对话框选择保存位置和文件名
CFileDialog saveFileDlg(FALSE, _T("png"), _T("saved_image.png"), OFN_OVERWRITEPROMPT, _T("PNG Files (*.png)|*.png|All Files (*.*)|*.*||"));
if (saveFileDlg.DoModal() == IDOK) {
// 获取用户选择的文件路径
CString savePath = saveFileDlg.GetPathName();
// 2. 调用图像保存函数
gCsvToImg->SaveLastImg(savePath);
//SaveImage(savePath);
}
}
void CCcdDataImageProcessingDlg::SaveImage(const CString& savePath)
{
// 创建一个 CImage 对象
CImage image;
// 获取当前绘图区域的尺寸
CClientDC dc(this); // 获取设备上下文
CRect rect;
GetClientRect(&rect);
// 创建图像24位颜色
image.Create(rect.Width(), rect.Height(), 24);
// 创建一个内存设备上下文,用于绘制到图像
CDC* pMemDC = CDC::FromHandle(image.GetDC());
// 将窗口内容复制到内存设备上下文
pMemDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dc, 0, 0, SRCCOPY);
// 释放设备上下文
image.ReleaseDC();
// 3. 保存图像到文件
HRESULT hr = image.Save(savePath, Gdiplus::ImageFormatPNG); // 保存为 PNG 格式
if (FAILED(hr)) {
AfxMessageBox(_T("保存图像失败"));
}
else {
AfxMessageBox(_T("图像已保存"));
}
}
void CCcdDataImageProcessingDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
gCsvToImg->ShowLastImg(&m_PicCtrl);
CDialogEx::OnTimer(nIDEvent);
}