#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* src = 0
IplImage* dst = 0
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre_pt = {-1,-1}
static CvPoint cur_pt = {-1,-1}
CvFont font
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA)
char temp[16]
if( event == CV_EVENT_LBUTTONDOWN )
{
cvCopy(dst,src)
sprintf(temp,"(%d,%d)",x,y)
pre_pt = cvPoint(x,y)
cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255))
cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 )
cvShowImage( "src", src )
cvCopy(src,dst)
}
else if( event == CV_EVENT_MOUSEMOVE &&(flags &CV_EVENT_FLAG_LBUTTON))
{
cvCopy(dst,src)
sprintf(temp,"(%d,%d)",x,y)
cur_pt = cvPoint(x,y)
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255))
cvLine(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, CV_AA, 0 )
cvShowImage( "src", src )
}
else if( event == CV_EVENT_LBUTTONUP )
{
sprintf(temp,"(%d,%d)",x,y)
cur_pt = cvPoint(x,y)
cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255))
cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 )
cvLine( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, CV_AA, 0 )
cvShowImage( "src", src )
cvCopy(src,dst)
}
}
int main()
{
src=cvLoadImage("lena.jpg",1)
dst=cvCloneImage(src)
cvNamedWindow("src",1)
cvSetMouseCallback( "src", on_mouse, 0 )
cvShowImage("src",src)
cvWaitKey(0)
cvDestroyAllWindows()
cvReleaseImage(&src)
cvReleaseImage(&dst)
return 0
}
可以通过一个bool全局变量指示划线菜单项是否被选中,首先用类向导在view类中为划线菜单项添加消息响应函数,在该函数中为该全局变量赋值指示开始划线可以这样赋值
m_bline=true
然后通过类向导向view类添加OnLButtonDown()函数以响应鼠标按下消息,该消息中首先判断m_bline是否为true,若为true,可通过一个全局CPoint变量记录鼠标按下的位置
OnLButtonUp(UINT,
CPoint
point)
{
if(m_bline)m_oripoint=point
}
相同方法添加OnLButtonUp()函数响应鼠标释放消息
if(m_bline)
{
ClientDC
dc(this)//获取设备描述表句柄
dc.MoveTo(m_oripoint)//画笔移动到第一点
dc.LineTo(point)//划线至第二点
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)