首页 > 其他分享 >VC,一条会被鼠标移动的直线

VC,一条会被鼠标移动的直线

时间:2022-12-13 18:03:47浏览次数:71  
标签:直线 VC 鼠标 point memDC nFlags nPos pDoc rect


                          

                                        对话框中的小红线可以被移动的


一、类名是 CBinarizationDlg


二、定义两个变量

BOOL m_flag;

int nPos;

在构造函数初始化m_flag = false;nPos=256;

三、响应消息函数三个

afx_msg void OnPaint();

afx_msg void OnLButtonDown(UINT nFlags, CPoint point);

afx_msg void OnLButtonUp(UINT nFlags, CPoint point);

afx_msg void onm ouseMove(UINT nFlags, CPoint point);

四、代码加注释

void CBinarizationDlg::OnPaint()
{
CPaintDC dc(this); // device context for painting

CPhotoMeterDoc* pDoc = (CPhotoMeterDoc*) ((CMainFrame*)AfxGetMainWnd())->GetActiveFrame()->GetActiveDocument();

CRect rect;
//GetDlgItem(IDC_STATIC_HISTO)->GetWindowRect(&rect);
//ScreenToClient(&rect);
GetDlgItem(IDC_STATIC_HISTO)->GetClientRect(&rect);
int width = rect.Width();
int height = rect.Height();

// Create a memory CDC
CDC memDC;
memDC.CreateCompatibleDC( &dc );
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( &dc, width, height );
CBitmap* oldbitmap = memDC.SelectObject( &bitmap );

memDC.FillSolidRect( rect, RGB( 255, 255, 255 ) );

CPen axisPen(PS_SOLID,1,RGB(0,0,0));
CPen* pOldPen = dc.SelectObject(&axisPen);
memDC.MoveTo(rect.left+5,rect.bottom-4);
memDC.LineTo(rect.right-5,rect.bottom-4);
memDC.SelectObject(pOldPen);

// The frame
CBrush brh(RGB(0,0,0));
memDC.FrameRect(&rect,&brh);

//histogram
if (pDoc && pDoc->image) {

if (pDoc->m_hmax){
int ybase = rect.bottom-5;
float yratio = (ybase - rect.top)/(float)pDoc->m_hmax;

CPen grayPen(PS_SOLID, 1, RGB(84, 64, 64));
pOldPen = memDC.SelectObject(&grayPen);
//调整的界面刚好使rect.right-rect.left-10是512,即中间横线为512
for (int x=0 ; x<256; x++)
{
memDC.MoveTo(2*x+rect.left+5,ybase);
memDC.LineTo(2*x+rect.left+5, (int)(ybase-pDoc->m_hgray[x]*yratio));
}

memDC.SelectObject(pOldPen);
}else{
pDoc->m_hmax=pDoc->GetImage()->Histogram(pDoc->m_hr,pDoc->m_hg,pDoc->m_hb,pDoc->m_hgray);
if (pDoc->m_hmax) Invalidate();
}
}//end of histogram

CPen redpen(PS_SOLID,0, RGB(222,0,0));
pOldPen = memDC.SelectObject(&redpen);
if ((nPos >= rect.left+5) && (nPos < rect.right-5) )
{
memDC.MoveTo(nPos,rect.bottom-5);
memDC.LineTo(nPos,rect.top+4);
}
memDC.SelectObject(&pOldPen);

dc.BitBlt( 10, 10, width, height, &memDC, 0, 0, SRCCOPY );
memDC.SelectObject( oldbitmap );

}

void CBinarizationDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
CRect rect;
GetDlgItem(IDC_STATIC_HISTO)->GetClientRect(&rect);
rect.left +=5;
rect.right -=5;
rect.bottom +=5;

point.Offset(-10,-10);
if (rect.PtInRect(point) && (nPos == point.x))
{
m_flag = TRUE;
nPos = point.x ;
}
CDialog::OnLButtonDown(nFlags, point);
}

void CBinarizationDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
m_flag = FALSE;
Invalidate(FALSE);

CDialog::OnLButtonUp(nFlags, point);
}

void CBinarizationDlg::OnMouseMove(UINT nFlags, CPoint point)
{
//CDC *pDC = this->GetDC();
CRect rect;
GetDlgItem(IDC_STATIC_HISTO)->GetClientRect(&rect);
rect.left +=5;
rect.right -=5;
rect.bottom +=5;

point.Offset(-10,-10);
if (m_flag && rect.PtInRect(point))
{
nPos = point.x;
Invalidate(FALSE);
}
CDialog::OnMouseMove(nFlags, point);
//ReleaseDC(pDC);
}


说明:IDC_STATIC_HISTO是一个picture控件的ID,visible = false,这个只是用来辅助定位的咚咚咚


标签:直线,VC,鼠标,point,memDC,nFlags,nPos,pDoc,rect
From: https://blog.51cto.com/u_15911341/5934953

相关文章