?? intensitydlg.cpp
字號(hào):
// IntensityDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LineTrans.h"
#include "IntensityDlg.h"
#include "DibImage.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CIntensityDlg dialog
CIntensityDlg::CIntensityDlg(CWnd* pParent /*=NULL*/)
: CDialog(CIntensityDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CIntensityDlg)
m_iLowGray = 0;
m_iUpGray = 0;
//}}AFX_DATA_INIT
}
void CIntensityDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CIntensityDlg)
DDX_Text(pDX, IDC_EDIT_LOWGRAY, m_iLowGray);
DDV_MinMaxInt(pDX, m_iLowGray, 0, 255);
DDX_Text(pDX, IDC_EDIT_UPGRAY, m_iUpGray);
DDV_MinMaxInt(pDX, m_iUpGray, 0, 255);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CIntensityDlg, CDialog)
//{{AFX_MSG_MAP(CIntensityDlg)
ON_WM_PAINT()
ON_EN_KILLFOCUS(IDC_EDIT_LOWGRAY, OnKillfocusEDITLowGray)
ON_EN_KILLFOCUS(IDC_EDIT_UPGRAY, OnKillfocusEDITUpGray)
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CIntensityDlg message handlers
BOOL CIntensityDlg::OnInitDialog()
{
unsigned char * lpSrc; // 指向源圖像象素的指針
LONG i,j;
CDialog::OnInitDialog();
CWnd* pWnd = GetDlgItem(IDC_COORD);
pWnd->GetClientRect(m_MouseRect);
pWnd->ClientToScreen(&m_MouseRect);
CRect rect;
GetClientRect(rect);
ClientToScreen(&rect);
m_MouseRect.top -= rect.top;
m_MouseRect.left -= rect.left;
// 設(shè)置接受鼠標(biāo)事件的有效區(qū)域
m_MouseRect.top += 25;
m_MouseRect.left += 10;
m_MouseRect.bottom = m_MouseRect.top + 255;
m_MouseRect.right = m_MouseRect.left + 256;
for (i = 0; i < 256; i ++)
{
m_lCount[i] = 0;
}
LONG lLineBytes; // 圖像每行的字節(jié)數(shù)
lLineBytes = WIDTHBYTES(m_lWidth * 8);
// 計(jì)算各個(gè)灰度值的計(jì)數(shù)
for (i = 0; i < m_lHeight; i ++)
{
for (j = 0; j < m_lWidth; j ++)
{
lpSrc = (unsigned char *)m_lpDIBBits + lLineBytes * i + j;
m_lCount[*(lpSrc)]++;
}
}
m_iIsDraging = 0;
return TRUE;
}
void CIntensityDlg::OnKillfocusEDITLowGray()
{
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 CIntensityDlg::OnKillfocusEDITUpGray()
{
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 CIntensityDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// 判斷是否在接受鼠標(biāo)事件的有效區(qū)域中
if(m_MouseRect.PtInRect(point))
{
if (point.x == (m_MouseRect.left + m_iLowGray))
{
m_iIsDraging = 1; // 設(shè)置拖動(dòng)狀態(tài)1,拖動(dòng)下限
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
else if (point.x == (m_MouseRect.left + m_iUpGray))
{
m_iIsDraging = 2; // 設(shè)置拖動(dòng)狀態(tài)為2,拖動(dòng)上限
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
CDialog::OnLButtonDown(nFlags, point);
}
void CIntensityDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// 判斷是否在接受鼠標(biāo)事件的有效區(qū)域中
if(m_MouseRect.PtInRect(point))
{
// 判斷是否正在拖動(dòng)
if (m_iIsDraging != 0)
{
// 判斷正在拖動(dòng)上限還是下限
if (m_iIsDraging == 1)
{
// 判斷是否下限<上限
if (point.x - m_MouseRect.left < m_iUpGray)
{
m_iLowGray = point.x - m_MouseRect.left; // 更改下限
}
else
{
m_iLowGray = m_iUpGray - 1; // 下限拖過上限,設(shè)置為上限-1
point.x = m_MouseRect.left + m_iUpGray - 1; // 重設(shè)鼠標(biāo)位置
}
}
else
{
// 正在拖動(dòng)上限
// 判斷是否上限>下限
if (point.x - m_MouseRect.left > m_iLowGray)
{
m_iUpGray = point.x - m_MouseRect.left; // 更改下限
}
else
{
m_iUpGray = m_iLowGray + 1; // 下限拖過上限,設(shè)置為下限+1
point.x = m_MouseRect.left + m_iLowGray + 1;// 重設(shè)鼠標(biāo)位置
}
}
::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 CIntensityDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// 當(dāng)用戶釋放鼠標(biāo)左鍵停止拖動(dòng)
if (m_iIsDraging != 0)
{
m_iIsDraging = 0;
}
CDialog::OnLButtonUp(nFlags, point);
}
void CIntensityDlg::OnPaint()
{
CString str;
LONG i;
LONG lMaxCount = 0; // 最大計(jì)數(shù)
CPaintDC dc(this);
CWnd* pWnd = GetDlgItem(IDC_COORD);
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,330,300);
CPen* pPenRed = new CPen; // 創(chuàng)建畫筆對(duì)象
pPenRed->CreatePen(PS_SOLID,1,RGB(255,0,0)); // 紅色畫筆
CPen* pPenBlue = new CPen; // 創(chuàng)建畫筆對(duì)象
pPenBlue->CreatePen(PS_SOLID,1,RGB(0,0, 255)); // 藍(lán)色畫筆
CPen* pPenGreen = new CPen; // 創(chuàng)建畫筆對(duì)象
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0)); // 綠色畫筆
// 選中當(dāng)前紅色畫筆,并保存以前的畫筆
CGdiObject* pOldPen = pDC->SelectObject(pPenRed);
pDC->MoveTo(10,10); // 繪制坐標(biāo)軸
pDC->LineTo(10,280); // 垂直軸
pDC->LineTo(320,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(265, 283, str);
// 繪制X軸刻度
for (i = 0; i < 256; i += 5)
{
if ((i & 1) == 0)
{
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 284);
}
else
{
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 282);
}
}
// 繪制X軸箭頭
pDC->MoveTo(315,275);
pDC->LineTo(320,280);
pDC->LineTo(315,285);
// 繪制X軸箭頭
pDC->MoveTo(10,10);
pDC->LineTo(5,15);
pDC->MoveTo(10,10);
pDC->LineTo(15,15);
// 計(jì)算最大計(jì)數(shù)值
for (i = m_iLowGray; i <= m_iUpGray; i ++)
{
// 判斷是否大于當(dāng)前最大值
if (m_lCount[i] > lMaxCount)
{
lMaxCount = m_lCount[i];
}
}
// 輸出最大計(jì)數(shù)值
pDC->MoveTo(10, 25);
pDC->LineTo(14, 25);
str.Format("%d", lMaxCount);
pDC->TextOut(11, 26, str);
// 更改成綠色畫筆
pDC->SelectObject(pPenGreen);
// 繪制窗口上下限
pDC->MoveTo(m_iLowGray + 10, 25);
pDC->LineTo(m_iLowGray + 10, 280);
pDC->MoveTo(m_iUpGray + 10, 25);
pDC->LineTo(m_iUpGray + 10, 280);
// 更改成藍(lán)色畫筆
pDC->SelectObject(pPenBlue);
// 判斷是否有計(jì)數(shù)
if (lMaxCount > 0)
{
// 繪制直方圖
for (i = m_iLowGray; i <= m_iUpGray; i ++)
{
pDC->MoveTo(i + 10, 280);
pDC->LineTo(i + 10, 281 - (int) (m_lCount[i] * 256 / lMaxCount));
}
}
// 恢復(fù)以前的畫筆
pDC->SelectObject(pOldPen);
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
}
void CIntensityDlg::OnOK()
{
// 判斷是否下限超過上限
if (m_iLowGray > m_iUpGray)
{
int iTemp = m_iLowGray;
m_iLowGray = m_iUpGray;
m_iUpGray = iTemp;
}
CDialog::OnOK();
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -