// ResizDlg.cpp : implementation file // #include "stdafx.h" #include "MagGlass.h" #include "ResizDlg.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CResizeDlg dialog CResizeDlg::CResizeDlg(int nValue, RESIZE_DLG nResizeDlg, CWnd* pParent /*=NULL*/) : CDialog(CResizeDlg::IDD, pParent) { //{{AFX_DATA_INIT(CResizeDlg) m_nValue = 0; //}}AFX_DATA_INIT m_nValue = nValue; switch (nResizeDlg) { case WIDTH_RESIZE_DLG: m_nStep = STEP_RESIZE_WH; m_nMinRange = MIN_RESIZE_WH; m_nMaxRange = MAX_RESIZE_WH; wsprintf((char*)m_pszDlgText, "Resize Magnifying Glass Width"); wsprintf((char*)m_pszDlgMsgText, "The width range is between %d and %d pixels", MIN_RESIZE_WH, MAX_RESIZE_WH); break; case HEIGHT_RESIZE_DLG: m_nStep = STEP_RESIZE_WH; m_nMinRange = MIN_RESIZE_WH; m_nMaxRange = MAX_RESIZE_WH; wsprintf((char*)m_pszDlgText, "Resize Magnifying Glass Height"); wsprintf((char*)m_pszDlgMsgText, "The height range is between %d and %d pixels", MIN_RESIZE_WH, MAX_RESIZE_WH); break; case BORDER_RESIZE_DLG: m_nStep = STEP_BORDER_SIZE; m_nMinRange = MIN_BORDER_SIZE; m_nMaxRange = MAX_BORDER_SIZE; wsprintf((char*)m_pszDlgText, "Resize Magnifying Glass Border"); wsprintf((char*)m_pszDlgMsgText, "The border range is between %d and %d pixels", MIN_BORDER_SIZE, MAX_BORDER_SIZE); break; case ZOOM_RESIZE_DLG: m_nStep = STEP_ZOOM_FACTOR; m_nMinRange = MIN_ZOOM_FACTOR; m_nMaxRange = MAX_ZOOM_FACTOR; wsprintf((char*)m_pszDlgText, "Magnifying Glass Zoom Factor"); wsprintf((char*)m_pszDlgMsgText, "The zoom factor range is between %d and %d pixels", MIN_ZOOM_FACTOR, MAX_ZOOM_FACTOR); break; default: AfxMessageBox("Error initializing the Resize dialog!"); break; } } void CResizeDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CResizeDlg) DDX_Text(pDX, IDC_EDIT, m_nValue); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CResizeDlg, CDialog) //{{AFX_MSG_MAP(CResizeDlg) ON_WM_HSCROLL() ON_EN_CHANGE(IDC_EDIT, OnChangeEdit) ON_EN_KILLFOCUS(IDC_EDIT, OnKillfocusEdit) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CResizeDlg message handlers BOOL CResizeDlg::OnInitDialog() { CDialog::OnInitDialog(); CWnd* pTextWnd = NULL; CWnd* pScrollWnd = NULL; SetWindowText((char*)m_pszDlgText); pTextWnd = GetDlgItem(IDC_MSG_TEXT); if (!pTextWnd) return FALSE; pTextWnd->SetWindowText((char*)m_pszDlgMsgText); pScrollWnd = GetDlgItem(IDC_HSCROLLBAR); if (!pScrollWnd) return FALSE; pScrollWnd->SetScrollRange(SB_CTL, m_nMinRange, m_nMaxRange, TRUE); pScrollWnd->SetScrollPos(SB_CTL, m_nValue, TRUE); return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } void CResizeDlg::OnOK() { if (m_nValue < m_nMinRange || m_nValue > m_nMaxRange) { AfxMessageBox("The entered value must be within the range!"); return; } CDialog::OnOK(); } void CResizeDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { int nScrollInc; CWnd* pScrollWnd = NULL; pScrollWnd = GetDlgItem(IDC_HSCROLLBAR); if (!pScrollWnd) return; switch (nSBCode) { /* Decrement by current scroll position */ case SB_LEFT: nScrollInc = m_nMinRange - m_nValue; break; /* Increment by the difference between Max scroll and current scroll position */ case SB_RIGHT: nScrollInc = m_nMaxRange - m_nValue; break; /* Decrement by one step */ case SB_LINELEFT: nScrollInc = -m_nStep; break; /* Increment by one step */ case SB_LINERIGHT: nScrollInc = m_nStep; break; /* Decrement by Maximum */ case SB_PAGELEFT: nScrollInc = -max (m_nStep, (m_nMaxRange - m_nStep)); break; /* Increment by Maximum */ case SB_PAGERIGHT: nScrollInc = max (m_nStep, (m_nMaxRange - m_nStep)); break; /* Increment by difference between current scroll position and THUMB position */ case SB_THUMBPOSITION: case SB_THUMBTRACK: nScrollInc = nPos - m_nValue; break; /* No Increment */ default: nScrollInc = 0; break; } nScrollInc = max(m_nMinRange - m_nValue, min (nScrollInc, (m_nMaxRange - m_nValue))); /* if Scroll increment is greater than nScrollInc, Increment scroll position by nScrollInc */ m_nValue += nScrollInc; /* Set new scroll position at current scroll position; */ pScrollWnd->SetScrollPos(SB_CTL, m_nValue, TRUE); pScrollWnd->SetDlgItemInt(IDC_EDIT, m_nValue, FALSE); UpdateData(FALSE); CDialog::OnHScroll(nSBCode, nPos, pScrollBar); } void CResizeDlg::OnChangeEdit() { CWnd* pScrollWnd = NULL; pScrollWnd = GetDlgItem(IDC_HSCROLLBAR); if (!pScrollWnd) return; UpdateData(TRUE); if(m_nValue >= m_nMinRange && m_nValue <= m_nMaxRange) { pScrollWnd->SetScrollPos(SB_CTL, m_nValue, TRUE); } } void CResizeDlg::OnKillfocusEdit() { CWnd* pScrollWnd = NULL; pScrollWnd = GetDlgItem(IDC_HSCROLLBAR); if (!pScrollWnd) return; UpdateData(TRUE); if(m_nValue < m_nMinRange || m_nValue > m_nMaxRange) { m_nValue = pScrollWnd->GetScrollPos(SB_CTL); UpdateData(FALSE); } }