?? textobj.cpp
字號:
//textobj.cpp 文本類的實現文檔
#include "StdAfx.h"
#include ".\textobj.h"
#include "visdrawview.h"
#include "visdrawdoc.h"
IMPLEMENT_SERIAL(CTextObj, CFigureObj, 0)
CTextObj::CTextObj(void)
{
}
CTextObj::~CTextObj(void)
{
}
CTextObj::CTextObj(const CRect& position)
: CFigureObj(position)
{
ASSERT_VALID(this);
//m_plDocument = NULL;
//
m_pPen = TRUE;
m_plogpen.lopnStyle = PS_NULL;
m_plogpen.lopnWidth.x = 1;
m_plogpen.lopnWidth.y = 1;
m_plogpen.lopnColor = RGB(0, 0, 192);
m_pBrush = FALSE;
m_plogbrush.lbStyle = BS_SOLID;
m_plogbrush.lbColor = RGB(192, 192, 192);
m_plogbrush.lbHatch = HS_HORIZONTAL;
m_ltx=0.0;m_lty=0.0;m_rbx=0.0;m_rby=0.0;
m_string="0000";
}
void CTextObj::Draw(CVisDrawView* pView, CDC* pDC)
{
ASSERT_VALID(this);
CBrush brush;
if (!brush.CreateBrushIndirect(&m_plogbrush))
return;
CPen pen;
if (!pen.CreatePenIndirect(&m_plogpen))
return;
CBrush* pOldBrush;
CPen* pOldPen;
if (m_pBrush)
pOldBrush = pDC->SelectObject(&brush);
else
pOldBrush = (CBrush*)pDC->SelectStockObject(WHITE_BRUSH);
if (m_pPen)
pOldPen = pDC->SelectObject(&pen);
else
pOldPen = (CPen*)pDC->SelectStockObject(NULL_PEN);
// CBrush newBrush;
//newBrush.CreateStockObject(NULL_BRUSH);
// pOldBrush =(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
//把世界坐標轉化為邏輯坐標
CPoint m_ltPoint, m_rbPoint;
CRect rect = CalcBounds(pView);
//繪制矩形圖元
pDC->Rectangle(rect);
pDC->DrawText(m_string,rect, DT_LEFT | DT_WORDBREAK );
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
void CTextObj::Serialize(CArchive& ar)
{
ASSERT_VALID(this);
if (ar.IsStoring())
{
ar << (WORD)m_pPen;
ar.Write(&m_plogpen, sizeof(LOGPEN));
ar << m_ltx << m_lty << m_rbx << m_rby;
}
else
{
// get the document back pointer from the archive
m_pDocument = (CVisDrawDoc*)ar.m_pDocument;
ASSERT_VALID(m_pDocument);
ASSERT_KINDOF(CVisDrawDoc, m_pDocument);
WORD wTemp;
ar >> wTemp; m_pPen = (BOOL)wTemp;
ar.Read(&m_plogpen,sizeof(LOGPEN));
ar >> m_ltx >> m_lty >> m_rbx >> m_rby;
}
}
void CTextObj::SetPoint(int ptNumber, double x, double y)
{
ASSERT(ptNumber <=2);
switch(ptNumber)
{
case 1:
m_ltx = x;
m_lty = y;
break;
case 2:
m_rbx = x;
m_rby = y;
break;
}
}
void CTextObj::GetPoint(int ptNumber, double& x, double& y)
{
ASSERT(ptNumber <=2);
switch(ptNumber)
{
case 1:
x = m_ltx;
y = m_lty;
break;
case 2:
x = m_rbx;
y = m_rby;
break;
}
}
CRect CTextObj::CalcBounds(CVisDrawView* pView)
{
CRect rect;
CPoint ltPoint, rbPoint;
pView->WorldToClient(ltPoint,m_ltx,m_lty);
pView->WorldToClient(rbPoint,m_rbx,m_rby);
rect.TopLeft() = ltPoint;
rect.BottomRight() = rbPoint;
m_position = rect;
return rect;
}
BOOL CTextObj::IsSelected(CVisDrawView* pView, const CPoint& point)
{
CPoint local, StartPt, EndPt;
//參數point是鼠標的邏輯坐標
int distance, nSelectDistance;
CRect rect = m_position;
//CPoint local;
local = point;
//鼠標點的設備坐標
pView->DocToClient(rect);
//識別精度值
nSelectDistance = pView->GetDocument()->GetSetectDistance()/2;
//鼠標點的設備坐標
pView->DocToClient(local);
//CPoint pt;
//把點的坐標轉化為邏輯坐標
//pView->WorldToClient(pt, m_pointx, m_pointy);
//把點的坐標由邏輯坐標轉化為設備坐標
//pView->DocToClient(pt);
//計算鼠標點point與點pt之間的像素距離
//第一條直線
distance = abs(local.y - rect.top);
if(distance < nSelectDistance) return true;
//第二條直線
distance = abs(local.x - rect.right);
if(distance < nSelectDistance) return true;
//第三條直線
distance = abs(local.y - rect.bottom);
if(distance < nSelectDistance) return true;
//第四條直線
distance = abs(local.x - rect.left);
if(distance < nSelectDistance) return true;
return false;
}
//返回手柄個數
int CTextObj::GetHandleCount()
{
return 9;
}
// 返回手柄中心邏輯坐標
CPoint CTextObj::GetHandle(CVisDrawView* pView, int nHandle)
{
return CFigureObj::GetHandle(pView, nHandle);
}
// point 為邏輯坐標
void CTextObj::MoveHandleTo(int nHandle, CPoint point, CVisDrawView* pView)
{
ASSERT_VALID(this);
//把鼠標邏輯坐標轉化為世界坐標
double pointx,pointy;
pView->ClientToWorld(point,pointx,pointy);
switch (nHandle)
{
default:
ASSERT(FALSE);
case 1:
SetPoint(1,pointx,pointy);
break;
case 2:
SetPoint(1,m_ltx,pointy);
break;
case 3:
SetPoint(1,m_ltx,pointy);
SetPoint(2,pointx,m_rby);
break;
case 4:
SetPoint(2,pointx,m_rby);
break;
case 5:
SetPoint(2,pointx,pointy);
break;
case 6:
SetPoint(2,m_rbx,pointy);
break;
case 7:
SetPoint(1,pointx,m_lty);
SetPoint(2,m_rbx,pointy);
break;
case 8:
SetPoint(1,pointx,m_lty);
break;
case 9:
break;
}
//重新計算邊界矩形
CalcBounds(pView);
//修改文檔標志
m_pDocument->SetModifiedFlag();
}
// delta為邏輯坐標
void CTextObj::MoveTo(CPoint delta, CVisDrawView* pView)
{
ASSERT_VALID(this);
//把delta轉化為世界坐標
double pointx,pointy;
pointx = pView->ClientToWorld(delta.x);
pointy = -pView->ClientToWorld(delta.y);
//修改矩形兩頂點坐標
m_ltx = m_ltx + pointx;
m_lty = m_lty + pointy;
m_rbx = m_rbx + pointx;
m_rby = m_rby + pointy;
//重新計算邊界矩形
CalcBounds(pView);
//修改文檔標志
m_pDocument->SetModifiedFlag();
}
void CTextObj::SetInputText(LPCTSTR lpszText)
{
ASSERT_VALID(this);
// AfxMessageBox(lpszText);
m_string=lpszText;
Invalidate();
m_pDocument->SetModifiedFlag();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -