看到有的程序在显示图像时可以把局部区域放大,随着鼠标的移动,以鼠标点为中心的区域跟着移动并放大图像,就像我们拿着放大镜看地图一样,一想原理很简单,就做了一下,先重载一个CWnd类,叫CMagWnd,用来显示放大的局部,获取鼠标移动的点,并抠出一块图像区域,将其放大显示在CMagWnd上,现在遇到的问题是随着鼠标的移动,CMagWnd也跟着移动,这样鼠标下按Z高度,先是它,然后是底图,因此它截获了鼠标消息,我希望它就跟透明的似的,不截获任何鼠标消息,尤其是OnMouseMove中的point,请高手帮忙,谢谢。
2003-4-30 12:59:28 VCTOP 汪援东 阅读次数: 1409 在有些应用中需要对图像的局部进行放大,本例程用VC++ 5.0编写,用CDC的StretchBlt实现图像放大镜,主要有以下功能:1.移动MOUSE放大显示图像的不同部位,2.左击增加放大倍率,3.右击减少放大倍率。 当放大倍率较大时产生抖动。 ---- 实现过程: ---- 1.使用AppWizard生成SDI应用IMAGE。 ---- 2.为CImageView增加以下成员数据: CSize m_sizeDest;
CSize m_sizeSource;
CBitmap * m_pBitmap;
CDC * m_pdcMem;
int oldx,oldy,s,d; file://s确定被放大区域,
d确定放大显示区域,放大倍率=d/s
bool recover;
long mana; ---- 3.在资源中加入自己喜欢的位图并设为IDB_BITMAP1。 ---- 4.对CImageView的以下消息编程: CImageView::CImageView()
{
// Initialize values
m_pdcMem = new CDC;
m_pBitmap = new CBitmap;
recover = true;
s = 30; d = 45;
mana = SRCCOPY;
} CImageView::~CImageView()
{
delete m_pdcMem;
delete m_pBitmap;
} void CImageView::OnDraw(CDC* pDC)
{
static bool load;
if (!load) {
BITMAP bm;
load = !load;
m_pBitmap- >LoadBitmap(IDB_BITMAP1);
m_pdcMem- >CreateCompatibleDC(pDC);
m_pdcMem- >SelectObject(m_pBitmap);
m_pBitmap- >GetObject(sizeof(bm),&bm);
m_sizeSource.cx = bm.bmWidth;
m_sizeSource.cy = bm.bmHeight;
m_sizeDest = m_sizeSource;
pDC- >StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
}
else {
pDC- >StretchBlt(0,0,m_sizeSource.cx,m_sizeSource.cy,
m_pdcMem,0,0,m_sizeSource.cx,m_sizeSource.cy,mana);
}
}
void CImageView::OnMouseMove(UINT nFlags, CPoint point)
{
CString cord;
int dd;
CRect srect,drect,mrect;
CMainFrame * pFrame = (CMainFrame *)AfxGetApp()- >m_pMainWnd ;
CStatusBar * pStatus = &pFrame- >m_wndStatusBar ;
if (pStatus)
{
cord.Format("X = %d, Y = %d",point.x,point.y);
pStatus- >SetPaneText (1,cord);
srect.left = point.x - s;
srect.top = point.y - s;
srect.right = point.x + s;
srect.bottom = point.y + s; drect.left = point.x - d;
drect.top = point.y - d;
drect.right = point.x + d;
drect.bottom = point.y + d; mrect.left = oldx - d;
mrect.top = oldy - d;
mrect.right = oldx + d;
mrect.bottom = oldy + d;
dd = 2*d;
CDC * pDC = GetDC();
OnPrepareDC(pDC);
if (recover)
{
pDC- >BitBlt(mrect.left,mrect.top,dd,dd,
m_pdcMem,mrect.left,mrect.top,mana);
}
pDC- >StretchBlt(drect.left,drect.top,
drect.Width(),drect.Height(),m_pdcMem,srect.left,
srect.top,srect.Width(),srect.Height(),SRCCOPY);
oldx = point.x; oldy = point.y;
ReleaseDC(pDC);
}
recover = true;
CView::OnMouseMove(nFlags, point);
} void CImageView::OnRButtonDown(UINT nFlags, CPoint point)
{
if (d > 5)
{
CDC * pDC = GetDC();
pDC- >StretchBlt(oldx - d,oldy - d,2*d,
2*d,m_pdcMem,oldx - d,oldy - d,2*d,2*d,mana);
d -= 10;
ReleaseDC(pDC);
CImageView::OnMouseMove(nFlags, point);
}
CView::OnRButtonDown(nFlags, point);
} void CImageView::OnLButtonDown(UINT nFlags, CPoint point)
{
if (d < 150)
{
d += 10;
CImageView::OnMouseMove(nFlags, point);
}
CView::OnLButtonDown(nFlags, point);
}