基于C++程序放大局部图形(mfc)

65 篇文章 2 订阅
54 篇文章 1 订阅

基于C++程序放大局部图形;功能的实现;通过VC++实现对图形的局部进行放大;实现方法:;CDC类StretchBlt函数可以将一幅位图从;BOOLStretchBlt(;intx,inty,//目标矩形的坐标原点int;intxSrc,intySrc,//源矩形的坐标;intnSrcWidth,intnSrcHeig;};;当指定的源和目标矩形的宽度或高度


基于C++程序放大局部图形

功能的实现

通过VC++实现对图形的局部进行放大。编译并运行程序,如下图所示。在图形上移动鼠标光标放大显示图形的不同部位,单击鼠标左键减小放大赔率,单击鼠标右键增大放大倍率。

实现方法:

CDC类StretchBlt函数可以将一幅位图从一个源矩形以一定的光栅操作拷贝到另外一个不同大小的目标矩形,该函数的定义如下:

BOOL StretchBlt(

int x, int y, //目标矩形的坐标原点 int nWidth,int nHeight, //目标矩形的长度和宽度 CDC*pSrcDC, //源设备环境句柄

int xSrc,int ySrc, //源矩形的坐标原点

int nSrcWidth,int nSrcHeight, //源矩形的长度和宽度 DWORD dwRop //光栅操作标志

};

当指定的源和目标矩形的宽度或高度不同时,StretchBlt函数将创建一个位图的镜象。如果是宽度变化,就沿x轴创建镜像;如果是高度变化就沿y轴创建镜像。而且该函数可以再内存中对源图像做拉伸或压缩处理后在拷贝到目标距。这样就实现某部分位图的放大功能。

编写步骤如下

(1)通过AppWizard创建一个单文档应用程序ZoomPart。

(2)在CZoomPartView类的头文件中增加以下保护类型的成员变量:

protected:

CSize m_sizeDest;

CSize m_sizeSource;

CBitmap * m_pBitmap;

CDC * m_pdcMem;

int oldx,oldy,s,d; //s确定被放大区域,d确定放大显示区域,放大倍率=d/s

bool recover;

long mana;

(3)在资源中加入自己喜欢的位图,其ID设为IDB_BITMAP1。

(4)在CZoomPartViewd的构造函数中初始化成员变量,其代码如下:

CZoomPartView::CZoomPartView()

{

// TODO: add construction code here

m_pdcMem = new CDC;

m_pBitmap = new CBitmap;

recover = true;

s = 30; d = 45;

mana = SRCCOPY;

}

(5)在CZoomPartViewd的析构函数中加入如下代码:

CZoomPartView::~CZoomPartView()

{

delete m_pdcMem;

delete m_pBitmap;

}

(6)在CZoomPartViewd的OnDraw函数中显示位图,其代码如下:

void CZoomPartView::OnDraw(CDC* pDC)

{

CZoomPartDoc* pDoc = GetDocument();

ASSERT_VALID(pDoc);

// TODO: add draw code for native data here

//声明判断是否load位图的静态标志

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);

}

(7)在CZoomPartViewd类中通过ClassWizard响应WM_MOUSEMOVE消息,根据当前放大倍数放大鼠标位置附近的局部图像,其代码如下:

void CZoomPartView::OnMouseMove(UINT nFlags, CPoint point)

{

//计算要放大的局部矩形的源图像位置和目标位置

CString cord;

int dd;

CRect srect,drect,mrect;

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);

(8)在CZoomPartViewd类中通过ClassWizard响应WM_LBUTTONDOWN消息和WM_RBUTTONDOWN消息,减小和增加放大倍数,然后进行放大显示。其代码如下:

void CZoomPartView::OnLButtonDown(UINT nFlags, CPoint point)

{

//如果鼠标位置不在位图上,则还原位图大小显示

CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);

if(!rc.PtInRect(point))

{

Invalidate();

}

else if (d > 5)//如果放大倍数大于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);

CZoomPartView::OnMouseMove(nFlags, point);

}

CView::OnLButtonDown(nFlags, point);

}

void CZoomPartView::OnRButtonDown(UINT nFlags, CPoint point)

{

//如果鼠标位置不在位图上,则还原位图大小显示

CRect rc(0,0,m_sizeSource.cx,m_sizeSource.cy);

if(!rc.PtInRect(point))

{

Invalidate();

}

else if (d <150)//如果放大倍数小于150,就继续增加放大倍数,然后进行放大显示 {

d += 10;

CZoomPartView::OnMouseMove(nFlags, point);

}

CView::OnRButtonDown(nFlags, point);

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值