// CPanelEquipment.cpp: 实现文件 // #include "stdafx.h" #include "Servo.h" #include "CPanelEquipment.h" #include "afxdialogex.h" #include "Common.h" #include "VerticalLine.h" // CPanelEquipment 对话框 IMPLEMENT_DYNAMIC(CPanelEquipment, CDialogEx) CPanelEquipment::CPanelEquipment(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_PANEL_EQUIPMENT, pParent) { m_crBkgnd = PANEL_EQUIPMENT_BACKGROUND_COLOR; m_hbrBkgnd = nullptr; m_nPanelWidth = 188; m_pEquipment = nullptr; } CPanelEquipment::~CPanelEquipment() { } void CPanelEquipment::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); DDX_Control(pDX, IDC_MFCPROPERTYGRID1, m_gridCtrl); } BEGIN_MESSAGE_MAP(CPanelEquipment, CDialogEx) ON_WM_CTLCOLOR() ON_WM_DESTROY() ON_WM_SIZE() ON_NOTIFY(BYVERTICALLINE_MOVEX, IDC_LINE1, &CPanelEquipment::OnVLineMoveX) ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CPanelEquipment::OnBnClickedButtonClose) END_MESSAGE_MAP() // CPanelEquipment 消息处理程序 int CPanelEquipment::getPanelWidth() { return m_nPanelWidth; } void CPanelEquipment::SetEquipment(SERVO::CEquipment* pEquipment) { m_pEquipment = pEquipment; ASSERT(m_pEquipment); if (::IsWindow(m_hWnd)) { SetDlgItemText(IDC_LABEL_TITLE, m_pEquipment->getName().c_str()); } } void CPanelEquipment::loadDataFromEquipment(SERVO::CEquipment* pEquipment) { m_pEquipment = pEquipment; ASSERT(m_pEquipment); // 先清空所有 m_gridCtrl.RemoveAll(); // 加载数据 SetDlgItemText(IDC_LABEL_TITLE, pEquipment->getName().c_str()); SERVO::CAttributeVector attrubutes; pEquipment->getAttributeVector(attrubutes); unsigned int nSize = attrubutes.size(); for (unsigned int i = 0; i < nSize; i++) { SERVO::CAttribute* pAttribute = attrubutes.getAttribute(i); CMFCPropertyGridProperty* pProperty = new CMFCPropertyGridProperty( pAttribute->getName().c_str(), pAttribute->getValue().c_str(), pAttribute->getDescription().c_str()); pProperty->AllowEdit(FALSE); m_gridCtrl.AddProperty(pProperty); } } BOOL CPanelEquipment::OnInitDialog() { CDialogEx::OnInitDialog(); // 关闭按钮 CString strIcon1; strIcon1.Format(_T("%s\\Res\\panel_close_24_b.ico"), theApp.m_strAppDir); HICON hIcon1 = (HICON)::LoadImage(AfxGetInstanceHandle(), strIcon1, IMAGE_ICON, 24, 24, LR_LOADFROMFILE | LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_DEFAULTSIZE); m_btnClose.SubclassDlgItem(IDC_BUTTON_CLOSE, this); m_btnClose.SetIcon(hIcon1, hIcon1, 24); m_btnClose.SetFaceColor(m_crBkgnd); m_btnClose.SetFrameColor(m_crBkgnd); m_btnClose.SetFrameColor(BS_HOVER, RGB(218, 218, 218)); m_btnClose.SetFrameColor(BS_PRESS, RGB(168, 168, 168)); CVerticalLine* pLine1 = CVerticalLine::Hook(GetDlgItem(IDC_LINE1)->GetSafeHwnd()); pLine1->SetBkgndColor(RGB(225, 225, 225)); pLine1->SetLineColor(RGB(198, 198, 198)); pLine1->EnableResize(); // 读取面板宽 CString strIniFile; strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile); m_nPanelWidth = GetPrivateProfileInt(_T("App"), _T("EquipmentPanelWidth"), int((double)GetSystemMetrics(SM_CXSCREEN) * 0.25), (LPTSTR)(LPCTSTR)strIniFile); return TRUE; // return TRUE unless you set the focus to a control // 异常: OCX 属性页应返回 FALSE } HBRUSH CPanelEquipment::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialogEx::OnCtlColor(pDC, pWnd, nCtlColor); if (nCtlColor == CTLCOLOR_STATIC) { pDC->SetBkColor(m_crBkgnd); pDC->SetTextColor(RGB(0, 0, 0)); } if (m_hbrBkgnd == nullptr) { m_hbrBkgnd = CreateSolidBrush(m_crBkgnd); } return m_hbrBkgnd; } void CPanelEquipment::OnDestroy() { CDialogEx::OnDestroy(); if (m_hbrBkgnd != nullptr) { ::DeleteObject(m_hbrBkgnd); } } void CPanelEquipment::OnSize(UINT nType, int cx, int cy) { CDialogEx::OnSize(nType, cx, cy); if (GetDlgItem(IDC_LINE1) == nullptr) return; CWnd* pItem; CRect rcClient, rcItem; GetClientRect(&rcClient); pItem = GetDlgItem(IDC_LINE1); pItem->MoveWindow(rcClient.right - 3, 0, 3, rcClient.Height()); int x2 = rcClient.right - 6; int y = 3; pItem = GetDlgItem(IDC_BUTTON_CLOSE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(x2 - rcItem.Width(), y, rcItem.Width(), rcItem.Height()); x2 -= rcItem.Width(); x2 -= 3; pItem = GetDlgItem(IDC_LABEL_TITLE); pItem->GetWindowRect(&rcItem); pItem->MoveWindow(5, y, x2 - 5, rcItem.Height()); y += rcItem.Height(); y += 8; GetDlgItem(IDC_MFCPROPERTYGRID1)->MoveWindow(5, y, rcClient.Width() - 13, rcClient.Height() - 3 - y); } #define EQUIPMENT_PANEL_MIN_WIDTH 88 #define EQUIPMENT_PANEL_MAX_WIDTH 588 void CPanelEquipment::OnVLineMoveX(NMHDR* nmhdr, LRESULT* result) { BYVERTICALLINE_NMHDR* pNmhdrex = (BYVERTICALLINE_NMHDR*)nmhdr; int x = pNmhdrex->dwData; m_nPanelWidth += x; m_nPanelWidth = max(m_nPanelWidth, EQUIPMENT_PANEL_MIN_WIDTH); m_nPanelWidth = min(m_nPanelWidth, EQUIPMENT_PANEL_MAX_WIDTH); GetParent()->SendMessage(ID_MSG_PANEL_RESIZE, m_nPanelWidth, 0); CString strIniFile, strValue; strIniFile.Format(_T("%s\\%s.ini"), (LPTSTR)(LPCTSTR)theApp.m_strAppDir, (LPTSTR)(LPCTSTR)theApp.m_strAppFile); strValue.Format(_T("%d"), m_nPanelWidth); WritePrivateProfileString(_T("App"), _T("EquipmentPanelWidth"), (LPTSTR)(LPCTSTR)strValue, (LPTSTR)(LPCTSTR)strIniFile); OnSize(0, 0, 0); *result = 0; } void CPanelEquipment::OnBnClickedButtonClose() { ShowWindow(SW_HIDE); GetParent()->SendMessage(ID_MSG_PANEL_RESIZE, m_nPanelWidth, 0); }