?? linerparadlg.cpp
字號:
// LinerParaDlg.cpp : implementation file
//
#include "stdafx.h"
#include "EAR.h"
#include "LinerParaDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CLinerParaDlg dialog
CLinerParaDlg::CLinerParaDlg(CWnd* pParent /*=NULL*/)
: CDialog(CLinerParaDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CLinerParaDlg)
m_fA = 0.0f;
m_fB = 0.0f;
//}}AFX_DATA_INIT
}
void CLinerParaDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CLinerParaDlg)
DDX_Text(pDX, IDC_EDIT_A, m_fA);
DDX_Text(pDX, IDC_EDIT_B, m_fB);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CLinerParaDlg, CDialog)
//{{AFX_MSG_MAP(CLinerParaDlg)
ON_WM_PAINT()
ON_EN_KILLFOCUS(IDC_EDIT_A, OnKillfocusEditA)
ON_EN_KILLFOCUS(IDC_EDIT_B, OnKillfocusEditB)
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDOWN()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CLinerParaDlg message handlers
void CLinerParaDlg::OnPaint()
{
CString str;
int x1, y1, x2, y2; // 直線和坐標軸二個交點坐標
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));
// 選中當前紅色畫筆,并保存以前的畫筆
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);
// 計算直線和坐標軸二個交點坐標
if (m_fA >= 0)
{
if (((m_fA * 255 + m_fB) >= 0) && (m_fB < 255))
{
if (m_fB < 0)
{
x1 = (int) (- m_fB/m_fA + 0.5);
y1 = 0;
}
else
{
x1 = 0;
y1 = (int) (m_fB + 0.5);
}
if ((m_fA * 255 + m_fB) > 255)
{
x2 = (int) ((255- m_fB)/m_fA + 0.5);
y2 = 255;
}
else
{
x2 = 255;
y2 = (int) (255* m_fA + m_fB + 0.5);
}
}
else if(((m_fA * 255 + m_fB) < 0))
{
x1 = 0;
y1 = 0;
x2 = 255;
y2 = 0;
}
else
{
x1 = 0;
y1 = 255;
x2 = 255;
y2 = 255;
}
}
else // 斜率小于0
{
if ((m_fB > 0) && (255* m_fA + m_fB < 255))
{
if (m_fB > 255)
{
x1 = (int) ((255- m_fB)/m_fA + 0.5);
y1 = 255;
}
else
{
x1 = 0;
y1 = (int) (m_fB + 0.5);
}
if ((m_fA * 255 + m_fB) < 0)
{
x2 = (int) (- m_fB/m_fA + 0.5);
y2 = 0;
}
else
{
x2 = 255;
y2 = (int) (255* m_fA + m_fB + 0.5);
}
}
else if (m_fB <=0)
{
x1 = 0;
y1 = 0;
x2 = 255;
y2 = 0;
}
else
{
x1 = 0;
y1 = 255;
x2 = 255;
y2 = 255;
}
}
// 繪制坐標值
str.Format("(%d, %d)", x1, y1);
pDC->TextOut(x1 + 10, 280 - y1 + 1, str);
str.Format("(%d, %d)", x2, y2);
pDC->TextOut(x2 + 10, 280 - y2 + 1, str);
// 繪制用戶指定的線性變換直線(注意轉換坐標系)
pDC->MoveTo(x1 + 10, 280 - y1);
pDC->LineTo(x2 + 10, 280 - y2);
// 恢復以前的畫筆
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 CLinerParaDlg::OnKillfocusEditA()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(m_MouseRect, TRUE);
}
void CLinerParaDlg::OnKillfocusEditB()
{
// TODO: Add your control notification handler code here
UpdateData(TRUE);
InvalidateRect(m_MouseRect, TRUE);
}
void CLinerParaDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 當用戶釋放鼠標左鍵停止拖動
if (m_bIsDraging)
{
// 判斷當前光標是否在繪制區域
if(m_MouseRect.PtInRect(point))
{
m_p2 = point;
// 轉換坐標系
m_p2.x = m_p2.x - m_MouseRect.left + 10;
m_p2.y = m_p2.y - m_MouseRect.top + 25;
if ((m_p1 != m_p2) && (m_p1.x != m_p2.x))
{
// 轉換坐標系
m_p1.x = m_p1.x - 10;
m_p1.y = 280 - m_p1.y;
m_p2.x = m_p2.x - 10;
m_p2.y = 280 - m_p2.y;
// 計算斜率和截距
m_fA = (float) (m_p2.y - m_p1.y) / (m_p2.x - m_p1.x);
m_fB = m_p1.y - m_fA * m_p1.x;
UpdateData(FALSE);
}
InvalidateRect(m_MouseRect, TRUE);
}
else
{
// 用戶在繪制區域外放開鼠標左鍵
CWnd* pWnd = GetDlgItem(IDC_COORD); // 獲取繪圖的標簽
CDC* pDC = pWnd->GetDC(); // 獲取設備上下文
int nOldDrawMode = pDC->SetROP2(R2_XORPEN); // 設置繪制方式為異或模式
CPen* pPen = new CPen;
pPen->CreatePen(PS_DOT,1,RGB(0,0,0));
CGdiObject* pOldPen = pDC->SelectObject(pPen);
// 判斷是否已經畫過橡皮筋線
if (m_bDrawed)
{
pDC->MoveTo(m_p1);
pDC->LineTo(m_p2);
}
pDC->SelectObject(pOldPen); // 選回以前的畫筆
pDC->SetROP2(nOldDrawMode); // 恢復成以前的繪制模式
delete pPen;
ReleaseDC(pDC);
}
::ReleaseCapture(); // 解除對鼠標事件的跟蹤
m_bIsDraging = FALSE; // 重置拖動狀態
}
CDialog::OnLButtonUp(nFlags, point);
}
void CLinerParaDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 判斷當前光標是否在繪制區域
if(m_MouseRect.PtInRect(point))
{
::SetCursor(::LoadCursor(NULL, IDC_CROSS));
if (m_bIsDraging)
{
CWnd* pWnd = GetDlgItem(IDC_COORD);
CDC* pDC = pWnd->GetDC();
// 設置繪制方式為異或模式
int nOldDrawMode = pDC->SetROP2(R2_XORPEN);
CPen* pPen = new CPen;
pPen->CreatePen(PS_DOT,1,RGB(0,0,0));
CGdiObject* pOldPen = pDC->SelectObject(pPen);
// 判斷是否已經畫過橡皮筋線
if (m_bDrawed)
{
pDC->MoveTo(m_p1);
pDC->LineTo(m_p2);
}
m_p2 = point;
// 轉換坐標系
m_p2.x = m_p2.x - m_MouseRect.left + 10;
m_p2.y = m_p2.y - m_MouseRect.top + 25;
// 繪制一條新橡皮筋線
pDC->MoveTo(m_p1);
pDC->LineTo(m_p2);
m_bDrawed = TRUE;
pDC->SelectObject(pOldPen); // 選回以前的畫筆
pDC->SetROP2(nOldDrawMode); // 恢復成以前的繪制模式
delete pPen;
ReleaseDC(pDC);
}
}
else
{
if (m_bIsDraging)
{
::SetCursor(::LoadCursor(NULL, IDC_NO));
}
}
CDialog::OnMouseMove(nFlags, point);
}
void CLinerParaDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// 當用戶單擊鼠標左鍵開始拖動
if(m_MouseRect.PtInRect(point))
{
m_p1 = point;
// 轉換坐標系
m_p1.x = m_p1.x - m_MouseRect.left + 10;
m_p1.y = m_p1.y - m_MouseRect.top + 25;
m_bIsDraging = TRUE;
m_bDrawed = FALSE;
::SetCursor(::LoadCursor(NULL, IDC_CROSS));
// 開始跟蹤鼠標事件(保證當鼠標移動到窗體外時也可以接收到鼠標釋放事件)
SetCapture();
}
CDialog::OnLButtonDown(nFlags, point);
}
BOOL CLinerParaDlg::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_bIsDraging = FALSE;
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CLinerParaDlg::OnOK()
{
// TODO: Add extra validation here
CDialog::OnOK();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -