www.pudn.com > Image_Manage.rar > DlgGrid.cpp
// DlgGrid.cpp : implementation file // #include "stdafx.h" #include "Image_Manage.h" #include "Image_ManageDoc.h" #include "DlgGrid.h" #include#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif HDIB hDIB=NULL; CPalette* palDIB=NULL; ///////////////////////////////////////////////////////////////////////////// // CDlgGrid dialog CDlgGrid::CDlgGrid(CWnd* pParent /*=NULL*/) : CDialog(CDlgGrid::IDD, pParent) { //{{AFX_DATA_INIT(CDlgGrid) m_pixels = 10; //}}AFX_DATA_INIT rtSel.SetRect(10,10,RT_WIDTH+10,RT_WIDTH+10); rcDest.SetRect(0,0,0,0); rcDIB.SetRect(0,0,0,0); bLButtonDown=FALSE; Oldx=0; Oldx=0; int nBitCount=0; int cxDIB=0; int cyDIB=0; dxy=0.0; LPSTR lpDIBBits=NULL; //srand((unsigned)time(NULL)); } void CDlgGrid::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgGrid) DDX_Text(pDX, IDC_EDIT_Num, m_pixels); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgGrid, CDialog) //{{AFX_MSG_MAP(CDlgGrid) ON_WM_PAINT() ON_WM_LBUTTONDOWN() ON_WM_LBUTTONUP() ON_WM_MOUSEMOVE() ON_WM_CTLCOLOR() //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgGrid message handlers void CDlgGrid::OnOK() { CRect rt; rt.SetRect(rcDest.right+10,rcDest.top,rcDest.right+10+m_pixels*40,rcDest.top+m_pixels*40); InvalidateRect(rt); // 更新 UpdateData(TRUE); rt.SetRect(rcDest.right+10,rcDest.top,rcDest.right+10+m_pixels*40,rcDest.top+m_pixels*40); InvalidateRect(rt); } BOOL CDlgGrid::OnInitDialog() { CDialog::OnInitDialog(); // 判断DIB是否为空 if (hDIB == NULL) { AfxMessageBox(TEXT("位图开取出现错误,请重试!")); OnCancel(); return FALSE; } // 锁定DIB LPSTR lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) hDIB); // 像素位数 nBitCount = (int) ::DIBBitCount(lpDIB); // 获取DIB宽度 cxDIB = (int) ::DIBWidth(lpDIB); // 获取DIB高度 cyDIB = (int) ::DIBHeight(lpDIB); // 找到DIB图像象素起始位置 lpDIBBits = ::FindDIBBits(lpDIB); // 解除锁定 ::GlobalUnlock((HGLOBAL) hDIB); float cxy=(float)cxDIB/(float)cyDIB; dxy=1.0; rcDIB.top = rcDIB.left = 0; rcDIB.right = cxDIB; rcDIB.bottom = cyDIB; rcDest.top = rcDest.left = 10; if(cxDIB>300||cyDIB>400) { if(cxy>0.75) { dxy=(float)cxDIB/(float)300.0; cxy=(float)300.0/(float)cxDIB; rcDest.right=310; rcDest.bottom=(int)((float)cyDIB*cxy)+10; } else { dxy=(float)cyDIB/(float)400.0; cxy=(float)400.0/(float)cyDIB; rcDest.bottom=410; rcDest.right=(int)((float)cxDIB*cxy)+10; } } else { rcDest.right = cxDIB+10; rcDest.bottom = cyDIB+10; } return TRUE; // return TRUE unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE } HBRUSH CDlgGrid::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) { HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor); // TODO: Change any attributes of the DC here if(nCtlColor==CTLCOLOR_STATIC) { // 设置文本的背景颜色跟对话框的背景一致 hbr=CreateSolidBrush(RGB(179,210,253)); pDC->SetBkColor(RGB(179,210,253)); pDC->SetTextColor(RGB(255,0,0)); } // TODO: Return a different brush if the default is not desired return hbr; } void CDlgGrid::OnPaint() { CPaintDC dc(this); // device context for painting BYTE rgbRtBound; CRect rtBg; CPen pen; CBrush brush; // 设置背景 pen.CreatePen(PS_SOLID,1,RGB(179,210,253)); brush.CreateSolidBrush(RGB(179,210,253)); GetClientRect(&rtBg); dc.SelectObject(&pen); dc.SelectObject(&brush); dc.Rectangle(rtBg); brush.DeleteObject(); pen.DeleteObject(); // 输出DIB ::PaintDIB(dc.m_hDC, &rcDest, hDIB, &rcDIB, palDIB); // 画矩形 CRect rtOutSel; // 矩形中心点的位置 int x; int y; x=(int)((float)(rtSel.left+RT_WIDTH/2-10)*dxy); y=(int)((float)(rtSel.top+RT_WIDTH/2-10)*dxy); rtOutSel.left=x-RT_WIDTH/2; rtOutSel.top=y-RT_WIDTH/2; rtOutSel.right=x+RT_WIDTH/2; rtOutSel.bottom=y+RT_WIDTH/2; // 输出原始图 ::PaintDIB(dc.m_hDC, &rtSel, hDIB, &rtOutSel, palDIB); // 画矩形边缘 if(bLButtonDown) rgbRtBound=0; else rgbRtBound=255; pen.CreatePen(PS_SOLID,1,RGB(rgbRtBound,rgbRtBound,rgbRtBound)); dc.SelectObject(&pen); dc.MoveTo(rtSel.left,rtSel.bottom); dc.LineTo(rtSel.left,rtSel.top); dc.LineTo(rtSel.right,rtSel.top); pen.DeleteObject(); pen.CreatePen(PS_SOLID,1,RGB(255-rgbRtBound,255-rgbRtBound,255-rgbRtBound)); dc.SelectObject(&pen); dc.LineTo(rtSel.right,rtSel.bottom); dc.LineTo(rtSel.left,rtSel.bottom); pen.DeleteObject(); // 画网格 rtBg.SetRect(rcDest.right+10,rcDest.top,rcDest.right+10+m_pixels*40,rcDest.top+m_pixels*40); DrawGrids(rtOutSel,rtBg); } void CDlgGrid::DrawGrids(RECT rtSrc, RECT rtDst) { CClientDC dc(this); int i; int j; int k; int temp1,temp2; BYTE R,G,B; COLORREF rgb; CRect pixels; double result; unsigned char pixel[4]; for(j=rtSel.top+1;j rtSel.left && point.x < rtSel.right && point.y > rtSel.top && point.y < rtSel.bottom) { bLButtonDown=TRUE; CRect rt(rtSel.left,rtSel.top,rtSel.right+1,rtSel.bottom+1); InvalidateRect(&rt); Oldx=point.x; Oldy=point.y; } CDialog::OnLButtonDown(nFlags, point); } void CDlgGrid::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default bLButtonDown=FALSE; CRect rt(rtSel.left,rtSel.top,rtSel.right+1,rtSel.bottom+1); InvalidateRect(&rt); CDialog::OnLButtonUp(nFlags, point); } void CDlgGrid::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default if(bLButtonDown) { int dx,dy; dx=point.x-Oldx; dy=point.y-Oldy; int offset=RT_WIDTH/2-(int)((float)(RT_WIDTH/2)/dxy+0.5); if(rtSel.left+dx<10-offset || rtSel.right+dx>rcDest.right+offset) dx=0; if(rtSel.top+dy<10-offset || rtSel.bottom+dy>rcDest.bottom+offset) dy=0; CRect rt; rt.SetRect(rtSel.left,rtSel.top,rtSel.right,rtSel.bottom); rtSel.OffsetRect(dx,dy); if(rt.left>rtSel.left) rt.left=rtSel.left; if(rt.top>rtSel.top) rt.top=rtSel.top; if(rt.right