VC,一条会被鼠标移动的直线
对话框中的小红线可以被移动的
一、类名是 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 OnMouseMove(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,这个只是用来辅助定位的咚咚咚
提问的帖子:http://topic.csdn.net/u/20111001/17/952edd78-cb91-48c8-861f-0eeebf81d75d.html
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。