?? dlgpointwin.cpp
字號:
// DlgPointWin.cpp : implementation file
//
#include "stdafx.h"
#include "ch1_1.h"
#include "DlgPointWin.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CDlgPointWin dialog
CDlgPointWin::CDlgPointWin(CWnd* pParent /*=NULL*/)
: CDialog(CDlgPointWin::IDD, pParent)
{
//{{AFX_DATA_INIT(CDlgPointWin)
m_bLow = 0;
m_bUp = 0;
//}}AFX_DATA_INIT
}
void CDlgPointWin::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDlgPointWin)
DDX_Text(pDX, IDC_EDIT_Low, m_bLow);
DDV_MinMaxByte(pDX, m_bLow, 0, 255);
DDX_Text(pDX, IDC_EDIT_Up, m_bUp);
DDV_MinMaxByte(pDX, m_bUp, 0, 255);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDlgPointWin, CDialog)
//{{AFX_MSG_MAP(CDlgPointWin)
ON_EN_KILLFOCUS(IDC_EDIT_Low, OnKillfocusEDITLow)
ON_EN_KILLFOCUS(IDC_EDIT_Up, OnKillfocusEDITUp)
ON_WM_PAINT()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDlgPointWin message handlers
BOOL CDlgPointWin::OnInitDialog()
{
// 調(diào)用默認OnInitDialog函數(shù)
CDialog::OnInitDialog();
// 獲取繪制直方圖的標簽
CWnd* pWnd = GetDlgItem(IDC_COORD);
// 計算接受鼠標事件的有效區(qū)域
pWnd->GetClientRect(m_MouseRect);
pWnd->ClientToScreen(&m_MouseRect);
CRect rect;
GetClientRect(rect);
ClientToScreen(&rect);
m_MouseRect.top -= rect.top;
m_MouseRect.left -= rect.left;
// 設置接受鼠標事件的有效區(qū)域
m_MouseRect.top += 25;
m_MouseRect.left += 10;
m_MouseRect.bottom = m_MouseRect.top + 255;
m_MouseRect.right = m_MouseRect.left + 256;
// 初始化拖動狀態(tài)
m_iIsDraging = 0;
// 返回TRUE
return TRUE;
}
void CDlgPointWin::OnKillfocusEDITLow()
{
// 更新
UpdateData(TRUE);
// 判斷是否下限超過上限
if (m_bLow > m_bUp)
{
// 互換
BYTE bTemp = m_bLow;
m_bLow = m_bUp;
m_bUp = bTemp;
// 更新
UpdateData(FALSE);
}
// 重繪
InvalidateRect(m_MouseRect, TRUE);
}
void CDlgPointWin::OnKillfocusEDITUp()
{
// 更新
UpdateData(TRUE);
// 判斷是否下限超過上限
if (m_bLow > m_bUp)
{
// 互換
BYTE bTemp = m_bLow;
m_bLow = m_bUp;
m_bUp = bTemp;
// 更新
UpdateData(FALSE);
}
// 重繪
InvalidateRect(m_MouseRect, TRUE);
}
void CDlgPointWin::OnLButtonDown(UINT nFlags, CPoint point)
{
// 當用戶單擊鼠標左鍵開始拖動
if(m_MouseRect.PtInRect(point))
{
if (point.x == (m_MouseRect.left + m_bLow))
{
// 設置拖動狀態(tài)1,拖動下限
m_iIsDraging = 1;
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
else if (point.x == (m_MouseRect.left + m_bUp))
{
// 設置拖動狀態(tài)為2,拖動上限
m_iIsDraging = 2;
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
// 默認單擊鼠標左鍵處理事件
CDialog::OnLButtonDown(nFlags, point);
}
void CDlgPointWin::OnMouseMove(UINT nFlags, CPoint point)
{
// 判斷當前光標是否在繪制區(qū)域
if(m_MouseRect.PtInRect(point))
{
// 判斷是否正在拖動
if (m_iIsDraging != 0)
{
// 判斷正在拖動上限還是下限
if (m_iIsDraging == 1)
{
// 判斷是否下限<上限
if (point.x - m_MouseRect.left < m_bUp)
{
// 更改下限
m_bLow = (BYTE) (point.x - m_MouseRect.left);
}
else
{
// 下限拖過上限,設置為上限-1
m_bLow = m_bUp - 1;
// 重設鼠標位置
point.x = m_MouseRect.left + m_bUp - 1;
}
}
else
{
// 正在拖動上限
// 判斷是否上限>下限
if (point.x - m_MouseRect.left > m_bLow)
{
// 更改下限
m_bUp = (BYTE) (point.x - m_MouseRect.left);
}
else
{
// 下限拖過上限,設置為下限+1
m_bUp = m_bLow + 1;
// 重設鼠標位置
point.x = m_MouseRect.left + m_bLow + 1;
}
}
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
// 更新
UpdateData(FALSE);
// 重繪
InvalidateRect(m_MouseRect, TRUE);
}
else if (point.x == (m_MouseRect.left + m_bLow) || point.x == (m_MouseRect.left + m_bUp))
{
// 更改光標
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
// 默認鼠標移動處理事件
CDialog::OnMouseMove(nFlags, point);
}
void CDlgPointWin::OnLButtonUp(UINT nFlags, CPoint point)
{
// 當用戶釋放鼠標左鍵停止拖動
if (m_iIsDraging != 0)
{
// 重置拖動狀態(tài)
m_iIsDraging = 0;
}
// 默認釋放鼠標左鍵處理事件
CDialog::OnLButtonUp(nFlags, point);
}
void CDlgPointWin::OnPaint()
{
// 字符串
CString str;
// 設備上下文
CPaintDC dc(this);
// 獲取繪制坐標的文本框
CWnd* pWnd = GetDlgItem(IDC_COORD);
// 指針
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,2,RGB(0,0, 255));
// 創(chuàng)建畫筆對象
CPen* pPenGreen = new CPen;
// 綠色畫筆
pPenGreen->CreatePen(PS_DOT,1,RGB(0,255,0));
// 選中當前紅色畫筆,并保存以前的畫筆
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(pPenGreen);
// 繪制窗口上下限
pDC->MoveTo(m_bLow + 10, 25);
pDC->LineTo(m_bLow + 10, 280);
pDC->MoveTo(m_bUp + 10, 25);
pDC->LineTo(m_bUp + 10, 280);
// 更改成藍色畫筆
pDC->SelectObject(pPenBlue);
// 繪制坐標值
str.Format("(%d, %d)", m_bLow, m_bLow);
pDC->TextOut(m_bLow + 10, 281 - m_bLow, str);
str.Format("(%d, %d)", m_bUp, m_bUp);
pDC->TextOut(m_bUp + 10, 281 - m_bUp, str);
// 繪制用戶指定的窗口(注意轉(zhuǎn)換坐標系)
pDC->MoveTo(10, 280);
pDC->LineTo(m_bLow + 10, 280);
pDC->LineTo(m_bLow + 10, 280 - m_bLow);
pDC->LineTo(m_bUp + 10, 280 - m_bUp);
pDC->LineTo(m_bUp + 10, 25);
pDC->LineTo(265, 25);
// 恢復以前的畫筆
pDC->SelectObject(pOldPen);
// 繪制邊緣
pDC->MoveTo(10,25);
pDC->LineTo(265,25);
pDC->LineTo(265,280);
// 刪除新的畫筆
delete pPenRed;
delete pPenBlue;
delete pPenGreen;
}
void CDlgPointWin::OnOK()
{
// 判斷是否下限超過上限
if (m_bLow > m_bUp)
{
// 互換
BYTE bTemp = m_bLow;
m_bLow = m_bUp;
m_bUp = bTemp;
}
// 默認處理事件
CDialog::OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -