📄 pointwindlg.cpp
字号:
// PointWinDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EAR.h"
#include "PointWinDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CPointWinDlg dialog
CPointWinDlg::CPointWinDlg(CWnd* pParent /*=NULL*/)
: CDialog(CPointWinDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CPointWinDlg)
m_bLow = 0;
m_bUp = 0;
//}}AFX_DATA_INIT
}
void CPointWinDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CPointWinDlg)
DDX_Text(pDX, IDC_EDIT_LOW, m_bLow);
DDX_Text(pDX, IDC_EDIT_UP, m_bUp);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CPointWinDlg, CDialog)
//{{AFX_MSG_MAP(CPointWinDlg)
ON_WM_PAINT()
ON_EN_KILLFOCUS(IDC_EDIT_LOW, OnKillfocusEditLow)
ON_EN_KILLFOCUS(IDC_EDIT_UP, OnKillfocusEditUp)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CPointWinDlg message handlers
void CPointWinDlg::OnPaint()
{
CString str;
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
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,2,RGB(255,0,0));
CPen* pPenBlue = new CPen;
pPenBlue->CreatePen(PS_SOLID,2,RGB(0,0, 255));
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);
// 绘制用户指定的窗口(注意转换坐标系)
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;
// Do not call CDialog::OnPaint() for painting messages
}
void CPointWinDlg::OnKillfocusEditLow()
{
// TODO: Add your control notification handler code here
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 CPointWinDlg::OnKillfocusEditUp()
{
// TODO: Add your control notification handler code here
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 CPointWinDlg::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 CPointWinDlg::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_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 CPointWinDlg::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_bLow))
{
// 设置拖动状态1,拖动下限
m_iIsDraging = 1;
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
else if (point.x == (m_MouseRect.left + m_bUp))
{
// 设置拖动状态为2,拖动上限
m_iIsDraging = 2;
::SetCursor(::LoadCursor(NULL, IDC_SIZEWE));
}
}
CDialog::OnLButtonDown(nFlags, point);
}
BOOL CPointWinDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
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;
// 设置接受鼠标事件的有效区域
m_MouseRect.top += 25;
m_MouseRect.left += 10;
m_MouseRect.bottom = m_MouseRect.top + 255;
m_MouseRect.right = m_MouseRect.left + 256;
m_iIsDraging = 0;
return TRUE;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CPointWinDlg::OnOK()
{
// TODO: Add extra validation here
// 判断是否下限超过上限
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 + -