- 用紙設計キャンバス(1/100mm 基準)・テキスト/画像/矩形/直線アイテム - コピー/複製/Zオーダー/整列/グリッドスナップ、原紙(下絵)読込と印刷可否 - GDI 直接印刷、.otz 保存/再読込(版数つき) - MFC/CRT 静的リンクで VS2022 IDE から F5 デバッグ可
77 lines
2.3 KiB
C++
77 lines
2.3 KiB
C++
// TextItem.cpp
|
|
#include "pch.h"
|
|
#include "TextItem.h"
|
|
|
|
IMPLEMENT_SERIAL(CTextItem, CPrintItem, 1)
|
|
|
|
// コンストラクタ。既定値(文字列・フォント・10pt・左揃え・非太字)で初期化。
|
|
CTextItem::CTextItem()
|
|
: m_text(_T("テキスト"))
|
|
, m_fontName(_T("MS ゴシック"))
|
|
, m_pointSize(10.0)
|
|
, m_align(0)
|
|
, m_bold(FALSE)
|
|
{
|
|
}
|
|
|
|
// テキストを描画する。画面・印刷の双方から同じ本関数が呼ばれ、
|
|
// CoordMap が DPI/ズームの違いを吸収するため、物理サイズ(pt)が
|
|
// どの解像度でも正しく出る。selected=true のとき選択枠も描く。
|
|
void CTextItem::Draw(CDC& dc, const CoordMap& map, bool selected)
|
|
{
|
|
CRect dr = map.MmToDev(m_rect);
|
|
|
|
// フォント高さをデバイス単位で算出(ポイント→1/100mm→デバイス)
|
|
int fontHeight = map.PointToDevHeight(m_pointSize);
|
|
if (fontHeight < 1) fontHeight = 1;
|
|
|
|
LOGFONT lf;
|
|
ZeroMemory(&lf, sizeof(lf));
|
|
lf.lfHeight = -fontHeight; // 負値 = 文字セル高さ(=文字高さ指定)
|
|
lf.lfWeight = m_bold ? FW_BOLD : FW_NORMAL;
|
|
lf.lfCharSet = DEFAULT_CHARSET;
|
|
_tcsncpy_s(lf.lfFaceName, LF_FACESIZE, m_fontName, _TRUNCATE);
|
|
|
|
CFont font;
|
|
font.CreateFontIndirect(&lf);
|
|
CFont* pOld = dc.SelectObject(&font);
|
|
int oldBk = dc.SetBkMode(TRANSPARENT);
|
|
COLORREF oldColor = dc.SetTextColor(RGB(0, 0, 0));
|
|
|
|
UINT fmt = DT_NOPREFIX | DT_WORDBREAK;
|
|
if (m_align == 1) fmt |= DT_CENTER;
|
|
else if (m_align == 2) fmt |= DT_RIGHT;
|
|
else fmt |= DT_LEFT;
|
|
|
|
CRect textRect = dr;
|
|
dc.DrawText(m_text, textRect, fmt);
|
|
|
|
dc.SetTextColor(oldColor);
|
|
dc.SetBkMode(oldBk);
|
|
dc.SelectObject(pOld);
|
|
|
|
if (selected)
|
|
DrawSelection(dc, dr);
|
|
}
|
|
|
|
// 自身の複製を生成して返す(コピー&貼付け・複製機能で使用)。
|
|
CPrintItem* CTextItem::Clone() const
|
|
{
|
|
CTextItem* p = new CTextItem(*this);
|
|
return p;
|
|
}
|
|
|
|
// 永続化。基底(矩形)に続けてテキスト固有の属性を読み書きする。
|
|
void CTextItem::Serialize(CArchive& ar)
|
|
{
|
|
CPrintItem::Serialize(ar);
|
|
if (ar.IsStoring())
|
|
{
|
|
ar << m_text << m_fontName << m_pointSize << m_align << m_bold;
|
|
}
|
|
else
|
|
{
|
|
ar >> m_text >> m_fontName >> m_pointSize >> m_align >> m_bold;
|
|
}
|
|
}
|