www.pudn.com > ImgSeg.rar > DlgIntensity.cpp
// DlgIntensity.cpp : implementation file
//
#include "stdafx.h"
#include "ImgSeg.h"
#include "DlgIntensity.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgIntensity dialog
CDlgIntensity::CDlgIntensity(CWnd* pParent /*=NULL*/)
: CDialog(CDlgIntensity::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgIntensity)
m_iLowGray = 0;
m_iUpGray = 0;
//}}AFX_DATA_INIT
}
void CDlgIntensity::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgIntensity)
DDX_Text(pDX, IDC_LOWGRAY, m_iLowGray);
DDV_MinMaxInt(pDX, m_iLowGray, 0, 255);
DDX_Text(pDX, IDC_UPGRAY, m_iUpGray);
DDV_MinMaxInt(pDX, m_iUpGray, 0, 255);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgIntensity, CDialog)
//{{AFX_MSG_MAP(CDlgIntensity)
ON_BN_CLICKED(IDC_OK, OnOk)
ON_EN_CHANGE(IDC_LOWGRAY, OnChangeLowgray)
ON_EN_CHANGE(IDC_UPGRAY, OnChangeUpgray)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgIntensity message handlers
void CDlgIntensity::OnOk()
{
// TODO: Add your control notification handler code here
// 判断是否下限超过上限
if (m_iLowGray > m_iUpGray)
{
// 互换
int iTemp = m_iLowGray;
m_iLowGray = m_iUpGray;
m_iUpGray = iTemp;
}
// 返回
CDialog::OnOK();
}
BOOL CDlgIntensity::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
//指向原图像象素的指针
unsigned char *lpSrc;
//循环变量
LONG i;
LONG j;
CDialog::OnInitDialog();
// TODO: Add extra initialization here
////////////////////////////
// 我的代码开始
////////////////////////////
//获取绘制直方图的标签
CWnd* pWnd=GetDlgItem(IDC_COORD);
//计算接受鼠标事件的有效区域
pWnd->GetClientRect(m_MouseRect);
pWnd->ClientToScreen(&m_MouseRect);
//设置接受鼠标事件的有效区域
m_MouseRect.top-=35;
m_MouseRect.left+=7;
m_MouseRect.bottom=m_MouseRect.top+260;
m_MouseRect.right=m_MouseRect.left+256;
//置计数为0
for(i=0;i<256;i++)
{
//清零
m_lGrayCount[i]=0;
}
//图像每行的字节数
LONG lLineBytes;
//计算图像每行的字节数
lLineBytes=m_lWidth+m_lWidth%4;
//计算各个灰度值的计数
for(i=0;i m_iUpGray)
{
// 互换
int iTemp = m_iLowGray;
m_iLowGray = m_iUpGray;
m_iUpGray = iTemp;
// 更新(成员变量中数值保存到控件中)
UpdateData(FALSE);
}
// 重绘直方图
InvalidateRect(m_MouseRect, TRUE);
}
void CDlgIntensity::OnChangeUpgray()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
// 保存变动
UpdateData(TRUE);
// 判断是否下限超过上限
if (m_iLowGray > m_iUpGray)
{
// 互换
int iTemp = m_iLowGray;
m_iLowGray = m_iUpGray;
m_iUpGray = iTemp;
// 更新
UpdateData(FALSE);
}
// 重绘直方图
InvalidateRect(m_MouseRect, TRUE);
}
void CDlgIntensity::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 当用户单击鼠标左键开始拖动
// 判断是否在接受鼠标事件的有效区域中
if(m_MouseRect.PtInRect(point))
{
if (point.x-(m_MouseRect.left + m_iLowGray)==0)
{
// 设置拖动状态1,拖动下限
m_iIsDraging = 1;
// 更改光标
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
else if (point.x-(m_MouseRect.left + m_iUpGray)==0)
{
// 设置拖动状态为2,拖动上限
m_iIsDraging = 2;
// 更改光标
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
CDialog::OnLButtonDown(nFlags, point);
}
void CDlgIntensity::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 判断是否在接受鼠标事件的有效区域中
if(m_MouseRect.PtInRect(point))
{
// 判断是否正在拖动
if (m_iIsDraging != 0)
{
// 判断正在拖动上限还是下限
if (m_iIsDraging == 1)
{
// 判断是否下限<上限
if (point.x - m_MouseRect.left < m_iUpGray)
{
// 是,更改下限
m_iLowGray = point.x - m_MouseRect.left;
}
else
{
// 下限拖过上限,设置为上限-1
m_iLowGray = m_iUpGray - 1;
// 重设鼠标位置
point.x = m_MouseRect.left + m_iUpGray - 1;
}
}
else
{
// 正在拖动上限
// 判断是否上限>下限
if (point.x - m_MouseRect.left > m_iLowGray)
{
// 更改上限
m_iUpGray = point.x - m_MouseRect.left;
}
else
{
// 上限拖过下限,设置为下限+1
m_iUpGray = m_iLowGray + 1;
// 重设鼠标位置
point.x = m_MouseRect.left + m_iLowGray + 1;
}
}
// 更改光标
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
// 更新
UpdateData(FALSE);
// 重绘直方图
InvalidateRect(m_MouseRect, TRUE);
}
else if (point.x == (m_MouseRect.left + m_iLowGray) || point.x == (m_MouseRect.left + m_iUpGray))
{
// 更改光标
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
CDialog::OnMouseMove(nFlags, point);
}
void CDlgIntensity::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 当用户释放鼠标左键停止拖动
if (m_iIsDraging != 0)
{
// 重置拖动状态
m_iIsDraging = 0;
}
CDialog::OnLButtonUp(nFlags, point);
}
void CDlgIntensity::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
// 字符串
CString str;
// 循环变量
LONG i;
// 最大计数
LONG lMaxCount = 0;
// 获取绘制坐标的文本框
CWnd* pWnd = GetDlgItem(IDC_COORD);
// 指针
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,330,300);
// 创建画笔对象
CPen* pPenRed = new CPen;
// 红色画笔
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0));
// 创建画笔对象
CPen* pPenBlack=new CPen;
// 黑色画笔
pPenBlack->CreatePen(PS_SOLID,1,RGB(0,0,0));
CGdiObject* pOldPen=pDC->SelectObject(pPenBlack);
// 绘制坐标轴
pDC->MoveTo(10,10);
// 垂直轴
pDC->LineTo(10,280);
// 水平轴
pDC->LineTo(310,280);
// 写X轴刻度值
str.Format("0");
pDC->TextOut(10, 283, str);
str.Format("50");
pDC->TextOut(60, 283, str);
str.Format("100");
pDC->TextOut(110, 283, str);
str.Format("150");
pDC->TextOut(160, 283, str);
str.Format("200");
pDC->TextOut(210, 283, str);
str.Format("255");
pDC->TextOut(260, 283, str);
// 绘制X轴刻度
for (i = 0; i < 256; i += 5)
{
if ((i & 1) == 0)
{
//10的倍数
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 284);
}
else
{
//非10的倍数
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 282);
}
}
// 绘制X轴箭头
pDC->MoveTo(305,275);
pDC->LineTo(310,280);
pDC->LineTo(305,285);
// 绘制Y轴箭头
pDC->MoveTo(5,15);
pDC->LineTo(10,10);
pDC->LineTo(15,15);
// 计算最大计数值
for (i = m_iLowGray; i <= m_iUpGray; i ++)
{
// 判断是否大于当前最大值
if (m_lGrayCount[i] > lMaxCount)
{
// 更新最大值
lMaxCount = m_lGrayCount[i];
}
}
// 输出最大计数值
pDC->MoveTo(10, 25);
pDC->LineTo(14, 25);
str.Format("%d", lMaxCount);
pDC->TextOut(11, 26, str);
// 更改成红色画笔
pDC->SelectObject(pPenRed);
// 绘制窗口上下限
pDC->MoveTo(m_iLowGray + 10, 25);
pDC->LineTo(m_iLowGray + 10, 280);
pDC->MoveTo(m_iUpGray + 10, 25);
pDC->LineTo(m_iUpGray + 10, 280);
// 更改成黑色画笔
pDC->SelectObject(pPenBlack);
// 判断是否有计数
if (lMaxCount > 0)
{
// 绘制直方图
for (i = m_iLowGray; i <= m_iUpGray; i ++)
{
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 281 - (int) (m_lGrayCount[i] * 256 / lMaxCount));
}
}
// 恢复以前的画笔
pDC->SelectObject(pOldPen);
// 删除新的画笔
delete pPenRed;
delete pPenBlack;
}