MFC簡易テキスト編集機能
1.挿入子の作成
2.マウスクリックで挿入子の位置を変更する
3.キーボードイベント応答
afx_msg int CTextEditorDlg::OnCreate(LPCREATESTRUCT crect)
{
TEXTMETRIC tm;
CClientDC cdc(this);
GetTextMetrics(cdc.m_hDC,&tm);
CreateSolidCaret(tm.tmAveCharWidth/3,tm.tmHeight);
SetCaretPos(m_CaretPit);
ShowCaret();
return 0;
}
2.マウスクリックで挿入子の位置を変更する
afx_msg void CTextEditorDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
m_InputStr = "";
m_CaretPit.x = point.x;
m_CaretPit.y = point.y;
SetCaretPos(m_CaretPit);
}
3.キーボードイベント応答
afx_msg void CTextEditorDlg::OnKeyDown(UINT nchar, UINT nRepCnt, UINT nFlags)
{
POINT tempp;
CSize size;
int interval = 'A' - 'a';
CClientDC cdc(this);
TEXTMETRIC tm;
GetTextMetrics(cdc.m_hDC, &tm);
unsigned char kbuf[256];
GetKeyboardState(kbuf);
int cap = kbuf[VK_CAPITAL] & 1;
switch (nchar)
{
case VK_SHIFT:
m_ShiftFlag = 1;
break;
case VK_RETURN:
size = cdc.GetTextExtent(m_InputStr);
m_CaretPit.x = m_CaretPit.x + size.cx;
m_CaretPit.y = m_CaretPit.y + tm.tmHeight;
SetCaretPos(m_CaretPit);
m_InputStr = "";
break;
default:
if ((m_ShiftFlag+cap)==1)
m_InputStr += (TCHAR)nchar;
else
m_InputStr += (TCHAR)(nchar-interval);
size = cdc.GetTextExtent(m_InputStr);
tempp = m_CaretPit;
tempp.x = m_CaretPit.x + size.cx;
SetCaretPos(tempp);
cdc.TextOutW(m_CaretPit.x, m_CaretPit.y, m_InputStr);
break;
}
}
void CTextEditorDlg::OnKeyUp(UINT nchar, UINT nRepCnt, UINT nFlags)
{
switch (nchar)
{
case VK_SHIFT:
m_ShiftFlag = 0;
break;
}
}