#include <afxwin.h>
#include "VisualKB.h"
CMyApp myApp;
/////////////////////////////////////////////////////////////////////////
// CMyApp member functions
BOOL CMyApp::InitInstance ()
{
m_pMainWnd = new CMainWindow;
m_pMainWnd->ShowWindow (m_nCmdShow);
m_pMainWnd->UpdateWindow ();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////
// CMainWindow message map and member functions
BEGIN_MESSAGE_MAP (CMainWindow, CWnd)
ON_WM_CREATE ()
ON_WM_PAINT ()
ON_WM_SETFOCUS ()
ON_WM_KILLFOCUS ()
ON_WM_SETCURSOR ()
ON_WM_LBUTTONDOWN ()
ON_WM_KEYDOWN ()
ON_WM_KEYUP ()
ON_WM_SYSKEYDOWN ()
ON_WM_SYSKEYUP ()
ON_WM_CHAR ()
ON_WM_SYSCHAR ()
END_MESSAGE_MAP ()
CMainWindow::CMainWindow ()
{
m_nTextPos = 0;
m_nMsgPos = 0;
//
// Load the arrow cursor and the I-beam cursor and save their handles.
//
m_hCursorArrow = AfxGetApp ()->LoadStandardCursor (IDC_ARROW);
m_hCursorIBeam = AfxGetApp ()->LoadStandardCursor (IDC_IBEAM);
//
// Register a WNDCLASS.
//
CString strWndClass = AfxRegisterWndClass (
0,
NULL,
(HBRUSH) (COLOR_3DFACE + 1),
AfxGetApp ()->LoadStandardIcon (IDI_WINLOGO)
);
//
// Create a window.
//
CreateEx (0, strWndClass, _T ("Visual Keyboard"),
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION | WS_MINIMIZEBOX,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL);
}
int CMainWindow::OnCreate (LPCREATESTRUCT lpCreateStruct)
{
if (CWnd::OnCreate (lpCreateStruct) == -1)
return -1;
// 初始化数据成员,以生成窗口
CClientDC dc (this);
TEXTMETRIC tm;
dc.GetTextMetrics (&tm);
m_cxChar = tm.tmAveCharWidth;
m_cyChar = tm.tmHeight;
m_cyLine = tm.tmHeight + tm.tmExternalLeading;
m_rcTextBoxBorder.SetRect (16, 16, (m_cxChar * 64) + 16,
((m_cyChar * 3) / 2) + 16);
m_rcTextBox = m_rcTextBoxBorder;
m_rcTextBox.InflateRect (-2, -2);
m_rcMsgBoxBorder.SetRect (16, (m_cyChar * 4) + 16,
(m_cxChar * 64) + 16, (m_cyLine * MAX_STRINGS) +
(m_cyChar * 6) + 16);
m_rcScroll.SetRect (m_cxChar + 16, (m_cyChar * 6) + 16,
(m_cxChar * 63) + 16, (m_cyLine * MAX_STRINGS) +
(m_cyChar * 5) + 16);
m_ptTextOrigin.x = m_cxChar + 16;
m_ptTextOrigin.y = (m_cyChar / 4) + 16;
m_ptCaretPos = m_ptTextOrigin;
m_nTextLimit = (m_cxChar * 63) + 16;
m_ptHeaderOrigin.x = m_cxChar + 16;
m_ptHeaderOrigin.y = (m_cyChar * 3) + 16;
m_ptUpperMsgOrigin.x = m_cxChar + 16;
m_ptUpperMsgOrigin.y = (m_cyChar * 5) + 16;
m_ptLowerMsgOrigin.x = m_cxChar + 16;
m_ptLowerMsgOrigin.y = (m_cyChar * 5) +
(m_cyLine * (MAX_STRINGS - 1)) + 16;
m_nTabStops[0] = (m_cxChar * 24) + 16;
m_nTabStops[1] = (m_cxChar * 30) + 16;
m_nTabStops[2] = (m_cxChar * 36) + 16;
m_nTabStops[3] = (m_cxChar * 42) + 16;
m_nTabStops[4] = (m_cxChar * 46) + 16;
m_nTabStops[5] = (m_cxChar * 50) + 16;
m_nTabStops[6] = (m_cxChar * 54) + 16;
//
// 设置窗口尺寸
CRect rect (0, 0, m_rcMsgBoxBorder.right + 16,
m_rcMsgBoxBorder.bottom + 16);
CalcWindowRect (&rect);
SetWindowPos (NULL, 0, 0, rect.Width (), rect.Height (),
SWP_NOZORDER | SWP_NOMOVE | SWP_NOREDRAW);
return 0;
}
void CMainWindow::PostNcDestroy ()
{
delete this;
}
void CMainWindow::OnPaint ()
{
CPaintDC dc (this);
//
// Draw the rectangles surrounding the text box and the message list.
//
dc.DrawEdge (m_rcTextBoxBorder, EDGE_SUNKEN, BF_RECT);
dc.DrawEdge (m_rcMsgBoxBorder, EDGE_SUNKEN, BF_RECT);
//
// Draw all the text that appears in the window.
//
DrawInputText (&dc);
DrawMessageHeader (&dc);
DrawMessages (&dc);
}
void CMainWindow::OnSetFocus (CWnd* pWnd)
{
//接收到输入焦点后生成插入符
//设置插入符宽度
CreateSolidCaret (max (2, ::GetSystemMetrics (SM_CXBORDER)),
m_cyChar);
SetCaretPos (m_ptCaretPos);
ShowCaret ();
}
void CMainWindow::OnKillFocus (CWnd* pWnd)
{
// 隐藏插入符,失去焦点时销毁插入符
HideCaret ();
//取最左边字符单元的坐标初始化m_ptCaretPos
m_ptCaretPos = GetCaretPos ();
::DestroyCaret ();
}
BOOL CMainWindow::OnSetCursor (CWnd* pWnd, UINT nHitTest, UINT message)
{
// 鼠标在文本区时,设置为“I”状
// 鼠标不在文本区时,设置为箭头状
//
if (nHitTest == HTCLIENT) {
DWORD dwPos = ::GetMessagePos ();
CPoint point (LOWORD (dwPos), HIWORD (dwPos));
ScreenToClient (&point);
::SetCursor (m_rcTextBox.PtInRect (point) ?
m_hCursorIBeam : m_hCursorArrow);
return TRUE;
}
return CWnd::OnSetCursor (pWnd, nHitTest, message);
}
void CMainWindow::OnLButtonDown (UINT nFlags, CPoint point)
{
// 文本区中按下鼠标左键时,移动插入符(即光标)
//
if (m_rcTextBox.PtInRect (point)) {
m_nTextPos = GetNearestPos (point);
PositionCaret ();
}
}
void CMainWindow::OnKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_KEYDOWN"), nChar, nRepCnt, nFlags);
// 当按下左键头键、右键头键、Home键或者End键时,移动插入符
switch (nChar) {
case VK_LEFT:
if (m_nTextPos != 0) {
m_nTextPos--;
PositionCaret ();
}
break;
case VK_RIGHT:
if (m_nTextPos != m_strInputText.GetLength ()) {
m_nTextPos++;
PositionCaret ();
}
break;
case VK_HOME:
m_nTextPos = 0;
PositionCaret ();
break;
case VK_END:
m_nTextPos = m_strInputText.GetLength ();
PositionCaret ();
break;
}
}
void CMainWindow::OnChar (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_CHAR"), nChar, nRepCnt, nFlags);
CClientDC dc (this);
// 判断键盘按下的是什么字符
//
switch (nChar) {
case VK_ESCAPE:
case VK_RETURN:
m_strInputText.Empty ();
m_nTextPos = 0;
break;
case VK_BACK:
if (m_nTextPos != 0) {
m_strInputText = m_strInputText.Left (m_nTextPos - 1) +
m_strInputText.Right (m_strInputText.GetLength () -
m_nTextPos);
m_nTextPos--;
}
break;
default:
if ((nChar >= 0) && (nChar <= 31))
return;
if (m_nTextPos == m_strInputText.GetLength ()) {
m_strInputText += nChar;
m_nTextPos++;
}
else
m_strInputText.SetAt (m_nTextPos++, nChar);
CSize size = dc.GetTextExtent (m_strInputText,
m_strInputText.GetLength ());
if ((m_ptTextOrigin.x + size.cx) > m_nTextLimit) {
m_strInputText = nChar;
m_nTextPos = 1;
}
break;
}
//
// 更新文本区内容
HideCaret ();
DrawInputText (&dc);
PositionCaret (&dc);
ShowCaret ();
}
void CMainWindow::OnKeyUp (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_KEYUP"), nChar, nRepCnt, nFlags);
CWnd::OnKeyUp (nChar, nRepCnt, nFlags);
}
void CMainWindow::OnSysKeyDown (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_SYSKEYDOWN"), nChar, nRepCnt, nFlags);
CWnd::OnSysKeyDown (nChar, nRepCnt, nFlags);
}
void CMainWindow::OnSysChar (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_SYSCHAR"), nChar, nRepCnt, nFlags);
CWnd::OnSysChar (nChar, nRepCnt, nFlags);
}
void CMainWindow::OnSysKeyUp (UINT nChar, UINT nRepCnt, UINT nFlags)
{
ShowMessage (_T ("WM_SYSKEYUP"), nChar, nRepCnt, nFlags);
CWnd::OnSysKeyUp (nChar, nRepCnt, nFlags);
}
void CMainWindow::PositionCaret (CDC* pDC)
{
BOOL bRelease = FALSE;
//