DlgImageScissor.cpp
上传用户:gzboli
上传日期:2013-04-10
资源大小:471k
文件大小:7k
- // DlgImageScissor.cpp : implementation file
- //
- #include "stdafx.h"
- #include "QuickImage.h"
- #include "DlgImageScissor.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CDlgImageScissor dialog
- CDlgImageScissor::CDlgImageScissor(HDIB hDIB, CWnd* pParent /*=NULL*/)
- : CDialog(CDlgImageScissor::IDD, pParent), m_hDIB(hDIB)
- {
- //{{AFX_DATA_INIT(CDlgImageScissor)
- m_iZoom = 1;
- m_ibrX = 0;
- m_ibrY = 0;
- m_itlX = 0;
- m_itlY = 0;
- //}}AFX_DATA_INIT
- m_iCorner = 0;
- // m_pPalette =new CPalette;
- }
- void CDlgImageScissor::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CDlgImageScissor)
- DDX_Text(pDX, IDC_EDIT_ZOOM, m_iZoom);
- DDV_MinMaxUInt(pDX, m_iZoom, 1, 10);
- DDX_Text(pDX, IDC_EDIT_BR_X, m_ibrX);
- DDX_Text(pDX, IDC_EDIT_BR_Y, m_ibrY);
- DDX_Text(pDX, IDC_EDIT_TL_X, m_itlX);
- DDX_Text(pDX, IDC_EDIT_TL_Y, m_itlY);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CDlgImageScissor, CDialog)
- //{{AFX_MSG_MAP(CDlgImageScissor)
- ON_WM_CTLCOLOR()
- ON_WM_PAINT()
- ON_EN_KILLFOCUS(IDC_EDIT_ZOOM, OnKillfocusEditZoom)
- ON_WM_MOUSEMOVE()
- ON_WM_LBUTTONUP()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- ON_CONTROL_RANGE(EN_SETFOCUS, IDC_EDIT_TL_X, IDC_EDIT_BR_Y, OnEditsSetFocus)
- ON_CONTROL_RANGE(EN_KILLFOCUS, IDC_EDIT_TL_X, IDC_EDIT_BR_Y, OnEditsKillFocus)
- ON_CONTROL_RANGE(WM_LBUTTONDOWN, IDC_EDIT_TL_X, IDC_EDIT_BR_Y, OnEditsMouseUp)
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CDlgImageScissor message handlers
- HBRUSH CDlgImageScissor::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
- {
- HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
-
- CWnd *pEdit = GetDlgItem(IDC_EDIT_ZOOM);
- ASSERT_VALID(pEdit);
- if(pEdit->m_hWnd == pWnd->m_hWnd)
- {
- CBrush b;
- COLORREF color = (COLORREF)::GetSysColor(COLOR_BTNFACE);
- b.CreateSolidBrush(color);
- CRect rc;
- pWnd->GetClientRect(&rc);
- pDC->FillRect(&rc,&b);
- pDC->SetBkColor(color);
- }
- if(nCtlColor == CTLCOLOR_STATIC || nCtlColor == CTLCOLOR_EDIT)
- pDC->SetTextColor(RGB(0,96,249));
- // TODO: Return a different brush if the default is not desired
- return hbr;
- }
- BOOL CDlgImageScissor::OnInitDialog()
- {
- CDialog::OnInitDialog();
- m_btOK.SubclassDlgItem(IDOK, this);
- m_btOK.SetIcon(IDI_OKAC, IDI_OKUN);
- m_btOK.SetActiveBgColor(RGB(220,220,220));
- m_btOK.SetActiveFgColor(RGB(255,0,0));
- m_btOK.SetBtnCursor(IDC_GREEN);
-
- m_btCancel.SubclassDlgItem(IDCANCEL, this);
- m_btCancel.SetIcon(IDI_CANCELAC, IDI_CANCELUN);
- m_btCancel.SetActiveBgColor(RGB(220,220,220));
- m_btCancel.SetActiveFgColor(RGB(255,0,0));
- m_btCancel.SetBtnCursor(IDC_GREEN);
-
- // HDIB hDIB = GetDocument()->GetHDIB();
- if (m_hDIB != NULL)
- {
- LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) m_hDIB);
- m_iWidth = (int) ::DIBWidth(lpDIB); // Size of DIB - x
- m_iHeight = (int) abs(::DIBHeight(lpDIB)); // Size of DIB - y
- ::GlobalUnlock((HGLOBAL) m_hDIB);
- if(m_itlX < 0)
- {
- m_itlX = 0;
- }
- if(m_itlY < 0)
- {
- m_itlY = 0;
- }
- if(m_ibrX > m_iWidth)
- {
- m_ibrX = m_iWidth;
- }
- if(m_ibrY > m_iHeight)
- {
- m_ibrY = m_iHeight;
- }
- if(m_iHeight > 0)
- {
- int iTemp = m_itlY;
- m_itlY = m_iHeight - m_ibrY;
- m_ibrY = m_iHeight - iTemp;
- }
- else
- {
- m_iHeight = -m_iHeight;
- }
- UpdateData(FALSE);
- }
-
- return TRUE; // return TRUE unless you set the focus to a control
- // EXCEPTION: OCX Property Pages should return FALSE
- }
- void CDlgImageScissor::DrawAll()
- {
- CWnd *pWnd = GetDlgItem(IDC_PIC_LARGE);
- ASSERT_VALID(pWnd);
- CDC *pDC = pWnd->GetDC();
- ASSERT_VALID(pDC);
- CRect rcDC;
- CPoint ptDCCent;
- pWnd->GetClientRect(&rcDC);
- ptDCCent = rcDC.CenterPoint();
- if(m_itlX < 0)m_itlX = 0;
- if(m_itlY < 0)m_itlY = 0;
- if(m_ibrX > m_iWidth)m_ibrX = m_iWidth;
- if(m_ibrY > m_iHeight)m_ibrY = m_iHeight;
- double dx = rcDC.Width() / double(m_ibrX - m_itlX)/2.0;
- double dy =rcDC.Height() / double(m_ibrY - m_itlY)/2.0;
- if(dx > dy)
- {
- rcDC.left = ptDCCent.x - long((m_ibrX - m_itlX)*dy);
- rcDC.right = ptDCCent.x + long((m_ibrX - m_itlX)*dy);
- }
- else
- {
- rcDC.top = ptDCCent.y - long((m_ibrY - m_itlY)*dx);
- rcDC.bottom = ptDCCent.y + long((m_ibrY - m_itlY)*dx);
- }
-
- /* CRect rcDC;
- GetClientRect(&rcDC);
- rcDC.left += 100;
- rcDC.right -= 100;
- rcDC.top += 50;
- rcDC.bottom -= 200;
- */ pDC->Rectangle(rcDC);
- ::PaintDIB(pDC->GetSafeHdc(), rcDC, m_hDIB,
- CRect(m_itlX,m_itlY,m_ibrX,m_ibrY),NULL);
- }
- void CDlgImageScissor::DrawCorner()
- {
- CWnd *pWnd = GetDlgItem(IDC_PIC_SMALL);
- ASSERT_VALID(pWnd);
- CDC *pDC = pWnd->GetDC();
- ASSERT_VALID(pDC);
-
- CRect rcDC, rcDIB;
- pWnd->GetClientRect(&rcDC);
- CPoint pt = rcDC.CenterPoint();
-
- int x, y;
- if(m_iCorner == 0)
- {
- x = m_itlX;
- y = m_ibrY;
- }
- else if(m_iCorner == 1)
- {
- x = m_ibrX;
- y = m_itlY;
- }
- int iHalf = int(rcDC.Width() / (double)m_iZoom / 2.0);
- rcDIB.left = x - iHalf;
- rcDIB.right = x + iHalf;
- iHalf = int(rcDC.Height() / (double)m_iZoom / 2.0);
- rcDIB.top = y - iHalf;
- rcDIB.bottom = y + iHalf;
- ::PaintDIB(pDC->GetSafeHdc(), rcDC, m_hDIB, rcDIB, NULL);
- pDC->MoveTo(0, pt.y);
- pDC->LineTo(rcDC.Width(), pt.y);
- pDC->MoveTo(pt.x, 0);
- pDC->LineTo(pt.x, rcDC.Height());
- }
- void CDlgImageScissor::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
-
- DrawAll();
- DrawCorner();
-
- // Do not call CDialog::OnPaint() for painting messages
- }
- void CDlgImageScissor::OnEditsSetFocus(UINT uID)
- {
- CEdit *pEdit = (CEdit*)GetDlgItem(uID);
- ASSERT_VALID(pEdit);
- pEdit->SetSel(0,-1);
- switch(uID)
- {
- case IDC_EDIT_TL_X:
- case IDC_EDIT_TL_Y:
- m_iCorner = 0;
- DrawCorner();
- break;
- case IDC_EDIT_BR_X:
- case IDC_EDIT_BR_Y:
- m_iCorner = 1;
- DrawCorner();
- break;
- default:
- ASSERT(FALSE);
- }
- }
- void CDlgImageScissor::OnEditsKillFocus(UINT uID)
- {
- CWnd *pWnd = GetDlgItem(IDC_PIC_LARGE);
- ASSERT_VALID(pWnd);
- CRect rcPic;
- pWnd ->GetClientRect(&rcPic);
- CDC *pDC = pWnd->GetDC();
- ASSERT_VALID(pDC);
- CBrush b;
- b.CreateSolidBrush((COLORREF)::GetSysColor(COLOR_BTNFACE));
- pDC->FillRect(rcPic, &b);
- UpdateData();
- DrawAll();
- DrawCorner();
- }
- void CDlgImageScissor::OnEditsMouseUp(UINT uID)
- {
- CEdit *pEdit = (CEdit*)GetDlgItem(uID);
- ASSERT_VALID(pEdit);
- pEdit->SetSel(0,-1);
- }
- void CDlgImageScissor::OnKillfocusEditZoom()
- {
- UpdateData();
- DrawCorner();
- }
- void CDlgImageScissor::OnLButtonDown(UINT nFlags, CPoint point)
- {
- CDialog::OnLButtonDown(nFlags, point);
- }
- void CDlgImageScissor::OnMouseMove(UINT nFlags, CPoint point)
- {
- CDialog::OnMouseMove(nFlags, point);
- }
- void CDlgImageScissor::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CDialog::OnLButtonUp(nFlags, point);
- }