?? dlgehnlintrans.cpp
字號:
// DlgEhnLinTrans.cpp : implementation file
//
#include "stdafx.h"
#include "ImageProcessing.h"
#include "DlgEhnLinTrans.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgEhnLinTrans dialog
CDlgEhnLinTrans::CDlgEhnLinTrans(CWnd* pParent /*=NULL*/)
: CDialog(CDlgEhnLinTrans::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgEhnLinTrans)
m_nX1 = 0;
m_nX2 = 0;
m_nY1 = 0;
m_nY2 = 0;
//}}AFX_DATA_INIT
}
void CDlgEhnLinTrans::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgEhnLinTrans)
DDX_Text(pDX, IDC_EDIT_LINTRANS_X1, m_nX1);
DDV_MinMaxInt(pDX, m_nX1, 0, 255);
DDX_Text(pDX, IDC_EDIT_LINTRANS_X2, m_nX2);
DDV_MinMaxInt(pDX, m_nX2, 0, 255);
DDX_Text(pDX, IDC_EDIT_LINTRANS_Y1, m_nY1);
DDV_MinMaxInt(pDX, m_nY1, 0, 255);
DDX_Text(pDX, IDC_EDIT_LINTRANS_Y2, m_nY2);
DDV_MinMaxInt(pDX, m_nY2, 0, 255);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgEhnLinTrans, CDialog)
//{{AFX_MSG_MAP(CDlgEhnLinTrans)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
ON_WM_PAINT()
ON_EN_KILLFOCUS(IDC_EDIT_LINTRANS_X1, OnKillfocusEditLintransX1)
ON_EN_KILLFOCUS(IDC_EDIT_LINTRANS_X2, OnKillfocusEditLintransX2)
ON_EN_KILLFOCUS(IDC_EDIT_LINTRANS_Y1, OnKillfocusEditLintransY1)
ON_EN_KILLFOCUS(IDC_EDIT_LINTRANS_Y2, OnKillfocusEditLintransY2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgEhnLinTrans message handlers
BOOL CDlgEhnLinTrans::OnInitDialog()
{
// 調(diào)用默認OnInitDialog函數(shù)
CDialog::OnInitDialog();
// 獲取繪制直方圖的標簽
CWnd* pWnd = GetDlgItem(IDC_LINTRANS_SHOWCOR);
// 計算接受鼠標事件的有效區(qū)域
pWnd->GetClientRect(m_rectMouse);
pWnd->ClientToScreen(&m_rectMouse);
CRect rect;
GetClientRect(rect);
ClientToScreen(&rect);
m_rectMouse.top -= rect.top;
m_rectMouse.left -= rect.left;
// 設(shè)置接受鼠標事件的有效區(qū)域
m_rectMouse.top += 25;
m_rectMouse.left += 10;
m_rectMouse.bottom = m_rectMouse.top + 255;
m_rectMouse.right = m_rectMouse.left + 256;
// 初始化拖動狀態(tài)
m_nIsDraging = 0;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CDlgEhnLinTrans::OnLButtonDown(UINT nFlags, CPoint point)
{
// 當用戶單擊鼠標左鍵開始拖動
if(m_rectMouse.PtInRect(point))
{
CRect rectTemp;
// 計算點1臨近區(qū)域
rectTemp.left = m_rectMouse.left + m_nX1 - 2;
rectTemp.right = m_rectMouse.left + m_nX1 + 2;
rectTemp.top = 255 + m_rectMouse.top - m_nY1 - 2;
rectTemp.bottom = 255 + m_rectMouse.top - m_nY1 + 2;
// 判斷用戶是不是想拖動點1
if (rectTemp.PtInRect(point))
{
// 設(shè)置拖動狀態(tài)1,拖動點1
m_nIsDraging = 1;
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEALL));
}
else
{
// 計算點2臨近區(qū)域
rectTemp.left = m_rectMouse.left + m_nX2 - 2;
rectTemp.right = m_rectMouse.left + m_nX2 + 2;
rectTemp.top = 255 + m_rectMouse.top - m_nY2 - 2;
rectTemp.bottom = 255 + m_rectMouse.top - m_nY2 + 2;
// 判斷用戶是不是想拖動點2
if (rectTemp.PtInRect(point))
{
// 設(shè)置拖動狀態(tài)為2,拖動點2
m_nIsDraging = 2;
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEALL));
}
}
}
// 默認單擊鼠標左鍵處理事件
CDialog::OnLButtonDown(nFlags, point);
}
void CDlgEhnLinTrans::OnLButtonUp(UINT nFlags, CPoint point)
{
// 當用戶釋放鼠標左鍵停止拖動
if (m_nIsDraging != 0)
{
// 重置拖動狀態(tài)
m_nIsDraging = 0;
}
// 默認釋放鼠標左鍵處理事件
CDialog::OnLButtonUp(nFlags, point);
}
void CDlgEhnLinTrans::OnMouseMove(UINT nFlags, CPoint point)
{
// 判斷當前光標是否在繪制區(qū)域
if(m_rectMouse.PtInRect(point))
{
// 判斷是否正在拖動
if (m_nIsDraging != 0)
{
// 判斷正在拖動點1還是點2
if (m_nIsDraging == 1)
{
// 判斷是否下限<上限
if (point.x - m_rectMouse.left < m_nX2)
{
// 更改下限
m_nX1 = point.x - m_rectMouse.left;
}
else
{
// 下限拖過上限,設(shè)置為上限-1
m_nX1 = m_nX2 - 1;
// 重設(shè)鼠標位置
point.x = m_rectMouse.left + m_nX2 - 1;
}
// 更改Y坐標
m_nY1 = 255 + m_rectMouse.top - point.y;
}
else
{
// 正在拖動點2
// 判斷是否上限>下限
if (point.x - m_rectMouse.left > m_nX1)
{
// 更改下限
m_nX2 = point.x - m_rectMouse.left;
}
else
{
// 下限拖過上限,設(shè)置為下限+1
m_nX2 = m_nX1 + 1;
// 重設(shè)鼠標位置
point.x = m_rectMouse.left + m_nX1 + 1;
}
// 更改Y坐標
m_nY2 = 255 + m_rectMouse.top - point.y;
}
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEALL));
// 更新
UpdateData(FALSE);
// 重繪
InvalidateRect(m_rectMouse, TRUE);
}
else
{
CRect rectTemp1;
CRect rectTemp2;
// 計算點1臨近區(qū)域
rectTemp1.left = m_rectMouse.left + m_nX1 - 2;
rectTemp1.right = m_rectMouse.left + m_nX1 + 2;
rectTemp1.top = 255 + m_rectMouse.top - m_nY1 - 2;
rectTemp1.bottom = 255 + m_rectMouse.top - m_nY1 + 2;
// 計算點2臨近區(qū)域
rectTemp2.left = m_rectMouse.left + m_nX2 - 2;
rectTemp2.right = m_rectMouse.left + m_nX2 + 2;
rectTemp2.top = 255 + m_rectMouse.top - m_nY2 - 2;
rectTemp2.bottom = 255 + m_rectMouse.top - m_nY2 + 2;
// 判斷用戶在點1或點2旁邊
if ((rectTemp1.PtInRect(point)) || (rectTemp2.PtInRect(point)))
{
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEALL));
}
}
}
// 默認鼠標移動處理事件
CDialog::OnMouseMove(nFlags, point);
}
void CDlgEhnLinTrans::OnPaint()
{
// 設(shè)備上下文
CPaintDC dc(this); // device context for painting
// 字符串
CString str;
// 獲取繪制坐標的文本框
CWnd* pWnd = GetDlgItem(IDC_LINTRANS_SHOWCOR);
// 指針
CDC* pDC = pWnd->GetDC();
pWnd->Invalidate();
pWnd->UpdateWindow();
pDC->Rectangle(0,0,330,300);
// 創(chuàng)建畫筆對象
CPen* pPenRed = new CPen;
// 紅色畫筆
pPenRed->CreatePen(PS_SOLID, 2, RGB(255,0,0));
// 創(chuàng)建畫筆對象
CPen* pPenBlue = new CPen;
// 藍色畫筆
pPenBlue->CreatePen(PS_SOLID, 1, RGB(0,0, 255));
// 選中當前紅色畫筆,并保存以前的畫筆
CGdiObject* pOldPen = pDC->SelectObject(pPenRed);
// 繪制坐標軸
pDC->MoveTo(10,10);
// 垂直軸
pDC->LineTo(10,280);
// 水平軸
pDC->LineTo(320,280);
// 寫坐標
str.Format("0");
pDC->TextOut(10, 281, str);
str.Format("255");
pDC->TextOut(265, 281, str);
pDC->TextOut(11, 25, str);
// 繪制X軸箭頭
pDC->LineTo(315,275);
pDC->MoveTo(320,280);
pDC->LineTo(315,285);
// 繪制X軸箭頭
pDC->MoveTo(10,10);
pDC->LineTo(5,15);
pDC->MoveTo(10,10);
pDC->LineTo(15,15);
// 更改成藍色畫筆
pDC->SelectObject(pPenBlue);
// 繪制坐標值
str.Format("(%d, %d)", m_nX1, m_nY1);
pDC->TextOut(m_nX1 + 10, 281 - m_nY1, str);
str.Format("(%d, %d)", m_nX2, m_nY2);
pDC->TextOut(m_nX2 + 10, 281 - m_nY2, str);
// 繪制用戶指定的變換直線
pDC->MoveTo(10, 280);
pDC->LineTo(m_nX1 + 10, 280 - m_nY1);
pDC->LineTo(m_nX2 + 10, 280 - m_nY2);
pDC->LineTo(265, 25);
// 繪制點邊緣的小矩形
CBrush brush;
brush.CreateSolidBrush(RGB(0,255,0));
// 選中刷子
CGdiObject* pOldBrush = pDC->SelectObject(&brush);
// 繪制小矩形
pDC->Rectangle(m_nX1 + 10 - 2, 280 - m_nY1 - 2, m_nX1 + 12, 280 - m_nY1 + 2);
pDC->Rectangle(m_nX2 + 10 - 2, 280 - m_nY2 - 2, m_nX2 + 12, 280 - m_nY2 + 2);
// 恢復以前的畫筆
pDC->SelectObject(pOldPen);
// 繪制邊緣
pDC->MoveTo(10,25);
pDC->LineTo(265,25);
pDC->LineTo(265,280);
// 刪除新的畫筆
delete pPenRed;
delete pPenBlue;
// Do not call CDialog::OnPaint() for painting messages
}
void CDlgEhnLinTrans::OnKillfocusEditLintransX1()
{
// 更新
UpdateData(TRUE);
// 判斷是否下限超過上限
if (m_nX1 > m_nX2)
{
// 互換
int nTemp = m_nX1;
m_nX1 = m_nX2;
m_nX2 = nTemp;
nTemp = m_nY1;
m_nY1 = m_nY2;
m_nY2 = nTemp;
// 更新
UpdateData(FALSE);
}
// 重繪
InvalidateRect(m_rectMouse, TRUE);
}
void CDlgEhnLinTrans::OnKillfocusEditLintransX2()
{
// 更新
UpdateData(TRUE);
// 判斷是否下限超過上限
if (m_nX1 > m_nX2)
{
// 互換
int nTemp = m_nX1;
m_nX1 = m_nX2;
m_nX2 = nTemp;
nTemp = m_nY1;
m_nY1 = m_nY2;
m_nY2 = nTemp;
// 更新
UpdateData(FALSE);
}
// 重繪
InvalidateRect(m_rectMouse, TRUE);
}
void CDlgEhnLinTrans::OnKillfocusEditLintransY1()
{
// 更新
UpdateData(TRUE);
// 重繪
InvalidateRect(m_rectMouse, TRUE);
}
void CDlgEhnLinTrans::OnKillfocusEditLintransY2()
{
// 更新
UpdateData(TRUE);
// 重繪
InvalidateRect(m_rectMouse, TRUE);
}
void CDlgEhnLinTrans::OnOK()
{
// 判斷是否下限超過上限
if (m_nX1 > m_nX2)
{
// 互換
int nTemp = m_nX1;
m_nX1 = m_nX2;
m_nX2 = nTemp;
nTemp = m_nY1;
m_nY1 = m_nY2;
m_nY2 = nTemp;
// 更新
UpdateData(FALSE);
}
// 默認處理事件
CDialog::OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -