?? dlgpointstre.cpp
字號:
// DlgPointStre.cpp : implementation file//#include "stdafx.h"#include "ch1_1.h"#include "DlgPointStre.h"#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE[] = __FILE__;#endif/////////////////////////////////////////////////////////////////////////////// CDlgPointStre dialogCDlgPointStre::CDlgPointStre(CWnd* pParent /*=NULL*/) : CDialog(CDlgPointStre::IDD, pParent){ //{{AFX_DATA_INIT(CDlgPointStre) m_bX1 = 0; m_bY1 = 0; m_bX2 = 0; m_bY2 = 0; //}}AFX_DATA_INIT}void CDlgPointStre::DoDataExchange(CDataExchange* pDX){ CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgPointStre) DDX_Text(pDX, IDC_EDIT_X1, m_bX1); DDV_MinMaxByte(pDX, m_bX1, 0, 255); DDX_Text(pDX, IDC_EDIT_X2, m_bX2); DDV_MinMaxByte(pDX, m_bY1, 0, 255); DDX_Text(pDX, IDC_EDIT_Y1, m_bY1); DDV_MinMaxByte(pDX, m_bX2, 0, 255); DDX_Text(pDX, IDC_EDIT_Y2, m_bY2); DDV_MinMaxByte(pDX, m_bY2, 0, 255); //}}AFX_DATA_MAP}BEGIN_MESSAGE_MAP(CDlgPointStre, CDialog) //{{AFX_MSG_MAP(CDlgPointStre) ON_WM_LBUTTONDOWN() ON_WM_MOUSEMOVE() ON_WM_LBUTTONUP() ON_WM_PAINT() ON_EN_KILLFOCUS(IDC_EDIT_X1, OnKillfocusEditX1) ON_EN_KILLFOCUS(IDC_EDIT_X2, OnKillfocusEditX2) ON_EN_KILLFOCUS(IDC_EDIT_Y1, OnKillfocusEditY1) ON_EN_KILLFOCUS(IDC_EDIT_Y2, OnKillfocusEditY2) //}}AFX_MSG_MAPEND_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////// CDlgPointStre message handlersBOOL CDlgPointStre::OnInitDialog() { // 調用默認OnInitDialog函數 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; // 設置接受鼠標事件的有效區域 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 unless you set the focus to a control // EXCEPTION: OCX Property Pages should return FALSE}void CDlgPointStre::OnKillfocusEditX1() { // 更新 UpdateData(TRUE); // 判斷是否下限超過上限 if (m_bX1 > m_bX2) { // 互換 BYTE bTemp = m_bX1; m_bX1 = m_bX2; m_bX2 = bTemp; bTemp = m_bY1; m_bY1 = m_bY2; m_bY2 = bTemp; // 更新 UpdateData(FALSE); } // 重繪 InvalidateRect(m_MouseRect, TRUE);}void CDlgPointStre::OnKillfocusEditX2() { // 更新 UpdateData(TRUE); // 判斷是否下限超過上限 if (m_bX1 > m_bX2) { // 互換 BYTE bTemp = m_bX1; m_bX1 = m_bX2; m_bX2 = bTemp; bTemp = m_bY1; m_bY1 = m_bY2; m_bY2 = bTemp; // 更新 UpdateData(FALSE); } // 重繪 InvalidateRect(m_MouseRect, TRUE);}void CDlgPointStre::OnKillfocusEditY1() { // 更新 UpdateData(TRUE); // 重繪 InvalidateRect(m_MouseRect, TRUE);}void CDlgPointStre::OnKillfocusEditY2() { // 更新 UpdateData(TRUE); // 重繪 InvalidateRect(m_MouseRect, TRUE);}void CDlgPointStre::OnOK() { // 判斷是否下限超過上限 if (m_bX1 > m_bX2) { // 互換 BYTE bTemp = m_bX1; m_bX1 = m_bX2; m_bX2 = bTemp; bTemp = m_bY1; m_bY1 = m_bY2; m_bY2 = bTemp; // 更新 UpdateData(FALSE); } // 默認處理事件 CDialog::OnOK();}void CDlgPointStre::OnLButtonDown(UINT nFlags, CPoint point) { // 當用戶單擊鼠標左鍵開始拖動 if(m_MouseRect.PtInRect(point)) { CRect rectTemp; // 計算點1臨近區域 rectTemp.left = m_MouseRect.left + m_bX1 - 2; rectTemp.right = m_MouseRect.left + m_bX1 + 2; rectTemp.top = 255 + m_MouseRect.top - m_bY1 - 2; rectTemp.bottom = 255 + m_MouseRect.top - m_bY1 + 2; // 判斷用戶是不是想拖動點1 if (rectTemp.PtInRect(point)) { // 設置拖動狀態1,拖動點1 m_iIsDraging = 1; // 更改光標 ::SetCursor(::LoadCursor(NULL, IDC_SIZEALL)); } else { // 計算點2臨近區域 rectTemp.left = m_MouseRect.left + m_bX2 - 2; rectTemp.right = m_MouseRect.left + m_bX2 + 2; rectTemp.top = 255 + m_MouseRect.top - m_bY2 - 2; rectTemp.bottom = 255 + m_MouseRect.top - m_bY2 + 2; // 判斷用戶是不是想拖動點2 if (rectTemp.PtInRect(point)) { // 設置拖動狀態為2,拖動點2 m_iIsDraging = 2; // 更改光標 ::SetCursor(::LoadCursor(NULL, IDC_SIZEALL)); } } } // 默認單擊鼠標左鍵處理事件 CDialog::OnLButtonDown(nFlags, point);}void CDlgPointStre::OnMouseMove(UINT nFlags, CPoint point) { // 判斷當前光標是否在繪制區域 if(m_MouseRect.PtInRect(point)) { // 判斷是否正在拖動 if (m_iIsDraging != 0) { // 判斷正在拖動點1還是點2 if (m_iIsDraging == 1) { // 判斷是否下限<上限 if (point.x - m_MouseRect.left < m_bX2) { // 更改下限 m_bX1 = (BYTE) (point.x - m_MouseRect.left); } else { // 下限拖過上限,設置為上限-1 m_bX1 = m_bX2 - 1; // 重設鼠標位置 point.x = m_MouseRect.left + m_bX2 - 1; } // 更改Y坐標 m_bY1 = (BYTE) (255 + m_MouseRect.top - point.y); } else { // 正在拖動點2 // 判斷是否上限>下限 if (point.x - m_MouseRect.left > m_bX1) { // 更改下限 m_bX2 = (BYTE) (point.x - m_MouseRect.left); } else { // 下限拖過上限,設置為下限+1 m_bX2 = m_bX1 + 1; // 重設鼠標位置 point.x = m_MouseRect.left + m_bX1 + 1; } // 更改Y坐標 m_bY2 = (BYTE) (255 + m_MouseRect.top - point.y); } // 更改光標 ::SetCursor(::LoadCursor(NULL, IDC_SIZEALL)); // 更新 UpdateData(FALSE); // 重繪 InvalidateRect(m_MouseRect, TRUE); } else { CRect rectTemp1; CRect rectTemp2; // 計算點1臨近區域 rectTemp1.left = m_MouseRect.left + m_bX1 - 2; rectTemp1.right = m_MouseRect.left + m_bX1 + 2; rectTemp1.top = 255 + m_MouseRect.top - m_bY1 - 2; rectTemp1.bottom = 255 + m_MouseRect.top - m_bY1 + 2; // 計算點2臨近區域 rectTemp2.left = m_MouseRect.left + m_bX2 - 2; rectTemp2.right = m_MouseRect.left + m_bX2 + 2; rectTemp2.top = 255 + m_MouseRect.top - m_bY2 - 2; rectTemp2.bottom = 255 + m_MouseRect.top - m_bY2 + 2; // 判斷用戶在點1或點2旁邊 if ((rectTemp1.PtInRect(point)) || (rectTemp2.PtInRect(point))) { // 更改光標 ::SetCursor(::LoadCursor(NULL, IDC_SIZEALL)); } } } // 默認鼠標移動處理事件 CDialog::OnMouseMove(nFlags, point);}void CDlgPointStre::OnLButtonUp(UINT nFlags, CPoint point) { // 當用戶釋放鼠標左鍵停止拖動 if (m_iIsDraging != 0) { // 重置拖動狀態 m_iIsDraging = 0; } // 默認釋放鼠標左鍵處理事件 CDialog::OnLButtonUp(nFlags, point);}void CDlgPointStre::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); // 創建畫筆對象 CPen* pPenRed = new CPen; // 紅色畫筆 pPenRed->CreatePen(PS_SOLID, 2, RGB(255,0,0)); // 創建畫筆對象 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_bX1, m_bY1); pDC->TextOut(m_bX1 + 10, 281 - m_bY1, str); str.Format("(%d, %d)", m_bX2, m_bY2); pDC->TextOut(m_bX2 + 10, 281 - m_bY2, str); // 繪制用戶指定的變換直線 pDC->MoveTo(10, 280); pDC->LineTo(m_bX1 + 10, 280 - m_bY1); pDC->LineTo(m_bX2 + 10, 280 - m_bY2); pDC->LineTo(265, 25); // 繪制點邊緣的小矩形 CBrush brush; brush.CreateSolidBrush(RGB(0,255,0)); // 選中刷子 CGdiObject* pOldBrush = pDC->SelectObject(&brush); // 繪制小矩形 pDC->Rectangle(m_bX1 + 10 - 2, 280 - m_bY1 - 2, m_bX1 + 12, 280 - m_bY1 + 2); pDC->Rectangle(m_bX2 + 10 - 2, 280 - m_bY2 - 2, m_bX2 + 12, 280 - m_bY2 + 2); // 恢復以前的畫筆 pDC->SelectObject(pOldPen); // 繪制邊緣 pDC->MoveTo(10,25); pDC->LineTo(265,25); pDC->LineTo(265,280); // 刪除新的畫筆 delete pPenRed; delete pPenBlue; }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -