📄 registgraph2d.cpp
字号:
// RegistGraph2D.cpp : implementation file//#include "stdafx.h"#include "fusion.h"#include "RegistGraph2D.h"#include "DlgRegistReport.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// RxRegistGraph2DRxRegistGraph2D::RxRegistGraph2D(){ m_dRangeMin = 0.; m_dRangeMax = 100.; m_dPos = 100.; m_iGripSize = 10;}RxRegistGraph2D::~RxRegistGraph2D(){ if(m_hWnd) DestroyWindow();}BEGIN_MESSAGE_MAP(RxRegistGraph2D, CWnd) //{{AFX_MSG_MAP(RxRegistGraph2D) ON_WM_PAINT() ON_WM_SIZE() ON_WM_SETCURSOR() ON_WM_MOUSEACTIVATE() ON_WM_LBUTTONDOWN() ON_WM_MOUSEWHEEL() //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// RxRegistGraph2D message handlersvoid RxRegistGraph2D::OnPaint() { CPaintDC dc(this); // device context for painting RedrawWnd(); // Do not call CWnd::OnPaint() for painting messages}void RxRegistGraph2D::RedrawWnd(){ RxDlgRegistReport *pParentWnd = (RxDlgRegistReport *)GetParent(); CDC *pDC = GetDC(); CRect rect; GetClientRect(rect); CDC dcMem; CBitmap bmpMem, *pOldBitmap; dcMem.CreateCompatibleDC(pDC); bmpMem.CreateCompatibleBitmap(pDC, rect.Width(), rect.Height()); pOldBitmap = dcMem.SelectObject(&bmpMem); // fill the background CBrush brush, *pOldBrush; brush.CreateSolidBrush(RGB( 0, 0, 0)); pOldBrush = dcMem.SelectObject(&brush); dcMem.PatBlt(0, 0, rect.Width(), rect.Height(), PATCOPY); dcMem.SelectObject(pOldBrush); brush.DeleteObject(); // draw border. CRect rcTemp = rect; rcTemp.DeflateRect(19,19,19,19); dcMem.Draw3dRect(rcTemp, RGB(200, 0, 0), RGB(200, 0, 0)); // draw horizontal index CFont font, *pOldFont; font.CreateFont(14, 0, 0, 0, FW_BOLD, FALSE, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_DONTCARE, _T("Arial")); pOldFont = dcMem.SelectObject(&font); dcMem.SetBkMode(TRANSPARENT); dcMem.SetTextColor(RGB(255,255,255)); CRect rcText; CString strText; rcText.SetRect(rcTemp.left, rcTemp.bottom, rcTemp.right, rect.bottom); strText.Format(_T("%d"), 0); dcMem.DrawText(strText, rcText, DT_SINGLELINE|DT_VCENTER|DT_LEFT); dcMem.DrawText(_T("Reference"), rcText, DT_SINGLELINE|DT_VCENTER|DT_CENTER); strText.Format(_T("%d"), pParentWnd->m_iCol); dcMem.DrawText(strText, rcText, DT_SINGLELINE|DT_VCENTER|DT_RIGHT); dcMem.SelectObject(pOldFont); font.DeleteObject(); // draw vertical index CPoint ptOldViewportOrg = dcMem.SetViewportOrg(rect.left, rcTemp.bottom); LOGFONT lf; ::ZeroMemory(&lf, sizeof(lf)); lf.lfHeight = 83; lf.lfWeight = FW_BOLD; lf.lfEscapement = 900; lf.lfOrientation = 900; ::lstrcpy(lf.lfFaceName, _T("Arial")); font.CreatePointFontIndirect(&lf); pOldFont = dcMem.SelectObject(&font); strText.Format(_T("%d"), 0); dcMem.TextOut(3, 0, strText); strText = _T("Float"); dcMem.TextOut(3, -((rcTemp.Height() - dcMem.GetTextExtent(strText).cx)/2), strText); strText.Format(_T("%d"), pParentWnd->m_iRow); dcMem.TextOut(3, -(rcTemp.Height() - dcMem.GetTextExtent(strText).cx), strText); dcMem.SelectObject(pOldFont); font.DeleteObject(); dcMem.SetViewportOrg(ptOldViewportOrg); // draw slider DrawSlider(&dcMem); // draw points rcTemp.DeflateRect(1,1,1,1); CPoint point; int iIndexX, iIndexY; int i, j; double dRatioX = (double)(pParentWnd->m_iCol) / (double)(rcTemp.right - rcTemp.left); double dRatioY = (double)(pParentWnd->m_iRow) / (double)(rcTemp.bottom - rcTemp.top); double dFactor; int iColorR, iColorG, iColorB; for(i = rcTemp.top; i < rcTemp.bottom; i++) { iIndexY = (pParentWnd->m_iRow - 1) - (int)((i - rcTemp.top) * dRatioY); if(iIndexY <= 0) iIndexY = 0; for(j = rcTemp.left; j < rcTemp.right; j++) { iIndexX = (int)((j - rcTemp.left) * dRatioX); if(iIndexX >= pParentWnd->m_iCol - 1) iIndexX = pParentWnd->m_iCol - 1; if(m_dPos <= m_dRangeMin) dFactor = (pParentWnd->m_ppTable[iIndexY][iIndexX] - m_dRangeMin); else dFactor = (pParentWnd->m_ppTable[iIndexY][iIndexX] - m_dRangeMin) / (m_dPos - m_dRangeMin); iColorR = (int)(dFactor * 255); iColorG = (int)(dFactor * 255); iColorB = (int)(dFactor * 255); point.x = j; point.y = i; dcMem.SetPixel(point, RGB(iColorR, iColorG, iColorB)); } } pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &dcMem, 0, 0, SRCCOPY); dcMem.SelectObject(pOldBitmap); bmpMem.DeleteObject(); dcMem.DeleteDC(); ReleaseDC(pDC);}void RxRegistGraph2D::DrawSlider(CDC *pDC){ pDC->Draw3dRect(m_rcSlider, RGB(255,255,255), RGB(255,255,255)); CRect rcGrip; rcGrip.SetRect(m_rcSlider.left+1, m_iPixel, m_rcSlider.right-1, m_iPixel+m_iGripSize); pDC->Draw3dRect(rcGrip, RGB(255, 0, 0), RGB(255, 0, 0));}void RxRegistGraph2D::OnSize(UINT nType, int cx, int cy) { CWnd::OnSize(nType, cx, cy); m_rcSlider.SetRect(cx - 16, 19, cx-2, cy - 19); UpdateGripPixel(TRUE);}void RxRegistGraph2D::UpdateGripPixel(BOOL bUpdate){ if(bUpdate) { m_iPixel = (int)((m_dPos - m_dRangeMin) / (m_dRangeMax - m_dRangeMin) * (double)(m_rcSlider.top+1 - (m_rcSlider.bottom-1 - m_iGripSize)) + m_rcSlider.bottom-1 - m_iGripSize); if(m_iPixel <= m_rcSlider.top+1) m_iPixel = m_rcSlider.top+1; else if(m_iPixel >= (m_rcSlider.bottom-1 - m_iGripSize)) m_iPixel = m_rcSlider.bottom-1 - m_iGripSize; } else { m_dPos = (double)(m_iPixel - (m_rcSlider.bottom-1 - m_iGripSize)) / (double)(m_rcSlider.top+1 - (m_rcSlider.bottom-1 - m_iGripSize)) * (m_dRangeMax - m_dRangeMin) + m_dRangeMin; if(m_dPos <= m_dRangeMin) m_dPos = m_dRangeMin; if(m_dPos >= m_dRangeMax) m_dPos = m_dRangeMax; }}BOOL RxRegistGraph2D::HitTestGrip(CPoint point){ CRect rcGrip; rcGrip.SetRect(m_rcSlider.left, m_iPixel, m_rcSlider.right, m_iPixel + m_iGripSize); return rcGrip.PtInRect(point);}BOOL RxRegistGraph2D::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { POINT point; GetCursorPos(&point); ScreenToClient(&point); if(HitTestGrip(point)) { SetCursor(AfxGetApp()->LoadStandardCursor(MAKEINTRESOURCE(32649))); return TRUE; } return CWnd::OnSetCursor(pWnd, nHitTest, message);}int RxRegistGraph2D::OnMouseActivate(CWnd* pDesktopWnd, UINT nHitTest, UINT message) { HWND hWndFocus = ::GetFocus(); if (m_hWnd != hWndFocus && !::IsChild(m_hWnd, hWndFocus)) ::SetFocus(m_hWnd); return CWnd::OnMouseActivate(pDesktopWnd, nHitTest, message);}void RxRegistGraph2D::OnLButtonDown(UINT nFlags, CPoint point) { if(!HitTestGrip(point)) { CWnd::OnLButtonDown(nFlags, point); return; } int iDiff, iOldPixel; iDiff = point.y - m_iPixel; SetCapture(); for (;;) { MSG msg; VERIFY(::GetMessage(&msg, NULL, 0, 0)); ScreenToClient(&msg.pt); switch (msg.message) { case WM_MOUSEMOVE : // Set Pixel Position iOldPixel = m_iPixel; m_iPixel = msg.pt.y - iDiff; if(m_iPixel < m_rcSlider.top+1) m_iPixel = m_rcSlider.top+1; else if(m_iPixel > (m_rcSlider.bottom-1 - m_iGripSize)) m_iPixel = m_rcSlider.bottom-1 - m_iGripSize; if(iOldPixel != m_iPixel) { UpdateGripPixel(FALSE); RedrawWnd(); } break; case WM_LBUTTONUP : goto EndLButtonDown; default : // just dispatch rest of the messages DispatchMessage(&msg); break; } }EndLButtonDown : ReleaseCapture(); CWnd::OnLButtonDown(nFlags, point);}BOOL RxRegistGraph2D::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) { if(zDelta >= 0) { double dOldPos = m_dPos; m_dPos += 2; if(m_dPos >= m_dRangeMax) m_dPos = m_dRangeMax; if(dOldPos != m_dPos) { UpdateGripPixel(TRUE); RedrawWnd(); } } else { double dOldPos = m_dPos; m_dPos -= 2; if(m_dPos <= m_dRangeMin) m_dPos = m_dRangeMin; if(dOldPos != m_dPos) { UpdateGripPixel(TRUE); RedrawWnd(); } } return CWnd::OnMouseWheel(nFlags, zDelta, pt);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -